Skip to content

Commit

Permalink
[clang][USR] Prevent crashes on incomplete FunctionDecls
Browse files Browse the repository at this point in the history
FunctionDecls can be created with null types (D124351 added such a new
code path), to be filled in later. But parsing can stop before
completing the Decl (e.g. if code completion
point is reached).
Unfortunately most of the methods in FunctionDecl and its derived
classes assume a complete decl and don't perform null-checks.
Since we're not encountring crashes in the wild along other code paths
today introducing extra checks into quite a lot of places didn't feel
right (due to extra complexity && run time checks).
I believe another alternative would be to change Parser & Sema to never
create decls with invalid types, but I can't really see an easy way of
doing that, as most of the pieces are structured around filling that
information as parsing proceeds.

Differential Revision: https://reviews.llvm.org/D149733
  • Loading branch information
kadircet committed May 15, 2023
1 parent b763d6a commit 0d1be98
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
13 changes: 13 additions & 0 deletions clang-tools-extra/clangd/unittests/CodeCompleteTests.cpp
Expand Up @@ -4002,6 +4002,19 @@ TEST(SignatureHelp, TemplateArguments) {
EXPECT_EQ(Second.activeParameter, 1);
}

TEST(CompletionTest, DoNotCrash) {
llvm::StringLiteral Cases[] = {
R"cpp(
template <typename = int> struct Foo {};
auto a = [x(3)](Foo<^>){};
)cpp",
};
for (auto Case : Cases) {
SCOPED_TRACE(Case);
auto Completions = completions(Case);
}
}

} // namespace
} // namespace clangd
} // namespace clang
5 changes: 5 additions & 0 deletions clang/lib/Index/USRGeneration.cpp
Expand Up @@ -226,6 +226,11 @@ void USRGenerator::VisitFunctionDecl(const FunctionDecl *D) {
if (ShouldGenerateLocation(D) && GenLoc(D, /*IncludeOffset=*/isLocal(D)))
return;

if (D->getType().isNull()) {
IgnoreResults = true;
return;
}

const unsigned StartSize = Buf.size();
VisitDeclContext(D->getDeclContext());
if (Buf.size() == StartSize)
Expand Down

0 comments on commit 0d1be98

Please sign in to comment.