1 /*===-- CXStoreDiagnostic.cpp - Diagnostics C Interface ----------*- C++ -*-===*\ 2 |* *| 3 |* The LLVM Compiler Infrastructure *| 4 |* *| 5 |* This file is distributed under the University of Illinois Open Source *| 6 |* License. See LICENSE.TXT for details. *| 7 |* *| 8 |*===----------------------------------------------------------------------===*| 9 |* *| 10 |* Implements part of the diagnostic functions of the Clang C interface. *| 11 |* *| 12 \*===----------------------------------------------------------------------===*/ 13 14 #include "CIndexDiagnostic.h" 15 #include "CIndexer.h" 16 #include "CXTranslationUnit.h" 17 #include "CXSourceLocation.h" 18 #include "CXString.h" 19 20 #include "clang/Frontend/ASTUnit.h" 21 #include "clang/Frontend/FrontendDiagnostic.h" 22 #include "llvm/ADT/SmallString.h" 23 #include "llvm/ADT/Twine.h" 24 #include "llvm/Support/MemoryBuffer.h" 25 #include "llvm/Support/raw_ostream.h" 26 27 using namespace clang; 28 using namespace clang::cxloc; 29 using namespace clang::cxstring; 30 31 CXDiagnosticSeverity CXStoredDiagnostic::getSeverity() const { 32 switch (Diag.getLevel()) { 33 case DiagnosticsEngine::Ignored: return CXDiagnostic_Ignored; 34 case DiagnosticsEngine::Note: return CXDiagnostic_Note; 35 case DiagnosticsEngine::Warning: return CXDiagnostic_Warning; 36 case DiagnosticsEngine::Error: return CXDiagnostic_Error; 37 case DiagnosticsEngine::Fatal: return CXDiagnostic_Fatal; 38 } 39 40 llvm_unreachable("Invalid diagnostic level"); 41 } 42 43 CXSourceLocation CXStoredDiagnostic::getLocation() const { 44 if (Diag.getLocation().isInvalid()) 45 return clang_getNullLocation(); 46 47 return translateSourceLocation(Diag.getLocation().getManager(), 48 LangOpts, Diag.getLocation()); 49 } 50 51 CXString CXStoredDiagnostic::getSpelling() const { 52 return createCXString(Diag.getMessage(), false); 53 } 54 55 CXString CXStoredDiagnostic::getDiagnosticOption(CXString *Disable) const { 56 unsigned ID = Diag.getID(); 57 StringRef Option = DiagnosticIDs::getWarningOptionForDiag(ID); 58 if (!Option.empty()) { 59 if (Disable) 60 *Disable = createCXString((Twine("-Wno-") + Option).str()); 61 return createCXString((Twine("-W") + Option).str()); 62 } 63 64 if (ID == diag::fatal_too_many_errors) { 65 if (Disable) 66 *Disable = createCXString("-ferror-limit=0"); 67 return createCXString("-ferror-limit="); 68 } 69 70 return createCXString(""); 71 } 72 73 unsigned CXStoredDiagnostic::getCategory() const { 74 return DiagnosticIDs::getCategoryNumberForDiag(Diag.getID()); 75 } 76 77 CXString CXStoredDiagnostic::getCategoryText() const { 78 unsigned catID = DiagnosticIDs::getCategoryNumberForDiag(Diag.getID()); 79 return createCXString(DiagnosticIDs::getCategoryNameFromID(catID)); 80 } 81 82 unsigned CXStoredDiagnostic::getNumRanges() const { 83 if (Diag.getLocation().isInvalid()) 84 return 0; 85 86 return Diag.range_size(); 87 } 88 89 CXSourceRange CXStoredDiagnostic::getRange(unsigned int Range) const { 90 assert(Diag.getLocation().isValid()); 91 return translateSourceRange(Diag.getLocation().getManager(), 92 LangOpts, 93 Diag.range_begin()[Range]); 94 } 95 96 unsigned CXStoredDiagnostic::getNumFixIts() const { 97 if (Diag.getLocation().isInvalid()) 98 return 0; 99 return Diag.fixit_size(); 100 } 101 102 CXString CXStoredDiagnostic::getFixIt(unsigned FixIt, 103 CXSourceRange *ReplacementRange) const { 104 const FixItHint &Hint = Diag.fixit_begin()[FixIt]; 105 if (ReplacementRange) { 106 // Create a range that covers the entire replacement (or 107 // removal) range, adjusting the end of the range to point to 108 // the end of the token. 109 *ReplacementRange = translateSourceRange(Diag.getLocation().getManager(), 110 LangOpts, Hint.RemoveRange); 111 } 112 return createCXString(Hint.CodeToInsert); 113 } 114 115