blob: cfcde118202cf75061aef5e534a3e16168df83e4 [file] [log] [blame]
Haojian Wu5f100262018-03-09 14:00:34 +00001//===--- AST.cpp - Utility AST functions -----------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "AST.h"
11
12#include "clang/AST/ASTContext.h"
13#include "clang/AST/Decl.h"
14#include "clang/Basic/SourceManager.h"
15
16namespace clang {
17namespace clangd {
18using namespace llvm;
19
20SourceLocation findNameLoc(const clang::Decl* D) {
21 const auto& SM = D->getASTContext().getSourceManager();
22 // FIXME: Revisit the strategy, the heuristic is limitted when handling
23 // macros, we should use the location where the whole definition occurs.
24 SourceLocation SpellingLoc = SM.getSpellingLoc(D->getLocation());
25 if (D->getLocation().isMacroID()) {
26 std::string PrintLoc = SpellingLoc.printToString(SM);
27 if (llvm::StringRef(PrintLoc).startswith("<scratch") ||
28 llvm::StringRef(PrintLoc).startswith("<command line>")) {
29 // We use the expansion location for the following symbols, as spelling
30 // locations of these symbols are not interesting to us:
31 // * symbols formed via macro concatenation, the spelling location will
32 // be "<scratch space>"
33 // * symbols controlled and defined by a compile command-line option
34 // `-DName=foo`, the spelling location will be "<command line>".
Richard Smith4bb15ab2018-04-30 05:26:07 +000035 SpellingLoc = SM.getExpansionRange(D->getLocation()).getBegin();
Haojian Wu5f100262018-03-09 14:00:34 +000036 }
37 }
38 return SpellingLoc;
39}
40
41} // namespace clangd
42} // namespace clang