1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
|
//===--- tools/extra/clang-tidy/ClangTidy.cpp - Clang tidy tool -----------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
///
/// \file This file implements a clang-tidy tool.
///
/// This tool uses the Clang Tooling infrastructure, see
/// http://clang.llvm.org/docs/HowToSetupToolingForLLVM.html
/// for details on setting it up with LLVM source tree.
///
//===----------------------------------------------------------------------===//
#include "ClangTidy.h"
#include "ClangTidyDiagnosticConsumer.h"
#include "ClangTidyModuleRegistry.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
#include "clang/Lex/PPCallbacks.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Frontend/ASTConsumers.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendActions.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Rewrite/Frontend/FixItRewriter.h"
#include "clang/Rewrite/Frontend/FrontendActions.h"
#include "clang/Tooling/Tooling.h"
#include "clang/Tooling/Refactoring.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/Signals.h"
#include <vector>
using namespace clang::ast_matchers;
using namespace clang::driver;
using namespace clang::tooling;
using namespace llvm;
namespace clang {
namespace tidy {
namespace {
class ClangTidyPPAction : public PreprocessOnlyAction {
public:
ClangTidyPPAction(SmallVectorImpl<ClangTidyCheck *> &Checks,
ClangTidyContext &Context)
: Checks(Checks), Context(Context) {}
private:
virtual bool BeginSourceFileAction(CompilerInstance &Compiler,
llvm::StringRef file_name) {
Context.setSourceManager(&Compiler.getSourceManager());
for (SmallVectorImpl<ClangTidyCheck *>::iterator I = Checks.begin(),
E = Checks.end();
I != E; ++I)
(*I)->registerPPCallbacks(Compiler);
return true;
}
SmallVectorImpl<ClangTidyCheck *> &Checks;
ClangTidyContext &Context;
};
class ClangTidyPPActionFactory : public FrontendActionFactory {
public:
ClangTidyPPActionFactory(SmallVectorImpl<ClangTidyCheck *> &Checks,
ClangTidyContext &Context)
: Checks(Checks), Context(Context) {}
virtual FrontendAction *create() {
return new ClangTidyPPAction(Checks, Context);
}
private:
SmallVectorImpl<ClangTidyCheck *> &Checks;
ClangTidyContext &Context;
};
} // namespace
ClangTidyError::ClangTidyError(const SourceManager &Sources, SourceLocation Loc,
StringRef Message,
const tooling::Replacements &Fix)
: Message(Message), Fix(Fix) {
FilePath = Sources.getFilename(Loc);
FileOffset = Sources.getFileOffset(Loc);
}
DiagnosticBuilder ClangTidyContext::Diag(SourceLocation Loc,
StringRef Message) {
return DiagEngine->Report(
Loc, DiagEngine->getCustomDiagID(DiagnosticsEngine::Warning, Message));
}
void ClangTidyContext::setDiagnosticsEngine(DiagnosticsEngine *Engine) {
DiagEngine = Engine;
}
void ClangTidyContext::setSourceManager(SourceManager *SourceMgr) {
DiagEngine->setSourceManager(SourceMgr);
}
/// \brief Store a \c ClangTidyError.
void ClangTidyContext::storeError(const ClangTidyError &Error) {
Errors->push_back(Error);
}
void ClangTidyCheck::run(const ast_matchers::MatchFinder::MatchResult &Result) {
Context->setSourceManager(Result.SourceManager);
check(Result);
}
void runClangTidy(StringRef CheckRegexString,
const tooling::CompilationDatabase &Compilations,
ArrayRef<std::string> Ranges,
SmallVectorImpl<ClangTidyError> *Errors) {
// FIXME: Ranges are currently full files. Support selecting specific
// (line-)ranges.
ClangTool Tool(Compilations, Ranges);
clang::tidy::ClangTidyContext Context(Errors);
ClangTidyDiagnosticConsumer DiagConsumer(Context);
ClangTidyCheckFactories CheckFactories;
for (ClangTidyModuleRegistry::iterator I = ClangTidyModuleRegistry::begin(),
E = ClangTidyModuleRegistry::end();
I != E; ++I) {
OwningPtr<ClangTidyModule> Module(I->instantiate());
Module->addCheckFactories(CheckFactories);
}
SmallVector<ClangTidyCheck *, 16> Checks;
CheckFactories.createChecks(CheckRegexString, Checks);
MatchFinder Finder;
for (SmallVectorImpl<ClangTidyCheck *>::iterator I = Checks.begin(),
E = Checks.end();
I != E; ++I) {
(*I)->setContext(&Context);
(*I)->registerMatchers(&Finder);
}
Tool.run(new ClangTidyPPActionFactory(Checks, Context));
Tool.run(newFrontendActionFactory(&Finder));
}
void handleErrors(SmallVectorImpl<ClangTidyError> &Errors, bool Fix) {
FileManager Files((FileSystemOptions()));
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
DiagnosticConsumer *DiagPrinter =
new TextDiagnosticPrinter(llvm::outs(), &*DiagOpts);
DiagnosticsEngine Diags(IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
&*DiagOpts, DiagPrinter);
DiagPrinter->BeginSourceFile(LangOptions());
SourceManager SourceMgr(Diags, Files);
Rewriter Rewrite(SourceMgr, LangOptions());
for (SmallVectorImpl<ClangTidyError>::iterator I = Errors.begin(),
E = Errors.end();
I != E; ++I) {
const FileEntry *File = Files.getFile(I->FilePath);
FileID ID = SourceMgr.createFileID(File, SourceLocation(), SrcMgr::C_User);
SourceLocation Loc = SourceMgr.getLocForStartOfFile(ID);
Diags.Report(Loc.getLocWithOffset(I->FileOffset),
Diags.getCustomDiagID(DiagnosticsEngine::Warning, I->Message));
tooling::applyAllReplacements(I->Fix, Rewrite);
}
// FIXME: Run clang-format on changes.
if (Fix)
Rewrite.overwriteChangedFiles();
}
// This anchor is used to force the linker to link the LLVMModule.
extern volatile int LLVMModuleAnchorSource;
static int LLVMModuleAnchorDestination = LLVMModuleAnchorSource;
// This anchor is used to force the linker to link the GoogleModule.
extern volatile int GoogleModuleAnchorSource;
static int GoogleModuleAnchorDestination = GoogleModuleAnchorSource;
} // namespace tidy
} // namespace clang
|