Skip to content

Commit

Permalink
Merge commit 5f4ee5a2dfa9 from llvm-project (by Shanzhi):
Browse files Browse the repository at this point in the history
  [Clang][AST] Fix a crash on attaching doc comments (#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 256a0b298c68b89688b80350b034daf2f7785b67, 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 #67979
  Fixes #68524
  Fixes #70550

This should fix a segfault when compiling graphics/gdal.
  • Loading branch information
DimitryAndric committed Feb 9, 2024
1 parent d4389cf commit 4721947
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion contrib/llvm-project/clang/lib/AST/ASTContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,11 @@ void ASTContext::attachCommentsToJustParsedDecls(ArrayRef<Decl *> Decls,
return;

FileID File;
for (Decl *D : Decls) {
for (const Decl *D : Decls) {
if (D->isInvalidDecl())
continue;

D = &adjustDeclToTemplate(*D);
SourceLocation Loc = D->getLocation();
if (Loc.isValid()) {
// See if there are any new comments that are not attached to a decl.
Expand Down

0 comments on commit 4721947

Please sign in to comment.