Skip to content

Commit

Permalink
[Sema] Fix a crash when attaching comments to an implicit decl
Browse files Browse the repository at this point in the history
When an implicitly generated decl was the first entry in the group, we
attempted to lookup comments with an empty FileID, leading to crashes. Avoid
this by trying to use the other declarations in the group, and then bailing out
if none are valid.

rdar://59919733
Differential revision: https://reviews.llvm.org/D75483
  • Loading branch information
epilk committed Mar 3, 2020
1 parent 44fa47c commit 29a4239
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
18 changes: 14 additions & 4 deletions clang/lib/AST/ASTContext.cpp
Expand Up @@ -474,10 +474,20 @@ void ASTContext::attachCommentsToJustParsedDecls(ArrayRef<Decl *> Decls,
if (Comments.empty() || Decls.empty())
return;

// See if there are any new comments that are not attached to a decl.
// The location doesn't have to be precise - we care only about the file.
const FileID File =
SourceMgr.getDecomposedLoc((*Decls.begin())->getLocation()).first;
FileID File;
for (Decl *D : Decls) {
SourceLocation Loc = D->getLocation();
if (Loc.isValid()) {
// See if there are any new comments that are not attached to a decl.
// The location doesn't have to be precise - we care only about the file.
File = SourceMgr.getDecomposedLoc(Loc).first;
break;
}
}

if (File.isInvalid())
return;

auto CommentsInThisFile = Comments.getCommentsInFile(File);
if (!CommentsInThisFile || CommentsInThisFile->empty() ||
CommentsInThisFile->rbegin()->second->isAttached())
Expand Down
8 changes: 8 additions & 0 deletions clang/test/Sema/warn-documentation.m
@@ -1,4 +1,5 @@
// RUN: %clang_cc1 -fsyntax-only -fblocks -Wno-objc-root-class -Wdocumentation -Wdocumentation-pedantic -verify %s
// RUN: %clang_cc1 -xobjective-c++ -fsyntax-only -fblocks -Wno-objc-root-class -Wdocumentation -Wdocumentation-pedantic -verify %s

@class NSString;

Expand Down Expand Up @@ -318,3 +319,10 @@ @interface CheckFunctionBlockPointerVars {
// expected-warning@-1 {{'\return' command used in a comment that is not attached to a function or method declaration}}
VoidBlockTypeCall ^e; ///< \return none
// expected-warning@-1 {{'\return' command used in a comment that is not attached to a function or method declaration}}

#ifdef __cplusplus
@interface HasAnonNamespace @end
@implementation HasAnonNamespace
namespace {}
@end
#endif

0 comments on commit 29a4239

Please sign in to comment.