Skip to content

Commit 3c1f490

Browse files
committed
[clang-tidy] Untangle layering in ClangTidyDiagnosticConsumer somewhat. NFC
Summary: Clang's hierarchy is CompilerInstance -> DiagnosticsEngine -> DiagnosticConsumer. (Ownership is optional/shared, but this structure is fairly clear). Currently ClangTidyDiagnosticConsumer *owns* the DiagnosticsEngine: - this inverts the hierarchy, which is confusing - this means ClangTidyDiagnosticConsumer() mutates the passed-in context, which is both surprising and limits flexibility - it's not possible to use a different DiagnosticsEngine with ClangTidy This means a little bit more code in the places ClangTidy is used standalone, but more flexibility in using ClangTidy with other diagnostics configurations. Reviewers: hokein Subscribers: xazax.hun, cfe-commits Differential Revision: https://reviews.llvm.org/D54033 llvm-svn: 346418
1 parent 08b64d6 commit 3c1f490

File tree

5 files changed

+24
-21
lines changed

5 files changed

+24
-21
lines changed

clang-tools-extra/clang-tidy/ClangTidy.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -551,7 +551,9 @@ runClangTidy(clang::tidy::ClangTidyContext &Context,
551551
Context.setProfileStoragePrefix(StoreCheckProfile);
552552

553553
ClangTidyDiagnosticConsumer DiagConsumer(Context);
554-
554+
DiagnosticsEngine DE(new DiagnosticIDs(), new DiagnosticOptions(),
555+
&DiagConsumer, /*ShouldOwnClient=*/false);
556+
Context.setDiagnosticsEngine(&DE);
555557
Tool.setDiagnosticConsumer(&DiagConsumer);
556558

557559
class ActionFactory : public FrontendActionFactory {

clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.cpp

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -267,13 +267,7 @@ ClangTidyDiagnosticConsumer::ClangTidyDiagnosticConsumer(
267267
ClangTidyContext &Ctx, bool RemoveIncompatibleErrors)
268268
: Context(Ctx), RemoveIncompatibleErrors(RemoveIncompatibleErrors),
269269
LastErrorRelatesToUserCode(false), LastErrorPassesLineFilter(false),
270-
LastErrorWasIgnored(false) {
271-
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
272-
Diags = llvm::make_unique<DiagnosticsEngine>(
273-
IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), &*DiagOpts, this,
274-
/*ShouldOwnClient=*/false);
275-
Context.DiagEngine = Diags.get();
276-
}
270+
LastErrorWasIgnored(false) {}
277271

278272
void ClangTidyDiagnosticConsumer::finalizeLastError() {
279273
if (!Errors.empty()) {
@@ -391,7 +385,7 @@ void ClangTidyDiagnosticConsumer::HandleDiagnostic(
391385

392386
if (Info.getLocation().isValid() && DiagLevel != DiagnosticsEngine::Error &&
393387
DiagLevel != DiagnosticsEngine::Fatal &&
394-
LineIsMarkedWithNOLINTinMacro(Diags->getSourceManager(),
388+
LineIsMarkedWithNOLINTinMacro(Info.getSourceManager(),
395389
Info.getLocation(), Info.getID(),
396390
Context)) {
397391
++Context.Stats.ErrorsIgnoredNOLINT;
@@ -453,14 +447,14 @@ void ClangTidyDiagnosticConsumer::HandleDiagnostic(
453447
Errors.back());
454448
SmallString<100> Message;
455449
Info.FormatDiagnostic(Message);
456-
FullSourceLoc Loc =
457-
(Info.getLocation().isInvalid())
458-
? FullSourceLoc()
459-
: FullSourceLoc(Info.getLocation(), Info.getSourceManager());
450+
FullSourceLoc Loc;
451+
if (Info.getLocation().isValid() && Info.hasSourceManager())
452+
Loc = FullSourceLoc(Info.getLocation(), Info.getSourceManager());
460453
Converter.emitDiagnostic(Loc, DiagLevel, Message, Info.getRanges(),
461454
Info.getFixItHints());
462455

463-
checkFilters(Info.getLocation());
456+
if (Info.hasSourceManager())
457+
checkFilters(Info.getLocation(), Info.getSourceManager());
464458
}
465459

466460
bool ClangTidyDiagnosticConsumer::passesLineFilter(StringRef FileName,
@@ -481,15 +475,15 @@ bool ClangTidyDiagnosticConsumer::passesLineFilter(StringRef FileName,
481475
return false;
482476
}
483477

484-
void ClangTidyDiagnosticConsumer::checkFilters(SourceLocation Location) {
478+
void ClangTidyDiagnosticConsumer::checkFilters(SourceLocation Location,
479+
const SourceManager &Sources) {
485480
// Invalid location may mean a diagnostic in a command line, don't skip these.
486481
if (!Location.isValid()) {
487482
LastErrorRelatesToUserCode = true;
488483
LastErrorPassesLineFilter = true;
489484
return;
490485
}
491486

492-
const SourceManager &Sources = Diags->getSourceManager();
493487
if (!*Context.getOptions().SystemHeaders &&
494488
Sources.isInSystemHeader(Location))
495489
return;

clang-tools-extra/clang-tidy/ClangTidyDiagnosticConsumer.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,12 @@ class ClangTidyContext {
102102
/// \brief Initializes \c ClangTidyContext instance.
103103
ClangTidyContext(std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider,
104104
bool AllowEnablingAnalyzerAlphaCheckers = false);
105+
/// Sets the DiagnosticsEngine that diag() will emit diagnostics to.
106+
// FIXME: this is required initialization, and should be a constructor param.
107+
// Fix the context -> diag engine -> consumer -> context initialization cycle.
108+
void setDiagnosticsEngine(DiagnosticsEngine *DiagEngine) {
109+
this->DiagEngine = DiagEngine;
110+
}
105111

106112
~ClangTidyContext();
107113

@@ -186,9 +192,8 @@ class ClangTidyContext {
186192
}
187193

188194
private:
189-
// Sets DiagEngine and Errors.
195+
// Writes to Stats.
190196
friend class ClangTidyDiagnosticConsumer;
191-
friend class ClangTidyPluginAction;
192197

193198
DiagnosticsEngine *DiagEngine;
194199
std::unique_ptr<ClangTidyOptionsProvider> OptionsProvider;
@@ -242,12 +247,11 @@ class ClangTidyDiagnosticConsumer : public DiagnosticConsumer {
242247

243248
/// \brief Updates \c LastErrorRelatesToUserCode and LastErrorPassesLineFilter
244249
/// according to the diagnostic \p Location.
245-
void checkFilters(SourceLocation Location);
250+
void checkFilters(SourceLocation Location, const SourceManager& Sources);
246251
bool passesLineFilter(StringRef FileName, unsigned LineNumber) const;
247252

248253
ClangTidyContext &Context;
249254
bool RemoveIncompatibleErrors;
250-
std::unique_ptr<DiagnosticsEngine> Diags;
251255
std::vector<ClangTidyError> Errors;
252256
std::unique_ptr<llvm::Regex> HeaderFilter;
253257
bool LastErrorRelatesToUserCode;

clang-tools-extra/clang-tidy/plugin/ClangTidyPlugin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class ClangTidyPluginAction : public PluginASTAction {
3535
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &Compiler,
3636
StringRef File) override {
3737
// Insert the current diagnostics engine.
38-
Context->DiagEngine = &Compiler.getDiagnostics();
38+
Context->setDiagnosticsEngine(&Compiler.getDiagnostics());
3939

4040
// Create the AST consumer.
4141
ClangTidyASTConsumerFactory Factory(*Context);

clang-tools-extra/unittests/clang-tidy/ClangTidyTest.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ runCheckOnCode(StringRef Code, std::vector<ClangTidyError> *Errors = nullptr,
8383
ClangTidyContext Context(llvm::make_unique<DefaultOptionsProvider>(
8484
ClangTidyGlobalOptions(), Options));
8585
ClangTidyDiagnosticConsumer DiagConsumer(Context);
86+
DiagnosticsEngine DE(new DiagnosticIDs(), new DiagnosticOptions,
87+
&DiagConsumer, false);
88+
Context.setDiagnosticsEngine(&DE);
8689

8790
std::vector<std::string> Args(1, "clang-tidy");
8891
Args.push_back("-fsyntax-only");

0 commit comments

Comments
 (0)