File tree Expand file tree Collapse file tree 2 files changed +21
-1
lines changed Expand file tree Collapse file tree 2 files changed +21
-1
lines changed Original file line number Diff line number Diff line change @@ -232,7 +232,12 @@ bool isSpelledInSource(SourceLocation Loc, const SourceManager &SM) {
232
232
if (Loc.isFileID ())
233
233
return true ;
234
234
auto Spelling = SM.getDecomposedSpellingLoc (Loc);
235
- StringRef SpellingFile = SM.getSLocEntry (Spelling.first ).getFile ().getName ();
235
+ bool InvalidSLocEntry = false ;
236
+ const auto SLocEntry = SM.getSLocEntry (Spelling.first , &InvalidSLocEntry);
237
+ if (InvalidSLocEntry) {
238
+ return false ;
239
+ }
240
+ const StringRef SpellingFile = SLocEntry.getFile ().getName ();
236
241
if (SpellingFile == " <scratch space>" )
237
242
return false ;
238
243
if (SpellingFile == " <built-in>" )
Original file line number Diff line number Diff line change @@ -813,6 +813,21 @@ TEST(SourceCodeTests, isKeywords) {
813
813
EXPECT_FALSE (isKeyword (" override" , LangOpts));
814
814
}
815
815
816
+ TEST (SourceCodeTests, isSpelledInSource) {
817
+ Annotations Test (R"cpp(
818
+ int abc = 1;
819
+ )cpp" );
820
+
821
+ ParsedAST AST = TestTU::withCode (Test.code ()).build ();
822
+ llvm::errs () << Test.code ();
823
+ const SourceManager &SM = AST.getSourceManager ();
824
+
825
+ EXPECT_TRUE (
826
+ isSpelledInSource (SM.getLocForStartOfFile (SM.getMainFileID ()), SM));
827
+ EXPECT_TRUE (isSpelledInSource (SourceLocation (), SM));
828
+ EXPECT_FALSE (isSpelledInSource (SourceLocation::getFromRawEncoding (-1 ), SM));
829
+ }
830
+
816
831
struct IncrementalTestStep {
817
832
llvm::StringRef Src;
818
833
llvm::StringRef Contents;
You can’t perform that action at this time.
0 commit comments