Debian Codegraph @

src/ClazyContext.cpp

raw
1 /*
2 SPDX-FileCopyrightText: 2017 Sergio Martins <[email protected]>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7 #include "ClazyContext.h"
8 #include "AccessSpecifierManager.h"
9 #include "FixItExporter.h"
10 #include "PreProcessorVisitor.h"
11 #include "checkmanager.h"
12
13 #include <clang/AST/ParentMap.h>
14 #include <clang/Frontend/CompilerInstance.h>
15 #include <clang/Lex/PreprocessorOptions.h>
16 #include <clang/Rewrite/Frontend/FixItRewriter.h>
17 #include <llvm/Support/Regex.h>
18
19 #include <cstdlib>
20
21 using namespace clang;
22
23 ClazyContext::ClazyContext(clang::ASTContext *context,
24 clang::SourceManager &manager,
25 const clang::LangOptions &lo,
26 const clang::PreprocessorOptions &pp,
27 const std::string &headerFilter,
28 const std::string &ignoreDirs,
29 std::string exportFixesFilename,
30 const std::vector<std::string> &translationUnitPaths,
31 ClazyOptions opts,
32 std::optional<WarningReporter> warningReporter,
33 bool isClangTidy)
34 : astContext(context)
35 , sm(manager)
36 , lo(lo)
37 , m_noWerror(getenv("CLAZY_NO_WERROR") != nullptr) // Allows user to make clazy ignore -Werror
38 , m_checksPromotedToErrors(CheckManager::instance()->checksAsErrors())
39 , options(opts)
40 , extraOptions(clazy::splitString(getenv("CLAZY_EXTRA_OPTIONS"), ','))
41 , m_translationUnitPaths(translationUnitPaths)
42 , m_pp(pp)
43 , p_warningReporter(warningReporter.value_or([this](std::string,
44 const clang::SourceLocation &loc,
45 clang::DiagnosticIDs::Level level,
46 std::string error,
47 const std::vector<clang::FixItHint> &fixits) {
48 auto &engine = astContext->getDiagnostics();
49 unsigned id = engine.getDiagnosticIDs()->getCustomDiagID(level, error.c_str());
50 DiagnosticBuilder B = engine.Report(loc, id);
51 for (const FixItHint &fixit : fixits) {
52 if (!fixit.isNull()) {
53 B.AddFixItHint(fixit);
54 }
55 }
56 }))
57 , m_isClangTidy(isClangTidy)
58 {
59 if (!headerFilter.empty()) {
60 headerFilterRegex = std::make_unique<llvm::Regex>(headerFilter);
61 }
62
63 if (!ignoreDirs.empty()) {
64 ignoreDirsRegex = std::make_unique<llvm::Regex>(ignoreDirs);
65 }
66
67 if (exportFixesEnabled() && context) {
68 if (exportFixesFilename.empty()) {
69 // Only clazy-standalone sets the filename by argument.
70 // clazy plugin sets it automatically here:
71 const auto fileEntry = sm.getFileEntryRefForID(sm.getMainFileID());
72 exportFixesFilename = fileEntry->getName().str() + ".clazy.yaml";
73 }
74
75 const bool isClazyStandalone = !translationUnitPaths.empty();
76 exporter = new FixItExporter(context->getDiagnostics(), sm, context->getLangOpts(), exportFixesFilename, isClazyStandalone);
77 }
78 }
79
80 ClazyContext::~ClazyContext()
81 {
82 // delete preprocessorVisitor; // we don't own it
83 delete accessSpecifierManager;
84 delete parentMap;
85
86 static unsigned long count = 0;
87 count++;
88
89 if (exporter) {
90 // With clazy-standalone we use the same YAML file for all translation-units, so only
91 // write out the last one. With clazy-plugin there's a YAML file per translation unit.
92 const bool isClazyPlugin = m_translationUnitPaths.empty();
93 const bool isLast = count == m_translationUnitPaths.size();
94 if (isLast || isClazyPlugin) {
95 exporter->Export();
96 }
97 delete exporter;
98 }
99
100 preprocessorVisitor = nullptr;
101 accessSpecifierManager = nullptr;
102 parentMap = nullptr;
103 }
104
105 void ClazyContext::registerPreprocessorCallbacks(clang::Preprocessor &pp)
106 {
107 if (!usingPreCompiledHeaders()) {
108 accessSpecifierManager = new AccessSpecifierManager(pp, exportFixesEnabled());
109 preprocessorVisitor = new PreProcessorVisitor(pp);
110 }
111 }
112
113 bool ClazyContext::isQt() const
114 {
115 static const bool s_isQt = [this] {
116 for (const auto &s : m_pp.Macros) {
117 if (s.first == "QT_CORE_LIB") {
118 return true;
119 }
120 }
121 return false;
122 }();
123
124 return s_isQt;
125 }
126 std::string ClazyContext::qtNamespace() const
127 {
128 return accessSpecifierManager ? accessSpecifierManager->qtNamespace() : "";
129 }
130 bool ClazyContext::usingPreCompiledHeaders() const
131 {
132 return !m_pp.ImplicitPCHInclude.empty();
133 }