Debian Codegraph @

libcextract/LLVMMisc.hh

raw
1 //===- LLVMMisc.cpp - Implement misc functions that depends on LLVM *- C++ -*-===//
2 //
3 // This project is licensed 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 /// \file
10 /// MISC functions that is used through the program and use any LLVM
11 /// datastructure.
12 //
13 //===----------------------------------------------------------------------===//
14
15 /* Author: Giuliano Belinassi */
16
17 #pragma once
18
19 #include <system_error>
20 #include <clang/Tooling/Tooling.h>
21 #include "clang/Analysis/CallGraph.h"
22 #include "clang/Sema/IdentifierResolver.h"
23 #include "clang/AST/DeclContextInternals.h"
24
25 #include "NonLLVMMisc.hh"
26
27 class SymbolNotFoundException : public std::exception {
28 public:
29 SymbolNotFoundException(std::string name)
30 : Name(name)
31 {}
32
33 virtual const char *what() const noexcept
34 {
35 return Name.c_str();
36 }
37
38 private:
39 std::string Name;
40 };
41
42 using namespace clang;
43
44 /** Check if Decl is a builtin. */
45 bool Is_Builtin_Decl(const Decl *decl);
46
47 /** Build CallGraph from AST.
48 *
49 * The CallGraph is a datastructure in which nodes are functions and edges
50 * represents function call points. For example:
51 *
52 * void f();
53 * void g() { f(); f(); }
54 *
55 * Resuluts in the following CallGraph:
56 *
57 * (f) -> (g)
58 * -> (g)
59 *
60 * There are two edges to `g` because there are two callpoints to it.
61 * Hence, the resulting graph is not `simple` in Graph Theory nomenclature. But
62 * for the analysis we are doing it is suffice to be, so perhaps some extra
63 * performance can be archived if we could remove duplicated edges.
64 *
65 */
66 CallGraph *Build_CallGraph_From_AST(ASTUnit *ast);
67
68 /* Look into previous versions of `decl` for a declaration in the AST without
69 a body. */
70 FunctionDecl *Get_Bodyless_Decl(FunctionDecl *decl);
71 TagDecl *Get_Bodyless_Decl(TagDecl *decl);
72 Decl *Get_Bodyless_Decl(Decl *decl);
73
74 /* Look into previous versions of `decl` for a declaration in the AST without
75 a body. If not found, it returns itself (the version with body). */
76 FunctionDecl *Get_Bodyless_Or_Itself(FunctionDecl *decl);
77 TagDecl *Get_Bodyless_Or_Itself(TagDecl *decl);
78 Decl *Get_Bodyless_Or_Itself(Decl *decl);
79
80 /** Get version of declarator with body. */
81 DeclaratorDecl *Get_With_Body(DeclaratorDecl *decl);
82
83 /** Get version of declarator with body or itself. */
84 DeclaratorDecl *Get_With_Body_Or_Itself(DeclaratorDecl *decl);
85
86 /* Get the TopLevel Decl that contains the location loc. */
87 Decl *Get_Toplevel_Decl_At_Location(ASTUnit *ast, const SourceLocation &loc);
88
89 /* Get Toplevel decls with same beginloc. */
90 ArrayRef<Decl *> Get_Toplev_Decls_With_Same_Beginloc(ASTUnit *ast, const SourceLocation &loc);
91
92 /** Build a clang-extract location comment. */
93 std::string Build_CE_Location_Comment(SourceManager &sm, const SourceLocation &loc);
94
95 /** Get the begin location of the Decl before its comment if it have one. */
96 SourceLocation Get_Begin_Loc_Of_Decl_Or_Comment(ASTContext &ctx, Decl *decl);
97
98 /** Get decl clang-extract location comment, or build one if it doesn't exist. */
99 std::string Get_Or_Build_CE_Location_Comment(ASTContext &ctx, Decl *decl);
100
101 /** Check if Decl have a Location comment. */
102 bool Have_Location_Comment(const SourceManager &sm, RawComment *comment);
103
104 /** Lookup in the symbol table for a declaration with given name passed by info. */
105 DeclContextLookupResult Get_Decl_From_Symtab(ASTUnit *ast, const IdentifierInfo *info);
106
107 /** Lookup in the symbol table for a declaration with given name passed by name. */
108 DeclContextLookupResult Get_Decl_From_Symtab(ASTUnit *ast, const StringRef &name);
109
110 /** Check if two Decls are equivalent. */
111 bool Is_Decl_Equivalent_To(Decl *a, Decl *b);
112
113 /** Check if string has unmatched #if, #ifdef, #ifndef. */
114 bool Has_Balanced_Ifdef(const StringRef &string);
115
116
117 /** Check if the VarDecl is declared as TLS. */
118 static inline bool Is_TLS(VarDecl *decl)
119 {
120 return decl->getTLSKind() == VarDecl::TLS_None ? false : true;
121 }