[clangd] Fix incorrect file path for symbols defined by the compile command-line option.

Summary:

Reviewers: ioeric

Reviewed By: ioeric

Subscribers: klimek, ilya-biryukov, jkorous-apple, cfe-commits

Differential Revision: https://reviews.llvm.org/D42913

llvm-svn: 324328
This commit is contained in:
Haojian Wu 2018-02-06 09:50:35 +00:00
parent 6df8f43c4d
commit 3b8e00c5e0
2 changed files with 33 additions and 6 deletions

View File

@ -124,10 +124,15 @@ SymbolLocation GetSymbolLocation(const NamedDecl *D, SourceManager &SM,
SourceLocation Loc = SM.getSpellingLoc(D->getLocation());
if (D->getLocation().isMacroID()) {
// The symbol is formed via macro concatenation, the spelling location will
// be "<scratch space>", which is not interesting to us, use the expansion
// location instead.
if (llvm::StringRef(Loc.printToString(SM)).startswith("<scratch")) {
// We use the expansion location for the following symbols, as spelling
// locations of these symbols are not interesting to us:
// * symbols formed via macro concatenation, the spelling location will
// be "<scratch space>"
// * symbols controlled and defined by a compile command-line option
// `-DName=foo`, the spelling location will be "<command line>".
std::string PrintLoc = Loc.printToString(SM);
if (llvm::StringRef(PrintLoc).startswith("<scratch") ||
llvm::StringRef(PrintLoc).startswith("<command line>")) {
FilePathStorage = makeAbsolutePath(
SM, SM.getFilename(SM.getExpansionLoc(D->getLocation())),
FallbackDir);

View File

@ -82,7 +82,8 @@ public:
class SymbolCollectorTest : public ::testing::Test {
public:
bool runSymbolCollector(StringRef HeaderCode, StringRef MainCode) {
bool runSymbolCollector(StringRef HeaderCode, StringRef MainCode,
const std::vector<std::string> &ExtraArgs = {}) {
llvm::IntrusiveRefCntPtr<vfs::InMemoryFileSystem> InMemoryFileSystem(
new vfs::InMemoryFileSystem);
llvm::IntrusiveRefCntPtr<FileManager> Files(
@ -90,8 +91,11 @@ public:
auto Factory = llvm::make_unique<SymbolIndexActionFactory>(CollectorOpts);
std::vector<std::string> Args = {"symbol_collector", "-fsyntax-only",
"-std=c++11", TestFileName};
Args.insert(Args.end(), ExtraArgs.begin(), ExtraArgs.end());
tooling::ToolInvocation Invocation(
{"symbol_collector", "-fsyntax-only", "-std=c++11", TestFileName},
Args,
Factory->create(), Files.get(),
std::make_shared<PCHContainerOperations>());
@ -264,6 +268,24 @@ TEST_F(SymbolCollectorTest, SymbolFormedFromMacroInMainFile) {
CPath(TestFileName))));
}
TEST_F(SymbolCollectorTest, SymbolFormedByCLI) {
CollectorOpts.IndexMainFiles = false;
Annotations Header(R"(
#ifdef NAME
$expansion[[class NAME {}]];
#endif
)");
runSymbolCollector(Header.code(), /*Main=*/"",
/*ExtraArgs=*/{"-DNAME=name"});
EXPECT_THAT(Symbols,
UnorderedElementsAre(
AllOf(QName("name"),
LocationOffsets(Header.offsetRange("expansion")),
CPath(TestHeaderName))));
}
TEST_F(SymbolCollectorTest, IgnoreSymbolsInMainFile) {
CollectorOpts.IndexMainFiles = false;
const std::string Header = R"(