clang-tools  9.0.0
AST.h
Go to the documentation of this file.
1 //===--- AST.h - Utility AST functions -------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Various code that examines C++ source code using AST.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_AST_H_
14 #define LLVM_CLANG_TOOLS_EXTRA_CLANGD_AST_H_
15 
16 #include "index/SymbolID.h"
17 #include "clang/AST/Decl.h"
18 #include "clang/Basic/SourceLocation.h"
19 #include "clang/Lex/MacroInfo.h"
20 
21 namespace clang {
22 class SourceManager;
23 class Decl;
24 
25 namespace clangd {
26 
27 /// Returns true if the declaration is considered implementation detail based on
28 /// heuristics. For example, a declaration whose name is not explicitly spelled
29 /// in code is considered implementation detail.
30 bool isImplementationDetail(const Decl *D);
31 
32 /// Find the identifier source location of the given D.
33 ///
34 /// The returned location is usually the spelling location where the name of the
35 /// decl occurs in the code.
36 SourceLocation findNameLoc(const clang::Decl *D);
37 
38 /// Returns the qualified name of ND. The scope doesn't contain unwritten scopes
39 /// like inline namespaces.
40 std::string printQualifiedName(const NamedDecl &ND);
41 
42 /// Returns the first enclosing namespace scope starting from \p DC.
43 std::string printNamespaceScope(const DeclContext &DC);
44 
45 /// Prints unqualified name of the decl for the purpose of displaying it to the
46 /// user. Anonymous decls return names of the form "(anonymous {kind})", e.g.
47 /// "(anonymous struct)" or "(anonymous namespace)".
48 std::string printName(const ASTContext &Ctx, const NamedDecl &ND);
49 
50 /// Prints template arguments of a decl as written in the source code, including
51 /// enclosing '<' and '>', e.g for a partial specialization like: template
52 /// <typename U> struct Foo<int, U> will return '<int, U>'. Returns an empty
53 /// string if decl is not a template specialization.
54 std::string printTemplateSpecializationArgs(const NamedDecl &ND);
55 
56 /// Gets the symbol ID for a declaration, if possible.
57 llvm::Optional<SymbolID> getSymbolID(const Decl *D);
58 
59 /// Gets the symbol ID for a macro, if possible.
60 /// Currently, this is an encoded USR of the macro, which incorporates macro
61 /// locations (e.g. file name, offset in file).
62 /// FIXME: the USR semantics might not be stable enough as the ID for index
63 /// macro (e.g. a change in definition offset can result in a different USR). We
64 /// could change these semantics in the future by reimplementing this funcure
65 /// (e.g. avoid USR for macros).
66 llvm::Optional<SymbolID> getSymbolID(const IdentifierInfo &II,
67  const MacroInfo *MI,
68  const SourceManager &SM);
69 
70 /// Returns a QualType as string.
71 std::string printType(const QualType QT, const DeclContext & Context);
72 
73 /// Try to shorten the OriginalName by removing namespaces from the left of
74 /// the string that are redundant in the CurrentNamespace. This way the type
75 /// idenfier become shorter and easier to read.
76 /// Limitation: It only handles the qualifier of the type itself, not that of
77 /// templates.
78 /// FIXME: change type of parameter CurrentNamespace to DeclContext ,
79 /// take in to account using directives etc
80 /// Example: shortenNamespace("ns1::MyClass<ns1::OtherClass>", "ns1")
81 /// --> "MyClass<ns1::OtherClass>"
82 std::string shortenNamespace(const llvm::StringRef OriginalName,
83  const llvm::StringRef CurrentNamespace);
84 
85 
86 } // namespace clangd
87 } // namespace clang
88 
89 #endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_AST_H_
std::string printName(const ASTContext &Ctx, const NamedDecl &ND)
Prints unqualified name of the decl for the purpose of displaying it to the user. ...
Definition: AST.cpp:90
std::string printType(const QualType QT, const DeclContext &Context)
Returns a QualType as string.
Definition: AST.cpp:193
std::string printQualifiedName(const NamedDecl &ND)
Returns the qualified name of ND.
Definition: AST.cpp:75
llvm::Optional< SymbolID > getSymbolID(const Decl *D)
Gets the symbol ID for a declaration, if possible.
Definition: AST.cpp:154
std::string printNamespaceScope(const DeclContext &DC)
Returns the first enclosing namespace scope starting from DC.
Definition: AST.cpp:146
SourceLocation findNameLoc(const clang::Decl *D)
Find the identifier source location of the given D.
Definition: AST.cpp:67
std::string printTemplateSpecializationArgs(const NamedDecl &ND)
Prints template arguments of a decl as written in the source code, including enclosing &#39;<&#39; and &#39;>&#39;...
Definition: AST.cpp:117
Context Ctx
const Decl * D
Definition: XRefs.cpp:868
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
bool isImplementationDetail(const Decl *D)
Returns true if the declaration is considered implementation detail based on heuristics.
Definition: AST.cpp:65
std::string shortenNamespace(const llvm::StringRef OriginalName, const llvm::StringRef CurrentNamespace)
Try to shorten the OriginalName by removing namespaces from the left of the string that are redundant...
Definition: AST.cpp:172