diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp index e4933fb10854..2c2f4661a95e 100644 --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -298,6 +298,9 @@ RawComment *ASTContext::getRawCommentForDeclNoCache(const Decl *D) const { return nullptr; const FileID File = SourceMgr.getDecomposedLoc(DeclLoc).first; + if (!File.isValid()) { + return nullptr; + } const auto CommentsInThisFile = Comments.getCommentsInFile(File); if (!CommentsInThisFile || CommentsInThisFile->empty()) return nullptr; diff --git a/clang/test/AST/ast-crash-doc.cpp b/clang/test/AST/ast-crash-doc.cpp new file mode 100644 index 000000000000..c4959647fc0f --- /dev/null +++ b/clang/test/AST/ast-crash-doc.cpp @@ -0,0 +1,30 @@ +// RUN: rm -rf %t +// RUN: split-file %s %t + +// RUN: %clang_cc1 -emit-module -x c++ -fmodules -I %t/Inputs -fmodule-name=aa %t/Inputs/module.modulemap -o %t/aa.pcm +// RUN: rm %t/Inputs/b.h +// RUN: not %clang_cc1 -x c++ -Wdocumentation -ast-dump-all -fmodules -I %t/Inputs -fmodule-file=%t/aa.pcm %t/test.cpp | FileCheck %s + +//--- Inputs/module.modulemap +module aa { + header "a.h" + header "b.h" +} + +//--- Inputs/a.h +// empty file + +//--- Inputs/b.h +/// test foo @return +int foo(); + + +//--- test.cpp +#include "a.h" + +/// test comment at the primary file + +int a = foo(); + + +// CHECK: TranslationUnitDecl