CXStoredDiagnostic.cpp revision 7530c034c0c71a64c5a9173206d9742ae847af8b
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 27using namespace clang; 28using namespace clang::cxloc; 29using namespace clang::cxstring; 30 31CXDiagnosticSeverity 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 43CXSourceLocation 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 51CXString CXStoredDiagnostic::getSpelling() const { 52 return createCXString(Diag.getMessage(), false); 53} 54 55CXString 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 bool EnabledByDefault; 71 if (DiagnosticIDs::isBuiltinExtensionDiag(ID, EnabledByDefault) && 72 !EnabledByDefault) 73 return createCXString("-pedantic"); 74 75 return createCXString(""); 76} 77 78unsigned CXStoredDiagnostic::getCategory() const { 79 return DiagnosticIDs::getCategoryNumberForDiag(Diag.getID()); 80} 81 82unsigned CXStoredDiagnostic::getNumRanges() const { 83 if (Diag.getLocation().isInvalid()) 84 return 0; 85 86 return Diag.range_size(); 87} 88 89CXSourceRange 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 96unsigned CXStoredDiagnostic::getNumFixIts() const { 97 if (Diag.getLocation().isInvalid()) 98 return 0; 99 return Diag.fixit_size(); 100} 101 102CXString 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