CXStoredDiagnostic.cpp revision 153221717e39ce41323d5bc6b8b8bf130923c1bd
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 return CXDiagnostic_Ignored; 42} 43 44CXSourceLocation CXStoredDiagnostic::getLocation() const { 45 if (Diag.getLocation().isInvalid()) 46 return clang_getNullLocation(); 47 48 return translateSourceLocation(Diag.getLocation().getManager(), 49 LangOpts, Diag.getLocation()); 50} 51 52CXString CXStoredDiagnostic::getSpelling() const { 53 return createCXString(Diag.getMessage(), false); 54} 55 56CXString CXStoredDiagnostic::getDiagnosticOption(CXString *Disable) const { 57 unsigned ID = Diag.getID(); 58 StringRef Option = DiagnosticIDs::getWarningOptionForDiag(ID); 59 if (!Option.empty()) { 60 if (Disable) 61 *Disable = createCXString((Twine("-Wno-") + Option).str()); 62 return createCXString((Twine("-W") + Option).str()); 63 } 64 65 if (ID == diag::fatal_too_many_errors) { 66 if (Disable) 67 *Disable = createCXString("-ferror-limit=0"); 68 return createCXString("-ferror-limit="); 69 } 70 71 bool EnabledByDefault; 72 if (DiagnosticIDs::isBuiltinExtensionDiag(ID, EnabledByDefault) && 73 !EnabledByDefault) 74 return createCXString("-pedantic"); 75 76 return createCXString(""); 77} 78 79unsigned CXStoredDiagnostic::getCategory() const { 80 return DiagnosticIDs::getCategoryNumberForDiag(Diag.getID()); 81} 82 83unsigned CXStoredDiagnostic::getNumRanges() const { 84 if (Diag.getLocation().isInvalid()) 85 return 0; 86 87 return Diag.range_size(); 88} 89 90CXSourceRange CXStoredDiagnostic::getRange(unsigned int Range) const { 91 assert(Diag.getLocation().isValid()); 92 return translateSourceRange(Diag.getLocation().getManager(), 93 LangOpts, 94 Diag.range_begin()[Range]); 95} 96 97unsigned CXStoredDiagnostic::getNumFixIts() const { 98 if (Diag.getLocation().isInvalid()) 99 return 0; 100 return Diag.fixit_size(); 101} 102 103CXString CXStoredDiagnostic::getFixIt(unsigned FixIt, 104 CXSourceRange *ReplacementRange) const { 105 const FixItHint &Hint = Diag.fixit_begin()[FixIt]; 106 if (ReplacementRange) { 107 // Create a range that covers the entire replacement (or 108 // removal) range, adjusting the end of the range to point to 109 // the end of the token. 110 *ReplacementRange = translateSourceRange(Diag.getLocation().getManager(), 111 LangOpts, Hint.RemoveRange); 112 } 113 return createCXString(Hint.CodeToInsert); 114} 115 116