Skip to content

Commit 5f4ee5a

Browse files
authored
[Clang][AST] Fix a crash on attaching doc comments (llvm#78716)
This crash is basically caused by calling `ASTContext::getRawCommentForDeclNoCacheImp` with its input arguments `RepresentativeLocForDecl` and `CommentsInTheFile` refering to different files. A reduced reproducer is provided in this patch. After the source locations for instantiations of funtion template are corrected in the commit 256a0b2, the variable `CommitsInThisFile` in the function `ASTContext::attachCommentsToJustParsedDecls` would refer to the source file rather than the header file for implicit function template instantiation. Therefore, in the first loop in `ASTContext::attachCommentsToJustParsedDecls`, `D` should also be adjusted for relevant scenarios like the second loop. Fixes llvm#67979 Fixes llvm#68524 Fixes llvm#70550
1 parent 14a1510 commit 5f4ee5a

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

Diff for: clang/lib/AST/ASTContext.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,11 @@ void ASTContext::attachCommentsToJustParsedDecls(ArrayRef<Decl *> Decls,
498498
return;
499499

500500
FileID File;
501-
for (Decl *D : Decls) {
501+
for (const Decl *D : Decls) {
502+
if (D->isInvalidDecl())
503+
continue;
504+
505+
D = &adjustDeclToTemplate(*D);
502506
SourceLocation Loc = D->getLocation();
503507
if (Loc.isValid()) {
504508
// See if there are any new comments that are not attached to a decl.

Diff for: clang/test/AST/ast-crash-doc-function-template.cpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// RUN: rm -rf %t
2+
// RUN: split-file %s %t
3+
4+
// RUN: %clang_cc1 -x c++ -Wdocumentation -fsyntax-only -ast-dump-all %t/t.cpp
5+
6+
//--- t.h
7+
/// MyClass in the header file
8+
class MyClass {
9+
public:
10+
template <typename T>
11+
void Foo() const;
12+
13+
/// Bar
14+
void Bar() const;
15+
};
16+
17+
//--- t.cpp
18+
#include "t.h"
19+
20+
/// MyClass::Bar: Foo<int>() is implicitly instantiated and called here.
21+
void MyClass::Bar() const {
22+
Foo<int>();
23+
}
24+
25+
/// MyClass::Foo
26+
template <typename T>
27+
void MyClass::Foo() const {
28+
}
29+
30+
// CHECK: TranslationUnitDecl

0 commit comments

Comments
 (0)