Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[analyzer] Allow registering custom statically-linked analyzer checkers
Summary: Add an extension point to allow registration of statically-linked Clang Static Analyzer checkers that are not a part of the Clang tree. This extension point employs the mechanism used when checkers are registered from dynamically loaded plugins. Reviewers: george.karpenkov, NoQ, xazax.hun, dcoughlin Reviewed By: george.karpenkov Subscribers: mgorny, mikhail.ramalho, rnkovacs, xazax.hun, szepet, a.sidorin, cfe-commits Differential Revision: https://reviews.llvm.org/D45718 llvm-svn: 335740
- Loading branch information
Showing
7 changed files
with
125 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
clang/unittests/StaticAnalyzer/RegisterCustomCheckersTest.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
//===- unittests/StaticAnalyzer/RegisterCustomCheckersTest.cpp ------------===// | ||
// | ||
// The LLVM Compiler Infrastructure | ||
// | ||
// This file is distributed under the University of Illinois Open Source | ||
// License. See LICENSE.TXT for details. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "clang/Frontend/CompilerInstance.h" | ||
#include "clang/StaticAnalyzer/Core/BugReporter/BugReporter.h" | ||
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" | ||
#include "clang/StaticAnalyzer/Core/Checker.h" | ||
#include "clang/StaticAnalyzer/Core/CheckerRegistry.h" | ||
#include "clang/StaticAnalyzer/Core/PathSensitive/AnalysisManager.h" | ||
#include "clang/StaticAnalyzer/Frontend/AnalysisConsumer.h" | ||
#include "clang/Tooling/Tooling.h" | ||
#include "gtest/gtest.h" | ||
|
||
namespace clang { | ||
namespace ento { | ||
namespace { | ||
|
||
class CustomChecker : public Checker<check::ASTCodeBody> { | ||
public: | ||
void checkASTCodeBody(const Decl *D, AnalysisManager &Mgr, | ||
BugReporter &BR) const { | ||
BR.EmitBasicReport(D, this, "Custom diagnostic", categories::LogicError, | ||
"Custom diagnostic description", | ||
PathDiagnosticLocation(D, Mgr.getSourceManager()), {}); | ||
} | ||
}; | ||
|
||
class TestAction : public ASTFrontendAction { | ||
class DiagConsumer : public PathDiagnosticConsumer { | ||
llvm::raw_ostream &Output; | ||
|
||
public: | ||
DiagConsumer(llvm::raw_ostream &Output) : Output(Output) {} | ||
void FlushDiagnosticsImpl(std::vector<const PathDiagnostic *> &Diags, | ||
FilesMade *filesMade) override { | ||
for (const auto *PD : Diags) | ||
Output << PD->getCheckName() << ":" << PD->getShortDescription(); | ||
} | ||
|
||
StringRef getName() const override { return "Test"; } | ||
}; | ||
|
||
llvm::raw_ostream &DiagsOutput; | ||
|
||
public: | ||
TestAction(llvm::raw_ostream &DiagsOutput) : DiagsOutput(DiagsOutput) {} | ||
|
||
std::unique_ptr<ASTConsumer> CreateASTConsumer(CompilerInstance &Compiler, | ||
StringRef File) override { | ||
std::unique_ptr<AnalysisASTConsumer> AnalysisConsumer = | ||
CreateAnalysisConsumer(Compiler); | ||
AnalysisConsumer->AddDiagnosticConsumer(new DiagConsumer(DiagsOutput)); | ||
Compiler.getAnalyzerOpts()->CheckersControlList = { | ||
{"custom.CustomChecker", true}}; | ||
AnalysisConsumer->AddCheckerRegistrationFn([](CheckerRegistry &Registry) { | ||
Registry.addChecker<CustomChecker>("custom.CustomChecker", "Description"); | ||
}); | ||
return AnalysisConsumer; | ||
} | ||
}; | ||
|
||
|
||
TEST(RegisterCustomCheckers, RegisterChecker) { | ||
std::string Diags; | ||
{ | ||
llvm::raw_string_ostream OS(Diags); | ||
EXPECT_TRUE(tooling::runToolOnCode(new TestAction(OS), "void f() {;}")); | ||
} | ||
EXPECT_EQ(Diags, "custom.CustomChecker:Custom diagnostic description"); | ||
} | ||
|
||
} | ||
} | ||
} |