Skip to content

Commit 72d04d7

Browse files
author
Balazs Benics
committed
[analyzer] Allow matching non-CallExprs using CallDescriptions
Fallback to stringification and string comparison if we cannot compare the `IdentifierInfo`s, which is the case for C++ overloaded operators, constructors, destructors, etc. Examples: { "std", "basic_string", "basic_string", 2} // match the 2 param std::string constructor { "std", "basic_string", "~basic_string" } // match the std::string destructor { "aaa", "bbb", "operator int" } // matches the struct bbb conversion operator to int Reviewed By: martong Differential Revision: https://reviews.llvm.org/D111535
1 parent 3ec7b91 commit 72d04d7

File tree

2 files changed

+6
-6
lines changed

2 files changed

+6
-6
lines changed

clang/lib/StaticAnalyzer/Core/CallEvent.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,9 +328,11 @@ bool CallEvent::isCalled(const CallDescription &CD) const {
328328
if (const auto *II = Name.getAsIdentifierInfo())
329329
return II == CD.II.getValue(); // Fast case.
330330

331-
// Simply report mismatch for:
331+
// Fallback to the slow stringification and comparison for:
332332
// C++ overloaded operators, constructors, destructors, etc.
333-
return false;
333+
// FIXME This comparison is way SLOWER than comparing pointers.
334+
// At some point in the future, we should compare FunctionDecl pointers.
335+
return Name.getAsString() == CD.getFunctionName();
334336
};
335337

336338
const auto ExactMatchArgAndParamCounts =

clang/unittests/StaticAnalyzer/CallDescriptionTest.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,12 +187,11 @@ TEST(CallDescription, MatchConstructor) {
187187
using namespace std;
188188
basic_string<char> s("hello");
189189
})code";
190-
// FIXME: We should match.
191190
const std::string Code = (Twine{MockStdStringHeader} + AdditionalCode).str();
192191
EXPECT_TRUE(tooling::runToolOnCode(
193192
std::unique_ptr<FrontendAction>(
194193
new CallDescriptionAction<CXXConstructExpr>({
195-
{{{"std", "basic_string", "basic_string"}, 2, 2}, false},
194+
{{{"std", "basic_string", "basic_string"}, 2, 2}, true},
196195
})),
197196
Code));
198197
}
@@ -216,10 +215,9 @@ TEST(CallDescription, MatchConversionOperator) {
216215
aaa::bbb::Bar x;
217216
int tmp = x;
218217
})code";
219-
// FIXME: We should match.
220218
EXPECT_TRUE(tooling::runToolOnCode(
221219
std::unique_ptr<FrontendAction>(new CallDescriptionAction<>({
222-
{{{"aaa", "bbb", "Bar", "operator int"}}, false},
220+
{{{"aaa", "bbb", "Bar", "operator int"}}, true},
223221
})),
224222
Code));
225223
}

0 commit comments

Comments
 (0)