[PATCH] D75460: [clangd] Fix isInsideMainFile to be aware of preamble.
Sam McCall via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Mar 2 08:05:02 PST 2020
sammccall created this revision.
sammccall added a reviewer: kadircet.
Herald added subscribers: cfe-commits, usaxena95, arphaman, jkorous, MaskRay, ilya-biryukov.
Herald added a project: clang.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D75460
Files:
clang-tools-extra/clangd/SourceCode.cpp
clang-tools-extra/clangd/unittests/SourceCodeTests.cpp
Index: clang-tools-extra/clangd/unittests/SourceCodeTests.cpp
===================================================================
--- clang-tools-extra/clangd/unittests/SourceCodeTests.cpp
+++ clang-tools-extra/clangd/unittests/SourceCodeTests.cpp
@@ -447,10 +447,12 @@
class Header {};
)cpp";
TU.Code = R"cpp(
+ #define DEFINE_MAIN4 class Main4{};
class Main1 {};
DEFINE_CLASS(Main2)
DEFINE_YY
class Main {};
+ DEFINE_MAIN4
)cpp";
TU.ExtraArgs.push_back("-DHeader=Header3");
TU.ExtraArgs.push_back("-DMain=Main3");
@@ -460,10 +462,13 @@
return findDecl(AST, Name).getLocation();
};
for (const auto *HeaderDecl : {"Header1", "Header2", "Header3"})
- EXPECT_FALSE(isInsideMainFile(DeclLoc(HeaderDecl), SM));
+ EXPECT_FALSE(isInsideMainFile(DeclLoc(HeaderDecl), SM)) << HeaderDecl;
- for (const auto *MainDecl : {"Main1", "Main2", "Main3", "YY"})
- EXPECT_TRUE(isInsideMainFile(DeclLoc(MainDecl), SM));
+ for (const auto *MainDecl : {"Main1", "Main2", "Main3", "Main4", "YY"})
+ EXPECT_TRUE(isInsideMainFile(DeclLoc(MainDecl), SM)) << MainDecl;
+
+ // Main4 is *spelled* in the preamble, but in the main-file part of it.
+ EXPECT_TRUE(isInsideMainFile(SM.getSpellingLoc(DeclLoc("Main4")), SM));
}
// Test for functions toHalfOpenFileRange and getHalfOpenFileRange
Index: clang-tools-extra/clangd/SourceCode.cpp
===================================================================
--- clang-tools-extra/clangd/SourceCode.cpp
+++ clang-tools-extra/clangd/SourceCode.cpp
@@ -431,7 +431,10 @@
}
bool isInsideMainFile(SourceLocation Loc, const SourceManager &SM) {
- return Loc.isValid() && SM.isWrittenInMainFile(SM.getExpansionLoc(Loc));
+ if (!Loc.isValid())
+ return false;
+ FileID FID = SM.getFileID(SM.getExpansionLoc(Loc));
+ return FID == SM.getMainFileID() || FID == SM.getPreambleFileID();
}
llvm::Optional<SourceRange> toHalfOpenFileRange(const SourceManager &SM,
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D75460.247651.patch
Type: text/x-patch
Size: 1962 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20200302/608c2dbe/attachment-0001.bin>
More information about the cfe-commits
mailing list