Reflection for RapidJSON 0.0.17
Reflection for serializing/deserializing with RapidJSON
Loading...
Searching...
No Matches
consumer.cpp
Go to the documentation of this file.
1#include "./consumer.h"
2#include "./codefactory.h"
3
4#include <clang/AST/ASTContext.h>
5#include <clang/AST/DeclCXX.h>
6#include <clang/AST/DeclGroup.h>
7#include <clang/AST/DeclTemplate.h>
8#include <clang/Basic/MacroBuilder.h>
9#include <clang/Sema/Sema.h>
10
11using namespace std;
12
13namespace ReflectiveRapidJSON {
14
15bool Consumer::shouldParseDecl(clang::Decl *declaration)
16{
17 const clang::SourceManager &sourceManager = m_compilerInstance.getSourceManager();
18 return sourceManager.getFileID(sourceManager.getExpansionLoc(declaration->getSourceRange().getBegin())) == sourceManager.getMainFileID();
19}
20
21bool Consumer::HandleTopLevelDecl(clang::DeclGroupRef groupRefDecl)
22{
23 for (clang::Decl *decl : groupRefDecl) {
24 if (clang::NamespaceDecl *namespaceDecl = llvm::dyn_cast<clang::NamespaceDecl>(decl)) {
25 if (!shouldParseDecl(namespaceDecl)) {
26 continue;
27 }
28 }
29 }
30 return clang::ASTConsumer::HandleTopLevelDecl(groupRefDecl);
31}
32
33void Consumer::HandleTranslationUnit(clang::ASTContext &context)
34{
35 m_visitor.TraverseDecl(context.getTranslationUnitDecl());
36 m_factory.generate();
37}
38
39void DiagConsumer::BeginSourceFile(const clang::LangOptions &langOpts, const clang::Preprocessor *pp)
40{
41 m_proxy->BeginSourceFile(langOpts, pp);
42}
43
45{
46 m_proxy->clear();
47}
48
50{
51 m_proxy->EndSourceFile();
52}
53
54#if CLANG_VERSION_MAJOR <= 22
56{
57 m_proxy->finish();
58}
59#endif
60
64void DiagConsumer::HandleDiagnostic(clang::DiagnosticsEngine::Level diagLevel, const clang::Diagnostic &info)
65{
66 bool shouldReset = false;
67 if (m_errorResilient) {
68 const auto diagId = info.getID();
69 const auto category = info.getDiags()->getDiagnosticIDs()->getCategoryNumberForDiag(diagId);
70
71 if (diagLevel >= clang::DiagnosticsEngine::Error) {
72 if (category == 2 /* 2 means "Semantic Issue" */) {
73 if (!m_realErrorCount) {
74 shouldReset = true;
75 }
76 diagLevel = clang::DiagnosticsEngine::Warning;
77 } else {
78 ++m_realErrorCount;
79 }
80 }
81 }
82
83 DiagnosticConsumer::HandleDiagnostic(diagLevel, info);
84 m_proxy->HandleDiagnostic(diagLevel, info);
85
86 if (shouldReset) {
87 // FIXME: is there another way to ignore errors?
88 const_cast<clang::DiagnosticsEngine *>(info.getDiags())->Reset();
89 }
90}
91
92} // namespace ReflectiveRapidJSON
void HandleTranslationUnit(clang::ASTContext &context) override
Definition consumer.cpp:33
bool shouldParseDecl(clang::Decl *declaration)
Definition consumer.cpp:15
bool HandleTopLevelDecl(clang::DeclGroupRef groupRefDecl) override
Definition consumer.cpp:21
void BeginSourceFile(const clang::LangOptions &langOpts, const clang::Preprocessor *pp=nullptr) override
Definition consumer.cpp:39
void HandleDiagnostic(clang::DiagnosticsEngine::Level diagLevel, const clang::Diagnostic &info) override
Turns most errors into warnings so the code generator can even work when parsing incomplete headers.
Definition consumer.cpp:64