slang_diagnostic_buffer.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright 2010, The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "slang_diagnostic_buffer.h"
  17. #include "clang/Basic/SourceLocation.h"
  18. #include "clang/Basic/SourceManager.h"
  19. #include "llvm/ADT/SmallString.h"
  20. #include "slang_assert.h"
  21. namespace slang {
  22. DiagnosticBuffer::DiagnosticBuffer()
  23. : mSOS(new llvm::raw_string_ostream(mDiags)) {
  24. }
  25. DiagnosticBuffer::~DiagnosticBuffer() {
  26. }
  27. void DiagnosticBuffer::HandleDiagnostic(
  28. clang::DiagnosticsEngine::Level DiagLevel,
  29. clang::Diagnostic const &Info) {
  30. clang::SourceLocation const &SrcLoc = Info.getLocation();
  31. std::string Message;
  32. llvm::raw_string_ostream stream(Message);
  33. if (SrcLoc.isValid()) {
  34. SrcLoc.print(stream, Info.getSourceManager());
  35. stream << ": ";
  36. }
  37. switch (DiagLevel) {
  38. case clang::DiagnosticsEngine::Note: {
  39. stream << "note: ";
  40. break;
  41. }
  42. case clang::DiagnosticsEngine::Warning: {
  43. stream << "warning: ";
  44. break;
  45. }
  46. case clang::DiagnosticsEngine::Error: {
  47. stream << "error: ";
  48. break;
  49. }
  50. case clang::DiagnosticsEngine::Fatal: {
  51. stream << "fatal: ";
  52. break;
  53. }
  54. default: {
  55. slangAssert(0 && "Diagnostic not handled during diagnostic buffering!");
  56. }
  57. }
  58. // 100 is enough for storing general diagnosis Message
  59. llvm::SmallString<100> Buf;
  60. Info.FormatDiagnostic(Buf);
  61. stream << Buf.str() << '\n';
  62. stream.flush();
  63. if (mIncludedMessages.find(Message) == mIncludedMessages.end()) {
  64. mIncludedMessages.insert(Message);
  65. (*mSOS) << Message;
  66. }
  67. }
  68. } // namespace slang