1d93256e55673a17d18543397ec462416acb13792Douglas Gregor/*===-- CIndexDiagnostic.h - Diagnostics C Interface ------------*- C++ -*-===*\
25352ac06d8f6194825bb2a99ffa009b61bafb503Douglas Gregor|*                                                                            *|
35352ac06d8f6194825bb2a99ffa009b61bafb503Douglas Gregor|*                     The LLVM Compiler Infrastructure                       *|
45352ac06d8f6194825bb2a99ffa009b61bafb503Douglas Gregor|*                                                                            *|
55352ac06d8f6194825bb2a99ffa009b61bafb503Douglas Gregor|* This file is distributed under the University of Illinois Open Source      *|
65352ac06d8f6194825bb2a99ffa009b61bafb503Douglas Gregor|* License. See LICENSE.TXT for details.                                      *|
75352ac06d8f6194825bb2a99ffa009b61bafb503Douglas Gregor|*                                                                            *|
85352ac06d8f6194825bb2a99ffa009b61bafb503Douglas Gregor|*===----------------------------------------------------------------------===*|
95352ac06d8f6194825bb2a99ffa009b61bafb503Douglas Gregor|*                                                                            *|
105352ac06d8f6194825bb2a99ffa009b61bafb503Douglas Gregor|* Implements the diagnostic functions of the Clang C interface.              *|
115352ac06d8f6194825bb2a99ffa009b61bafb503Douglas Gregor|*                                                                            *|
125352ac06d8f6194825bb2a99ffa009b61bafb503Douglas Gregor\*===----------------------------------------------------------------------===*/
135352ac06d8f6194825bb2a99ffa009b61bafb503Douglas Gregor#ifndef LLVM_CLANG_CINDEX_DIAGNOSTIC_H
145352ac06d8f6194825bb2a99ffa009b61bafb503Douglas Gregor#define LLVM_CLANG_CINDEX_DIAGNOSTIC_H
155352ac06d8f6194825bb2a99ffa009b61bafb503Douglas Gregor
161edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek#include "clang-c/Index.h"
17153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek#include <vector>
18153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek#include <assert.h>
191edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek
205352ac06d8f6194825bb2a99ffa009b61bafb503Douglas Gregornamespace clang {
215352ac06d8f6194825bb2a99ffa009b61bafb503Douglas Gregor
2235b8440f615a2336f2819a98a78fc89f07d905e1Daniel Dunbarclass LangOptions;
23b846debc1b22a37228efe4aa87b34482d15b6a3cBenjamin Kramerclass StoredDiagnostic;
24153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenekclass CXDiagnosticImpl;
25153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek
26153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenekclass CXDiagnosticSetImpl {
27153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek  std::vector<CXDiagnosticImpl *> Diagnostics;
28153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek  const bool IsExternallyManaged;
29153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenekpublic:
30153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek  CXDiagnosticSetImpl(bool isManaged = false)
31153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek    : IsExternallyManaged(isManaged) {}
32153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek
33153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek  virtual ~CXDiagnosticSetImpl();
34153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek
35153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek  size_t getNumDiagnostics() const {
36153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek    return Diagnostics.size();
37153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek  }
38153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek
39153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek  CXDiagnosticImpl *getDiagnostic(unsigned i) const {
40153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek    assert(i < getNumDiagnostics());
41153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek    return Diagnostics[i];
42153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek  }
43153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek
44153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek  void appendDiagnostic(CXDiagnosticImpl *D) {
45153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek    Diagnostics.push_back(D);
46153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek  }
47153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek
48153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek  bool empty() const {
49153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek    return Diagnostics.empty();
50153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek  }
51153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek
52153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek  bool isExternallyManaged() const { return IsExternallyManaged; }
53153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek};
544914612675787cda11ad6b31735e130a81a1d7a1Daniel Dunbar
551edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenekclass CXDiagnosticImpl {
561edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenekpublic:
577473b1c6e7ba2654d4a0d469198f0e01b485b51aTed Kremenek  enum Kind { StoredDiagnosticKind, LoadedDiagnosticKind,
587473b1c6e7ba2654d4a0d469198f0e01b485b51aTed Kremenek              CustomNoteDiagnosticKind };
591edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek
601edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek  virtual ~CXDiagnosticImpl();
611edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek
621edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek  /// \brief Return the severity of the diagnostic.
631edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek  virtual CXDiagnosticSeverity getSeverity() const = 0;
641edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek
651edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek  /// \brief Return the location of the diagnostic.
661edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek  virtual CXSourceLocation getLocation() const = 0;
671edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek
681edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek  /// \brief Return the spelling of the diagnostic.
691edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek  virtual CXString getSpelling() const = 0;
701edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek
711edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek  /// \brief Return the text for the diagnostic option.
721edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek  virtual CXString getDiagnosticOption(CXString *Disable) const = 0;
731edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek
741edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek  /// \brief Return the category of the diagnostic.
751edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek  virtual unsigned getCategory() const = 0;
7678d5d3bb9386a5aa31d19445eb8f81bf2652acb4Ted Kremenek
7778d5d3bb9386a5aa31d19445eb8f81bf2652acb4Ted Kremenek  /// \brief Return the category string of the diagnostic.
7878d5d3bb9386a5aa31d19445eb8f81bf2652acb4Ted Kremenek  virtual CXString getCategoryText() const = 0;
7978d5d3bb9386a5aa31d19445eb8f81bf2652acb4Ted Kremenek
801edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek  /// \brief Return the number of source ranges for the diagnostic.
811edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek  virtual unsigned getNumRanges() const = 0;
821edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek
831edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek  /// \brief Return the source ranges for the diagnostic.
841edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek  virtual CXSourceRange getRange(unsigned Range) const = 0;
851edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek
861edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek  /// \brief Return the number of FixIts.
871edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek  virtual unsigned getNumFixIts() const = 0;
881edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek
891edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek  /// \brief Return the FixIt information (source range and inserted text).
901edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek  virtual CXString getFixIt(unsigned FixIt,
911edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek                            CXSourceRange *ReplacementRange) const = 0;
921edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek
931edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek  Kind getKind() const { return K; }
94153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek
95153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek  CXDiagnosticSetImpl &getChildDiagnostics() {
96153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek    return ChildDiags;
97153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek  }
98153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek
991edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenekprotected:
1001edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek  CXDiagnosticImpl(Kind k) : K(k) {}
101153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek  CXDiagnosticSetImpl ChildDiags;
102153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek
103153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek  void append(CXDiagnosticImpl *D) {
104153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek    ChildDiags.appendDiagnostic(D);
105153221717e39ce41323d5bc6b8b8bf130923c1bdTed Kremenek  }
1061edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek
1071edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenekprivate:
1081edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek  Kind K;
1091edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek};
1101edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek
111a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor/// \brief The storage behind a CXDiagnostic
1121edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenekstruct CXStoredDiagnostic : public CXDiagnosticImpl {
113a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  const StoredDiagnostic &Diag;
114a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  const LangOptions &LangOpts;
115a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor
116a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor  CXStoredDiagnostic(const StoredDiagnostic &Diag,
117a88084b78fd4ca5d3d858c14b02414f8cc399f02Douglas Gregor                     const LangOptions &LangOpts)
1181edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek    : CXDiagnosticImpl(StoredDiagnosticKind),
1191edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek      Diag(Diag), LangOpts(LangOpts) { }
1201edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek
1211edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek  virtual ~CXStoredDiagnostic() {}
1221edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek
1231edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek  /// \brief Return the severity of the diagnostic.
124651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  CXDiagnosticSeverity getSeverity() const override;
125651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
1261edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek  /// \brief Return the location of the diagnostic.
127651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  CXSourceLocation getLocation() const override;
1281edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek
1291edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek  /// \brief Return the spelling of the diagnostic.
130651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  CXString getSpelling() const override;
1311edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek
1321edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek  /// \brief Return the text for the diagnostic option.
133651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  CXString getDiagnosticOption(CXString *Disable) const override;
134651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
1351edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek  /// \brief Return the category of the diagnostic.
136651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  unsigned getCategory() const override;
137651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
13878d5d3bb9386a5aa31d19445eb8f81bf2652acb4Ted Kremenek  /// \brief Return the category string of the diagnostic.
139651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  CXString getCategoryText() const override;
14078d5d3bb9386a5aa31d19445eb8f81bf2652acb4Ted Kremenek
1411edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek  /// \brief Return the number of source ranges for the diagnostic.
142651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  unsigned getNumRanges() const override;
143651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines
1441edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek  /// \brief Return the source ranges for the diagnostic.
145651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  CXSourceRange getRange(unsigned Range) const override;
1461edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek
1471edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek  /// \brief Return the number of FixIts.
148651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  unsigned getNumFixIts() const override;
1491edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek
1501edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek  /// \brief Return the FixIt information (source range and inserted text).
151651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines  CXString getFixIt(unsigned FixIt,
152651f13cea278ec967336033dd032faef0e9fc2ecStephen Hines                    CXSourceRange *ReplacementRange) const override;
1531edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek
1541edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek  static bool classof(const CXDiagnosticImpl *D) {
1551edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek    return D->getKind() == StoredDiagnosticKind;
1561edabbc32a07654d9b9fe44f50533ff146abd03eTed Kremenek  }
1575352ac06d8f6194825bb2a99ffa009b61bafb503Douglas Gregor};
158d93256e55673a17d18543397ec462416acb13792Douglas Gregor
159996e6e564af7483e2d5e0b70df5fdb9f79ec4b5aArgyrios Kyrtzidisnamespace cxdiag {
160996e6e564af7483e2d5e0b70df5fdb9f79ec4b5aArgyrios KyrtzidisCXDiagnosticSetImpl *lazyCreateDiags(CXTranslationUnit TU,
161996e6e564af7483e2d5e0b70df5fdb9f79ec4b5aArgyrios Kyrtzidis                                     bool checkIfChanged = false);
162996e6e564af7483e2d5e0b70df5fdb9f79ec4b5aArgyrios Kyrtzidis} // end namespace cxdiag
163996e6e564af7483e2d5e0b70df5fdb9f79ec4b5aArgyrios Kyrtzidis
1645352ac06d8f6194825bb2a99ffa009b61bafb503Douglas Gregor} // end namespace clang
1655352ac06d8f6194825bb2a99ffa009b61bafb503Douglas Gregor
1665352ac06d8f6194825bb2a99ffa009b61bafb503Douglas Gregor#endif // LLVM_CLANG_CINDEX_DIAGNOSTIC_H
167