1d5a2a073SDaniel Dunbar /*===-- CIndexDiagnostic.h - Diagnostics C Interface ------------*- C++ -*-===*\ 2d5a2a073SDaniel Dunbar |* *| 3*2946cd70SChandler Carruth |* Part of the LLVM Project, under the Apache License v2.0 with LLVM *| 4*2946cd70SChandler Carruth |* Exceptions. *| 5*2946cd70SChandler Carruth |* See https://llvm.org/LICENSE.txt for license information. *| 6*2946cd70SChandler Carruth |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *| 7d5a2a073SDaniel Dunbar |* *| 8d5a2a073SDaniel Dunbar |*===----------------------------------------------------------------------===*| 9d5a2a073SDaniel Dunbar |* *| 10d5a2a073SDaniel Dunbar |* Implements the diagnostic functions of the Clang C interface. *| 11d5a2a073SDaniel Dunbar |* *| 12d5a2a073SDaniel Dunbar \*===----------------------------------------------------------------------===*/ 132f5db8b3SBenjamin Kramer #ifndef LLVM_CLANG_TOOLS_LIBCLANG_CINDEXDIAGNOSTIC_H 142f5db8b3SBenjamin Kramer #define LLVM_CLANG_TOOLS_LIBCLANG_CINDEXDIAGNOSTIC_H 15d5a2a073SDaniel Dunbar 16bb2c7101STed Kremenek #include "clang-c/Index.h" 17759548b5SDavid Blaikie #include <memory> 18d010ba47STed Kremenek #include <vector> 19d010ba47STed Kremenek #include <assert.h> 20bb2c7101STed Kremenek 21d5a2a073SDaniel Dunbar namespace clang { 22d5a2a073SDaniel Dunbar 23d5a2a073SDaniel Dunbar class LangOptions; 24d5a2a073SDaniel Dunbar class StoredDiagnostic; 25d010ba47STed Kremenek class CXDiagnosticImpl; 26d010ba47STed Kremenek 27d010ba47STed Kremenek class CXDiagnosticSetImpl { 28759548b5SDavid Blaikie std::vector<std::unique_ptr<CXDiagnosticImpl>> Diagnostics; 29d010ba47STed Kremenek const bool IsExternallyManaged; 30d010ba47STed Kremenek public: 31d010ba47STed Kremenek CXDiagnosticSetImpl(bool isManaged = false) IsExternallyManaged(isManaged)32d010ba47STed Kremenek : IsExternallyManaged(isManaged) {} 33d010ba47STed Kremenek 34d010ba47STed Kremenek virtual ~CXDiagnosticSetImpl(); 35d010ba47STed Kremenek getNumDiagnostics()36d010ba47STed Kremenek size_t getNumDiagnostics() const { 37d010ba47STed Kremenek return Diagnostics.size(); 38d010ba47STed Kremenek } 39d010ba47STed Kremenek getDiagnostic(unsigned i)40d010ba47STed Kremenek CXDiagnosticImpl *getDiagnostic(unsigned i) const { 41d010ba47STed Kremenek assert(i < getNumDiagnostics()); 42759548b5SDavid Blaikie return Diagnostics[i].get(); 43d010ba47STed Kremenek } 44d010ba47STed Kremenek 45759548b5SDavid Blaikie void appendDiagnostic(std::unique_ptr<CXDiagnosticImpl> D); 46d010ba47STed Kremenek empty()47d010ba47STed Kremenek bool empty() const { 48d010ba47STed Kremenek return Diagnostics.empty(); 49d010ba47STed Kremenek } 50d010ba47STed Kremenek isExternallyManaged()51d010ba47STed Kremenek bool isExternallyManaged() const { return IsExternallyManaged; } 52d010ba47STed Kremenek }; 53d5a2a073SDaniel Dunbar 54bb2c7101STed Kremenek class CXDiagnosticImpl { 55bb2c7101STed Kremenek public: 56914c7e62STed Kremenek enum Kind { StoredDiagnosticKind, LoadedDiagnosticKind, 57914c7e62STed Kremenek CustomNoteDiagnosticKind }; 58bb2c7101STed Kremenek 59bb2c7101STed Kremenek virtual ~CXDiagnosticImpl(); 60bb2c7101STed Kremenek 619fc8faf9SAdrian Prantl /// Return the severity of the diagnostic. 62bb2c7101STed Kremenek virtual CXDiagnosticSeverity getSeverity() const = 0; 63bb2c7101STed Kremenek 649fc8faf9SAdrian Prantl /// Return the location of the diagnostic. 65bb2c7101STed Kremenek virtual CXSourceLocation getLocation() const = 0; 66bb2c7101STed Kremenek 679fc8faf9SAdrian Prantl /// Return the spelling of the diagnostic. 68bb2c7101STed Kremenek virtual CXString getSpelling() const = 0; 69bb2c7101STed Kremenek 709fc8faf9SAdrian Prantl /// Return the text for the diagnostic option. 71bb2c7101STed Kremenek virtual CXString getDiagnosticOption(CXString *Disable) const = 0; 72bb2c7101STed Kremenek 739fc8faf9SAdrian Prantl /// Return the category of the diagnostic. 74bb2c7101STed Kremenek virtual unsigned getCategory() const = 0; 75bb2c7101STed Kremenek 769fc8faf9SAdrian Prantl /// Return the category string of the diagnostic. 7726a6d498STed Kremenek virtual CXString getCategoryText() const = 0; 7826a6d498STed Kremenek 799fc8faf9SAdrian Prantl /// Return the number of source ranges for the diagnostic. 80bb2c7101STed Kremenek virtual unsigned getNumRanges() const = 0; 81bb2c7101STed Kremenek 829fc8faf9SAdrian Prantl /// Return the source ranges for the diagnostic. 83bb2c7101STed Kremenek virtual CXSourceRange getRange(unsigned Range) const = 0; 84bb2c7101STed Kremenek 859fc8faf9SAdrian Prantl /// Return the number of FixIts. 86bb2c7101STed Kremenek virtual unsigned getNumFixIts() const = 0; 87bb2c7101STed Kremenek 889fc8faf9SAdrian Prantl /// Return the FixIt information (source range and inserted text). 89bb2c7101STed Kremenek virtual CXString getFixIt(unsigned FixIt, 90bb2c7101STed Kremenek CXSourceRange *ReplacementRange) const = 0; 91bb2c7101STed Kremenek getKind()92bb2c7101STed Kremenek Kind getKind() const { return K; } 93bb2c7101STed Kremenek getChildDiagnostics()94d010ba47STed Kremenek CXDiagnosticSetImpl &getChildDiagnostics() { 95d010ba47STed Kremenek return ChildDiags; 96d010ba47STed Kremenek } 97d010ba47STed Kremenek 98bb2c7101STed Kremenek protected: CXDiagnosticImpl(Kind k)99bb2c7101STed Kremenek CXDiagnosticImpl(Kind k) : K(k) {} 100d010ba47STed Kremenek CXDiagnosticSetImpl ChildDiags; 101d010ba47STed Kremenek append(std::unique_ptr<CXDiagnosticImpl> D)102759548b5SDavid Blaikie void append(std::unique_ptr<CXDiagnosticImpl> D) { 103759548b5SDavid Blaikie ChildDiags.appendDiagnostic(std::move(D)); 104d010ba47STed Kremenek } 105bb2c7101STed Kremenek 106bb2c7101STed Kremenek private: 107bb2c7101STed Kremenek Kind K; 108bb2c7101STed Kremenek }; 109bb2c7101STed Kremenek 1109fc8faf9SAdrian Prantl /// The storage behind a CXDiagnostic 111bb2c7101STed Kremenek struct CXStoredDiagnostic : public CXDiagnosticImpl { 112d5a2a073SDaniel Dunbar const StoredDiagnostic &Diag; 113d5a2a073SDaniel Dunbar const LangOptions &LangOpts; 114d5a2a073SDaniel Dunbar CXStoredDiagnosticCXStoredDiagnostic115d5a2a073SDaniel Dunbar CXStoredDiagnostic(const StoredDiagnostic &Diag, 116d5a2a073SDaniel Dunbar const LangOptions &LangOpts) 117bb2c7101STed Kremenek : CXDiagnosticImpl(StoredDiagnosticKind), 118bb2c7101STed Kremenek Diag(Diag), LangOpts(LangOpts) { } 119bb2c7101STed Kremenek ~CXStoredDiagnosticCXStoredDiagnostic120637d1e66SAngel Garcia Gomez ~CXStoredDiagnostic() override {} 121bb2c7101STed Kremenek 1229fc8faf9SAdrian Prantl /// Return the severity of the diagnostic. 12336835568SCraig Topper CXDiagnosticSeverity getSeverity() const override; 124bb2c7101STed Kremenek 1259fc8faf9SAdrian Prantl /// Return the location of the diagnostic. 12636835568SCraig Topper CXSourceLocation getLocation() const override; 127bb2c7101STed Kremenek 1289fc8faf9SAdrian Prantl /// Return the spelling of the diagnostic. 12936835568SCraig Topper CXString getSpelling() const override; 130bb2c7101STed Kremenek 1319fc8faf9SAdrian Prantl /// Return the text for the diagnostic option. 13236835568SCraig Topper CXString getDiagnosticOption(CXString *Disable) const override; 133bb2c7101STed Kremenek 1349fc8faf9SAdrian Prantl /// Return the category of the diagnostic. 13536835568SCraig Topper unsigned getCategory() const override; 136bb2c7101STed Kremenek 1379fc8faf9SAdrian Prantl /// Return the category string of the diagnostic. 13836835568SCraig Topper CXString getCategoryText() const override; 13926a6d498STed Kremenek 1409fc8faf9SAdrian Prantl /// Return the number of source ranges for the diagnostic. 14136835568SCraig Topper unsigned getNumRanges() const override; 142bb2c7101STed Kremenek 1439fc8faf9SAdrian Prantl /// Return the source ranges for the diagnostic. 14436835568SCraig Topper CXSourceRange getRange(unsigned Range) const override; 145bb2c7101STed Kremenek 1469fc8faf9SAdrian Prantl /// Return the number of FixIts. 14736835568SCraig Topper unsigned getNumFixIts() const override; 148bb2c7101STed Kremenek 1499fc8faf9SAdrian Prantl /// Return the FixIt information (source range and inserted text). 15036835568SCraig Topper CXString getFixIt(unsigned FixIt, 15136835568SCraig Topper CXSourceRange *ReplacementRange) const override; 152bb2c7101STed Kremenek classofCXStoredDiagnostic153bb2c7101STed Kremenek static bool classof(const CXDiagnosticImpl *D) { 154bb2c7101STed Kremenek return D->getKind() == StoredDiagnosticKind; 155bb2c7101STed Kremenek } 156d5a2a073SDaniel Dunbar }; 157d5a2a073SDaniel Dunbar 158f2d99b0dSArgyrios Kyrtzidis namespace cxdiag { 159f2d99b0dSArgyrios Kyrtzidis CXDiagnosticSetImpl *lazyCreateDiags(CXTranslationUnit TU, 160f2d99b0dSArgyrios Kyrtzidis bool checkIfChanged = false); 161f2d99b0dSArgyrios Kyrtzidis } // end namespace cxdiag 162f2d99b0dSArgyrios Kyrtzidis 163d5a2a073SDaniel Dunbar } // end namespace clang 164d5a2a073SDaniel Dunbar 1652f5db8b3SBenjamin Kramer #endif 166