[clang] 29a4239 - [Sema] Fix a crash when attaching comments to an implicit decl

Erik Pilkington via cfe-commits cfe-commits at lists.llvm.org
Mon Mar 2 16:50:28 PST 2020


Author: Erik Pilkington
Date: 2020-03-02T16:49:53-08:00
New Revision: 29a4239d31c6d8ccc557afbe0999aa096ca95cc6

URL: https://github.com/llvm/llvm-project/commit/29a4239d31c6d8ccc557afbe0999aa096ca95cc6
DIFF: https://github.com/llvm/llvm-project/commit/29a4239d31c6d8ccc557afbe0999aa096ca95cc6.diff

LOG: [Sema] Fix a crash when attaching comments to an implicit decl

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

Added: 
    

Modified: 
    clang/lib/AST/ASTContext.cpp
    clang/test/Sema/warn-documentation.m

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp
index bc4f2b491e11..93a8aab7c068 100644
--- a/clang/lib/AST/ASTContext.cpp
+++ b/clang/lib/AST/ASTContext.cpp
@@ -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())

diff  --git a/clang/test/Sema/warn-documentation.m b/clang/test/Sema/warn-documentation.m
index c713d5b07f85..5d60a52ae6fe 100644
--- a/clang/test/Sema/warn-documentation.m
+++ b/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;
 
@@ -318,3 +319,10 @@ @interface CheckFunctionBlockPointerVars {
 // expected-warning at -1 {{'\return' command used in a comment that is not attached to a function or method declaration}}
 VoidBlockTypeCall ^e; ///< \return none
 // expected-warning at -1 {{'\return' command used in a comment that is not attached to a function or method declaration}}
+
+#ifdef __cplusplus
+ at interface HasAnonNamespace @end
+ at implementation HasAnonNamespace
+namespace {}
+ at end
+#endif


        


More information about the cfe-commits mailing list