Skip to content

Commit cd3f147

Browse files
author
Kristof Umann
committedApr 19, 2019
[analyzer] Fix an assertion failure if plugins added dependencies
Ideally, there is no reason behind not being able to depend on checkers that come from a different plugin (or on builtin checkers) -- however, this is only possible if all checkers are added to the registry before resolving checker dependencies. Since I used a binary search in my addDependency method, this also resulted in an assertion failure (due to CheckerRegistry::Checkers not being sorted), since the function used by plugins to register their checkers (clang_registerCheckers) calls addDependency. This patch resolves this issue by only noting which dependencies have to established when addDependency is called, and resolves them at a later stage when no more checkers are added to the registry, by which point CheckerRegistry::Checkers is already sorted. Differential Revision: https://reviews.llvm.org/D59461 llvm-svn: 358750
1 parent 99f641c commit cd3f147

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed
 

Diff for: ‎clang/include/clang/StaticAnalyzer/Frontend/CheckerRegistry.h

+6
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,12 @@ class CheckerRegistry {
195195
CheckerInfoList Checkers;
196196
llvm::StringMap<size_t> PackageSizes;
197197

198+
/// Contains all (Dependendent checker, Dependency) pairs. We need this, as
199+
/// we'll resolve dependencies after all checkers were added first.
200+
llvm::SmallVector<std::pair<StringRef, StringRef>, 0> Dependencies;
201+
202+
void resolveDependencies();
203+
198204
DiagnosticsEngine &Diags;
199205
AnalyzerOptions &AnOpts;
200206
const LangOptions &LangOpts;

Diff for: ‎clang/lib/StaticAnalyzer/Frontend/CheckerRegistry.cpp

+20-10
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ CheckerRegistry::CheckerRegistry(
177177
#undef CHECKER_DEPENDENCY
178178
#undef GET_CHECKER_DEPENDENCIES
179179

180+
resolveDependencies();
181+
180182
// Parse '-analyzer-checker' and '-analyzer-disable-checker' options from the
181183
// command line.
182184
for (const std::pair<std::string, bool> &Opt : AnOpts.CheckersControlList) {
@@ -278,18 +280,26 @@ void CheckerRegistry::addChecker(InitializationFunction Rfn,
278280
}
279281
}
280282

281-
void CheckerRegistry::addDependency(StringRef FullName, StringRef Dependency) {
282-
auto CheckerIt = binaryFind(Checkers, FullName);
283-
assert(CheckerIt != Checkers.end() && CheckerIt->FullName == FullName &&
284-
"Failed to find the checker while attempting to set up its "
285-
"dependencies!");
283+
void CheckerRegistry::resolveDependencies() {
284+
for (const std::pair<StringRef, StringRef> &Entry : Dependencies) {
285+
auto CheckerIt = binaryFind(Checkers, Entry.first);
286+
assert(CheckerIt != Checkers.end() && CheckerIt->FullName == Entry.first &&
287+
"Failed to find the checker while attempting to set up its "
288+
"dependencies!");
286289

287-
auto DependencyIt = binaryFind(Checkers, Dependency);
288-
assert(DependencyIt != Checkers.end() &&
289-
DependencyIt->FullName == Dependency &&
290-
"Failed to find the dependency of a checker!");
290+
auto DependencyIt = binaryFind(Checkers, Entry.second);
291+
assert(DependencyIt != Checkers.end() &&
292+
DependencyIt->FullName == Entry.second &&
293+
"Failed to find the dependency of a checker!");
294+
295+
CheckerIt->Dependencies.emplace_back(&*DependencyIt);
296+
}
291297

292-
CheckerIt->Dependencies.emplace_back(&*DependencyIt);
298+
Dependencies.clear();
299+
}
300+
301+
void CheckerRegistry::addDependency(StringRef FullName, StringRef Dependency) {
302+
Dependencies.emplace_back(FullName, Dependency);
293303
}
294304

295305
void CheckerRegistry::initializeManager(CheckerManager &CheckerMgr) const {

Diff for: ‎clang/test/Analysis/checker-dependencies.c

+17
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,20 @@
11
// RUN: %clang_analyze_cc1 %s \
22
// RUN: -analyzer-checker=core \
33
// RUN: -analyzer-checker=nullability.NullReturnedFromNonnull
4+
5+
// RUN: %clang_analyze_cc1 %s \
6+
// RUN: -analyzer-checker=osx.cocoa.RetainCount \
7+
// RUN: -analyzer-list-enabled-checkers \
8+
// RUN: 2>&1 | FileCheck %s -check-prefix=CHECK-IMPLICITLY-ENABLED
9+
10+
// CHECK-IMPLICITLY-ENABLED: osx.cocoa.RetainCountBase
11+
// CHECK-IMPLICITLY-ENABLED: osx.cocoa.RetainCount
12+
13+
// RUN: %clang_analyze_cc1 %s \
14+
// RUN: -analyzer-checker=osx.cocoa.RetainCount \
15+
// RUN: -analyzer-disable-checker=osx.cocoa.RetainCountBase \
16+
// RUN: -analyzer-list-enabled-checkers \
17+
// RUN: 2>&1 | FileCheck %s -check-prefix=CHECK-IMPLICITLY-DISABLED
18+
19+
// CHECK-IMPLICITLY-DISABLED-NOT: osx.cocoa.RetainCountBase
20+
// CHECK-IMPLICITLY-DISABLED-NOT: osx.cocoa.RetainCount

0 commit comments

Comments
 (0)
Please sign in to comment.