| 1 |
//===- PrettyPrint.hh - Print Decls or Preprocessor entities to file *- 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 |
/// Print Decls or Preprocessor entities to file. |
| 11 |
// |
| 12 |
//===----------------------------------------------------------------------===// |
| 13 |
|
| 14 |
/* Author: Giuliano Belinassi */ |
| 15 |
|
| 16 |
#pragma once |
| 17 |
|
| 18 |
#include <unordered_set> |
| 19 |
#include <clang/Tooling/Tooling.h> |
| 20 |
#include <llvm/Support/raw_ostream.h> |
| 21 |
|
| 22 |
#include "IncludeTree.hh" |
| 23 |
#include "MacroWalker.hh" |
| 24 |
#include "TopLevelASTIterator.hh" |
| 25 |
|
| 26 |
using namespace clang; |
| 27 |
|
| 28 |
class RecursivePrint; |
| 29 |
|
| 30 |
/** @brief Wrapper class to printing clang AST nodes. |
| 31 |
* |
| 32 |
* Clang AST pretty printer does a good job at printing declaration nodes. Just |
| 33 |
* calling decl->print(params) will also print its children. However there are |
| 34 |
* some caveats regarding some nodes, in which the methods `Print_Decl` and |
| 35 |
* `Print_Stmt` are used to detect and print them correctly. |
| 36 |
*/ |
| 37 |
class PrettyPrint |
| 38 |
{ |
| 39 |
public: |
| 40 |
/** Print a Decl node into ostream `Out`. */ |
| 41 |
static void Print_Decl(Decl *decl, bool keep_includes = false); |
| 42 |
|
| 43 |
/** Print Decl node as is, without any kind of processing. */ |
| 44 |
static void Print_Decl_Raw(Decl *decl); |
| 45 |
|
| 46 |
static inline void Print_Decl_Tree(Decl *decl) |
| 47 |
{ decl->print(*Out, PPolicy); *Out << '\n'; } |
| 48 |
|
| 49 |
static inline void Debug_Decl_Tree(Decl *decl) |
| 50 |
{ decl->print(llvm::outs(), PPolicy); llvm::outs() << '\n'; } |
| 51 |
|
| 52 |
static void Debug_Decl(Decl *decl); |
| 53 |
static void Debug_Stmt(Stmt *stmt); |
| 54 |
static void Debug_Macro_Def(MacroDefinitionRecord *rec); |
| 55 |
static void Debug_Macro_Undef(MacroDirective *); |
| 56 |
static void Debug_InclusionDirective(InclusionDirective *); |
| 57 |
|
| 58 |
/** Print a Stmt node into ostream `Out`. */ |
| 59 |
static void Print_Stmt(Stmt *stmt); |
| 60 |
|
| 61 |
/** Print a Macro Defintion into ostream `Out`. */ |
| 62 |
static void Print_Macro_Def(MacroDefinitionRecord *rec); |
| 63 |
|
| 64 |
/** Print a Macro Undef into ostream `Out`. */ |
| 65 |
static void Print_Macro_Undef(MacroDirective *directive); |
| 66 |
|
| 67 |
static void Print_MacroInfo(MacroInfo *info); |
| 68 |
|
| 69 |
static void Print_InclusionDirective(InclusionDirective *); |
| 70 |
|
| 71 |
static void Print_Attr(Attr *attr); |
| 72 |
|
| 73 |
static void Print_Comment(const std::string &comment); |
| 74 |
|
| 75 |
static void Print_Raw(const std::string &string); |
| 76 |
|
| 77 |
static void Print_RawComment(SourceManager &sm, RawComment *comment); |
| 78 |
|
| 79 |
static void Debug_SourceLoc(const SourceLocation &loc); |
| 80 |
|
| 81 |
static bool Contains_From_LineCol(const SourceRange &a, const SourceRange &b); |
| 82 |
|
| 83 |
static bool Contains(const SourceRange &a, const SourceRange &b); |
| 84 |
|
| 85 |
static inline void Set_AST(ASTUnit *ast) |
| 86 |
{ |
| 87 |
AST = ast; |
| 88 |
} |
| 89 |
|
| 90 |
static inline LangOptions &Get_Lang_Options(void) |
| 91 |
{ |
| 92 |
return LangOpts; |
| 93 |
} |
| 94 |
|
| 95 |
static void Set_Output_Ostream(llvm::raw_ostream *out) |
| 96 |
{ |
| 97 |
Out = out; |
| 98 |
} |
| 99 |
|
| 100 |
/** Gets the portion of the code that corresponds to given SourceRange, including the |
| 101 |
last token. Returns expanded macros. |
| 102 |
|
| 103 |
@see get_source_text_raw(). */ |
| 104 |
static StringRef Get_Source_Text(const SourceRange &range); |
| 105 |
|
| 106 |
/** Gets the portion of the code that corresponds to given SourceRange exactly as |
| 107 |
the range is given. |
| 108 |
|
| 109 |
@warning The end location of the SourceRange returned by some Clang functions |
| 110 |
(such as clang::Expr::getSourceRange) might actually point to the first character |
| 111 |
(the "location") of the last token of the expression, rather than the character |
| 112 |
past-the-end of the expression like clang::Lexer::getSourceText expects. |
| 113 |
get_source_text_raw() does not take this into account. Use get_source_text() |
| 114 |
instead if you want to get the source text including the last token. |
| 115 |
|
| 116 |
@warning This function does not obtain the source of a macro/preprocessor expansion. |
| 117 |
Use get_source_text() for that. */ |
| 118 |
static StringRef Get_Source_Text_Raw(const SourceRange &range); |
| 119 |
|
| 120 |
/** Check if SourceLocation a is located before than b in the SourceCode. */ |
| 121 |
static bool Is_Before(const SourceLocation &a, const SourceLocation &b); |
| 122 |
|
| 123 |
/** Check if SourceLocation a is located after than b in the SourceCode. */ |
| 124 |
static bool Is_After(const SourceLocation &a, const SourceLocation &b); |
| 125 |
|
| 126 |
/** Set output to file. */ |
| 127 |
static void Set_Output_To(const std::string &path); |
| 128 |
|
| 129 |
static StringRef Get_Filename_From_Loc(const SourceLocation &loc); |
| 130 |
|
| 131 |
static SourceLocation Get_Expanded_Loc(Decl *decl); |
| 132 |
|
| 133 |
static OptionalFileEntryRef Get_FileEntry(const SourceLocation &loc); |
| 134 |
|
| 135 |
/* This class can not be initialized. */ |
| 136 |
PrettyPrint() = delete; |
| 137 |
|
| 138 |
private: |
| 139 |
|
| 140 |
static bool Is_Range_Valid(const SourceRange &loc); |
| 141 |
|
| 142 |
/** Output object to where this class will output to. Current default is the |
| 143 |
same as llvm::outs(). */ |
| 144 |
static raw_ostream *Out; |
| 145 |
|
| 146 |
/** Language options used by clang's internal PrettyPrinter. We use the |
| 147 |
default options for now. */ |
| 148 |
static LangOptions LangOpts; |
| 149 |
|
| 150 |
/** Policy for printing. We use the default for now. */ |
| 151 |
static PrintingPolicy PPolicy; |
| 152 |
|
| 153 |
/** ASTUnit object. Must be set after constructing the ast by |
| 154 |
calling Set_Source_Manager. FIXME: This should not be a statc |
| 155 |
class variable. Refactor this class entirely. */ |
| 156 |
static ASTUnit *AST; |
| 157 |
|
| 158 |
friend class RecursivePrint; |
| 159 |
}; |
| 160 |
|
| 161 |
/** Since writing PrettyPrint::Print_Decl can be bothering and result in |
| 162 |
unecessary long code, we also provide those non-namespaced functions. */ |
| 163 |
static inline void Print_Decl(Decl *decl) |
| 164 |
{ |
| 165 |
PrettyPrint::Print_Decl(decl); |
| 166 |
} |
| 167 |
|
| 168 |
static inline void Print_Stmt(Stmt *stmt) |
| 169 |
{ |
| 170 |
PrettyPrint::Print_Stmt(stmt); |
| 171 |
} |
| 172 |
|
| 173 |
static inline bool Is_Loc_Before(const SourceLocation &a, const SourceLocation &b) |
| 174 |
{ |
| 175 |
return PrettyPrint::Is_Before(a, b); |
| 176 |
} |
| 177 |
|
| 178 |
class RecursivePrint |
| 179 |
{ |
| 180 |
public: |
| 181 |
RecursivePrint(ASTUnit *ast, |
| 182 |
std::unordered_set<Decl *> &deps, |
| 183 |
IncludeTree &it, |
| 184 |
bool keep_includes); |
| 185 |
|
| 186 |
/* Print output to `Out`. */ |
| 187 |
void Print(void); |
| 188 |
|
| 189 |
/** Print single decl. */ |
| 190 |
void Print_Decl(Decl *decl); |
| 191 |
|
| 192 |
/** Print a Macro Defintion into ostream `Out`. */ |
| 193 |
void Print_Macro(MacroDefinitionRecord *rec); |
| 194 |
|
| 195 |
void Print_Preprocessed(PreprocessedEntity *prep); |
| 196 |
|
| 197 |
/** Print a Macro Undef into ostream `Out`. */ |
| 198 |
void Print_Macro_Undef(MacroDirective *directive); |
| 199 |
|
| 200 |
protected: |
| 201 |
|
| 202 |
/** Remove decls and macros that are already covered by includes that are |
| 203 |
marked for output. */ |
| 204 |
void Analyze_Includes(void); |
| 205 |
|
| 206 |
/** Check if a given declaration was already marked as dependency. */ |
| 207 |
inline bool Is_Decl_Marked(Decl *decl) |
| 208 |
{ return Decl_Deps.find(decl) != Decl_Deps.end(); } |
| 209 |
|
| 210 |
/** Determine if a macro that are marked for output. */ |
| 211 |
inline bool Is_Macro_Marked(MacroInfo *x) |
| 212 |
{ return x && x->isUsed(); } |
| 213 |
|
| 214 |
inline void Unmark_Decl(Decl *decl) |
| 215 |
{ Decl_Deps.erase(decl); } |
| 216 |
|
| 217 |
inline void Unmark_Macro(MacroInfo *x) |
| 218 |
{ x->setIsUsed(false); } |
| 219 |
|
| 220 |
ASTUnit *AST; |
| 221 |
TopLevelASTIterator ASTIterator; |
| 222 |
MacroWalker MW; |
| 223 |
std::unordered_set<Decl *> &Decl_Deps; |
| 224 |
IncludeTree &IT; |
| 225 |
bool KeepIncludes; |
| 226 |
|
| 227 |
/* Vector of MacroDirective of macros that needs to be undefined somewhere in |
| 228 |
the code. */ |
| 229 |
std::vector<MacroDirective*> NeedsUndef; |
| 230 |
}; |