| 1 |
//===- PrettyPrint.cpp - 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 |
#include "PrettyPrint.hh" |
| 17 |
#include "ClangCompat.hh" |
| 18 |
#include "TopLevelASTIterator.hh" |
| 19 |
#include "NonLLVMMisc.hh" |
| 20 |
#include "LLVMMisc.hh" |
| 21 |
|
| 22 |
#include <clang/AST/Attr.h> |
| 23 |
#include <llvm/Support/Regex.h> |
| 24 |
|
| 25 |
/** Public methods. */ |
| 26 |
|
| 27 |
#define Out (*Out) |
| 28 |
|
| 29 |
/* Workarround LLVM bug #162126 (CE bug #146)*/ |
| 30 |
static void Attr_Order_Fix(Decl *decl) |
| 31 |
{ |
| 32 |
/* This only seems to affect variables, if its not the case then quickly |
| 33 |
return. */ |
| 34 |
VarDecl *vdecl = dyn_cast<VarDecl>(decl); |
| 35 |
if (vdecl == nullptr) |
| 36 |
return; |
| 37 |
|
| 38 |
if (decl->hasAttrs()) { |
| 39 |
AttrVec &Attrs = decl->getAttrs(); |
| 40 |
|
| 41 |
/* Clang always seems to insert the `asm` label last, but lets just swap |
| 42 |
with the first one. */ |
| 43 |
for (unsigned i = 0; i < Attrs.size(); ++i) { |
| 44 |
if (isa<AsmLabelAttr>(Attrs[i])) { |
| 45 |
auto temp = Attrs[i]; |
| 46 |
Attrs[i] = Attrs[0]; |
| 47 |
Attrs[0] = temp; |
| 48 |
break; |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
void PrettyPrint::Print_Decl(Decl *decl, bool keep_includes) |
| 55 |
{ |
| 56 |
/* When handling C code, we need to check if given declaration is a function |
| 57 |
with body. If yes, we can simply print the declaration, but otherwise |
| 58 |
we need to manually insert the end of statement ';' token. */ |
| 59 |
|
| 60 |
FunctionDecl *f = dyn_cast<FunctionDecl>(decl); |
| 61 |
TagDecl *t = dyn_cast<TagDecl>(decl); |
| 62 |
EnumDecl *e = dyn_cast<EnumDecl>(decl); |
| 63 |
|
| 64 |
if (f && f->hasBody() && f->isThisDeclarationADefinition()) { |
| 65 |
Print_Decl_Raw(f); |
| 66 |
Out << "\n\n"; |
| 67 |
} else if (!e && t && t->getName() == "") { |
| 68 |
/* If the RecordType doesn't have a name, then don't print it. Except when |
| 69 |
it is an empty named enum declaration, which in this case we must print |
| 70 |
because it contains declared constants. */ |
| 71 |
} else { |
| 72 |
/* Structs, enums, and prototypes */ |
| 73 |
|
| 74 |
/** Check if the output string has an include directive. In that case, if we |
| 75 |
don't want to keep the includes, we must print the ast dump rather than |
| 76 |
the source text to get rid of the #include directive. |
| 77 |
FIXME: regexes are slow. */ |
| 78 |
static llvm::Regex regex("# *include *(<|\")"); |
| 79 |
|
| 80 |
/* FIXME: Why isCompleteDefinitionRequired does not work for EnumDecls? */ |
| 81 |
if (t && ((!e && t->isCompleteDefinitionRequired() == false) |
| 82 |
|| (!keep_includes && regex.match(Get_Source_Text(t->getSourceRange()))))) { |
| 83 |
|
| 84 |
/* FIXME: The Print_Decl_Raw class will attempt to write this declaration |
| 85 |
as the user wrote, that means WITH a body. To avoid this, we set |
| 86 |
the StartLocation to equal the End of location, which will trigger |
| 87 |
the "clang got confused" mechanism in PrettyPrint and force it to |
| 88 |
be output as a tree dump instead of what the user wrote. The |
| 89 |
correct way of doing this would be update the source location to |
| 90 |
the correct range. */ |
| 91 |
t->setLocStart(t->getEndLoc()); |
| 92 |
} |
| 93 |
|
| 94 |
/** Check if we can get a partial declaration of `decl` rather than a full |
| 95 |
declaration. */ |
| 96 |
bool full_def_removed = false; |
| 97 |
/* FIXME: Why isCompleteDefinitionRequired does not work for EnumDecls? */ |
| 98 |
if (t && !e && t->isCompleteDefinitionRequired() == false) { |
| 99 |
/* We don't need the full defintion. Hide the body for Print_Decl_Raw. */ |
| 100 |
t->setCompleteDefinition(false); |
| 101 |
full_def_removed = true; |
| 102 |
} |
| 103 |
|
| 104 |
Print_Decl_Raw(decl); |
| 105 |
Out << ';'; |
| 106 |
if (full_def_removed) { |
| 107 |
Out << "/* Full definition was removed. */"; |
| 108 |
} |
| 109 |
Out << "\n\n"; |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
bool PrettyPrint::Is_Range_Valid(const SourceRange &range) |
| 114 |
{ |
| 115 |
const SourceLocation begin = range.getBegin(); |
| 116 |
const SourceLocation end = range.getEnd(); |
| 117 |
|
| 118 |
if (!begin.isValid() || !end.isValid()) |
| 119 |
return false; |
| 120 |
|
| 121 |
return Is_Before(begin, end) && (begin != end); |
| 122 |
} |
| 123 |
|
| 124 |
bool Has_NoInline_Attr(Decl *decl) |
| 125 |
{ |
| 126 |
const AttrVec &attrs = decl->getAttrs(); |
| 127 |
for (const Attr *attr : attrs) { |
| 128 |
if (attr->getKind() == attr::NoInline) { |
| 129 |
return true; |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
return false; |
| 134 |
} |
| 135 |
|
| 136 |
void PrettyPrint::Print_Decl_Raw(Decl *decl) |
| 137 |
{ |
| 138 |
SourceRange decl_range = decl->getSourceRange(); |
| 139 |
|
| 140 |
/* If a SourceManager was specified and the decl source range seems valid, |
| 141 |
then output based on the original source code. */ |
| 142 |
if (Is_Range_Valid(decl_range)) { |
| 143 |
|
| 144 |
/* The range given by getSourceRange ignores attributes. Example : |
| 145 |
|
| 146 |
int a __attribute__((aligned (8))); |
| 147 |
|
| 148 |
outputs: |
| 149 |
|
| 150 |
int a; |
| 151 |
|
| 152 |
Therefore we must check for the attributes of this declaration and compute |
| 153 |
the furthest location. */ |
| 154 |
|
| 155 |
SourceLocation furthest = PrettyPrint::Get_Expanded_Loc(decl); |
| 156 |
decl_range = SourceRange(decl_range.getBegin(), furthest); |
| 157 |
StringRef decl_source = Get_Source_Text(decl_range); |
| 158 |
|
| 159 |
/* If for some reason Get_Source_Text is unable to find the source range |
| 160 |
which comes this declaration because it is very complex, then fall back |
| 161 |
to AST dump. */ |
| 162 |
if (decl_source == "") { |
| 163 |
/* TODO: warn user that we had to fallback to AST dump. */ |
| 164 |
|
| 165 |
if (TypedefNameDecl *typedecl = dyn_cast<TypedefNameDecl>(decl)) { |
| 166 |
/* The AST dump will hang on the following case: |
| 167 |
|
| 168 |
typedef struct { |
| 169 |
attrs |
| 170 |
} Type; |
| 171 |
|
| 172 |
That is because clang decompose such constructions in |
| 173 |
|
| 174 |
struct { |
| 175 |
attrs |
| 176 |
}; |
| 177 |
|
| 178 |
typedef struct Type Type; |
| 179 |
|
| 180 |
but since the struct has no name, Type goes undefined. |
| 181 |
|
| 182 |
The way we fix that is output the typedef and the TagDecl |
| 183 |
body, and we avoid outputs of unamed structs. |
| 184 |
|
| 185 |
This problem does not seem to show up if we are printing |
| 186 |
from source location. */ |
| 187 |
TagDecl *tagdecl = typedecl->getAnonDeclWithTypedefName(); |
| 188 |
if (tagdecl && tagdecl->getName() == "") { |
| 189 |
Out << "typedef "; |
| 190 |
tagdecl->getDefinition()->print(Out, PPolicy); |
| 191 |
Out << " " << typedecl->getName(); |
| 192 |
} else { |
| 193 |
Attr_Order_Fix(decl); |
| 194 |
decl->print(Out, PPolicy); |
| 195 |
} |
| 196 |
} else { |
| 197 |
MacroInfo *noinline_info = nullptr; |
| 198 |
if (Has_NoInline_Attr(decl)) { |
| 199 |
/* If the code has a `noinline` macro defined, it conflicts with |
| 200 |
clang's __attribute__((noinline)) attribute dump. Hence we |
| 201 |
have to undef it. */ |
| 202 |
Preprocessor &pp = AST->getPreprocessor(); |
| 203 |
MacroWalker MW(pp); |
| 204 |
|
| 205 |
/* Get the valid macro at the source location. */ |
| 206 |
IdentifierInfo *noinline = pp.getIdentifierInfo("noinline"); |
| 207 |
noinline_info = MW.Get_Macro_Info(noinline, decl->getBeginLoc()); |
| 208 |
/* Only print it if the noinline macro is defined at location. */ |
| 209 |
if (noinline_info) { |
| 210 |
Out << "#undef noinline\n"; |
| 211 |
} |
| 212 |
} |
| 213 |
Attr_Order_Fix(decl); |
| 214 |
decl->print(Out, LangOpts); |
| 215 |
if (noinline_info) { |
| 216 |
/* Redeclare the macro to the previous value. */ |
| 217 |
PrettyPrint::Print_MacroInfo(noinline_info); |
| 218 |
} |
| 219 |
} |
| 220 |
} else { |
| 221 |
/* Check if the output string has a balanced number of ifdefs. This is |
| 222 |
required because of the following construct from glibc: |
| 223 |
|
| 224 |
#ifdef !SHARED |
| 225 |
#else |
| 226 |
struct rtld_global_ro { |
| 227 |
#endif |
| 228 |
|
| 229 |
In case we want to print rtld_global_ro, PrettyPrint will get the text |
| 230 |
from struct rtld_global_ro and below, which would only include the |
| 231 |
#endif, and not the #ifdef. In case its not balanced we fallback to |
| 232 |
AST dumping. */ |
| 233 |
if (Has_Balanced_Ifdef(decl_source)) { |
| 234 |
Out << decl_source; |
| 235 |
} else { |
| 236 |
/* In case its not balanced, fallback to AST dump. */ |
| 237 |
Attr_Order_Fix(decl); |
| 238 |
decl->print(Out, LangOpts); |
| 239 |
} |
| 240 |
} |
| 241 |
} else { |
| 242 |
/* Else, we fallback to AST Dumping. */ |
| 243 |
Attr_Order_Fix(decl); |
| 244 |
decl->print(Out, LangOpts); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
void PrettyPrint::Debug_Decl(Decl *decl) |
| 249 |
{ |
| 250 |
#undef Out |
| 251 |
auto o = Out; |
| 252 |
Out = &llvm::outs(); |
| 253 |
Print_Decl(decl);; |
| 254 |
Out = o; |
| 255 |
#define Out (*Out) |
| 256 |
} |
| 257 |
|
| 258 |
void PrettyPrint::Debug_Stmt(Stmt *stmt) |
| 259 |
{ |
| 260 |
stmt->printPretty(llvm::outs(), nullptr, PPolicy); |
| 261 |
} |
| 262 |
|
| 263 |
void PrettyPrint::Print_Stmt(Stmt *stmt) |
| 264 |
{ |
| 265 |
/* Currently only used for debugging. */ |
| 266 |
stmt->printPretty(Out, nullptr, PPolicy); |
| 267 |
Out << "\n"; |
| 268 |
} |
| 269 |
|
| 270 |
void PrettyPrint::Print_Macro_Def(MacroDefinitionRecord *rec) |
| 271 |
{ |
| 272 |
Out << "#define " << Get_Source_Text(rec->getSourceRange()) << "\n"; |
| 273 |
} |
| 274 |
|
| 275 |
void PrettyPrint::Debug_Macro_Def(MacroDefinitionRecord *rec) |
| 276 |
{ |
| 277 |
llvm::outs() << "#define " << Get_Source_Text(rec->getSourceRange()) << "\n"; |
| 278 |
} |
| 279 |
|
| 280 |
void PrettyPrint::Print_Macro_Undef(MacroDirective *directive) |
| 281 |
{ |
| 282 |
SourceLocation loc = directive->getDefinition().getUndefLocation(); |
| 283 |
assert(loc.isValid() && "Undefine location is invalid"); |
| 284 |
Out << "#undef " << Get_Source_Text(loc) << '\n'; |
| 285 |
} |
| 286 |
|
| 287 |
void PrettyPrint::Debug_Macro_Undef(MacroDirective *directive) |
| 288 |
{ |
| 289 |
SourceLocation loc = directive->getDefinition().getUndefLocation(); |
| 290 |
assert(loc.isValid() && "Undefine location is invalid"); |
| 291 |
llvm::outs() << "#undef " << Get_Source_Text(loc) << '\n'; |
| 292 |
} |
| 293 |
|
| 294 |
void PrettyPrint::Print_InclusionDirective(InclusionDirective *include) |
| 295 |
{ |
| 296 |
Out << Get_Source_Text(include->getSourceRange()) << '\n'; |
| 297 |
} |
| 298 |
|
| 299 |
void PrettyPrint::Debug_InclusionDirective(InclusionDirective *include) |
| 300 |
{ |
| 301 |
llvm::outs() << Get_Source_Text(include->getSourceRange()) << '\n'; |
| 302 |
} |
| 303 |
|
| 304 |
void PrettyPrint::Print_MacroInfo(MacroInfo *info) |
| 305 |
{ |
| 306 |
SourceRange range(info->getDefinitionLoc(), info->getDefinitionEndLoc()); |
| 307 |
Out << "#define " << Get_Source_Text(range) << '\n'; |
| 308 |
} |
| 309 |
|
| 310 |
void PrettyPrint::Print_Attr(Attr *attr) |
| 311 |
{ |
| 312 |
attr->printPretty(llvm::outs(), PPolicy); |
| 313 |
llvm::outs() << '\n'; |
| 314 |
} |
| 315 |
|
| 316 |
void PrettyPrint::Print_Comment(const std::string &comment) |
| 317 |
{ |
| 318 |
Out << "/** " << comment << " */\n"; |
| 319 |
} |
| 320 |
|
| 321 |
void PrettyPrint::Print_Raw(const std::string &string) |
| 322 |
{ |
| 323 |
Out << string; |
| 324 |
} |
| 325 |
|
| 326 |
void PrettyPrint::Print_RawComment(SourceManager &sm, RawComment *comment) |
| 327 |
{ |
| 328 |
Out << comment->getRawText(sm) << '\n'; |
| 329 |
} |
| 330 |
|
| 331 |
/** Private stuff. */ |
| 332 |
|
| 333 |
StringRef PrettyPrint::Get_Source_Text(const SourceRange &range) |
| 334 |
{ |
| 335 |
// NOTE: sm.getSpellingLoc() used in case the range corresponds to a macro/preprocessed source. |
| 336 |
// NOTE2: getSpellingLoc() breaks in the case where a macro was asigned to be expanded to typedef. |
| 337 |
SourceManager &SM = AST->getSourceManager(); |
| 338 |
auto start_loc = range.getBegin();//SM->getSpellingLoc(range.getBegin()); |
| 339 |
auto last_token_loc = range.getEnd();//SM->getSpellingLoc(range.getEnd()); |
| 340 |
auto end_loc = clang::Lexer::getLocForEndOfToken(last_token_loc, 0, SM, LangOpts); |
| 341 |
auto printable_range = clang::SourceRange{start_loc, end_loc}; |
| 342 |
return Get_Source_Text_Raw(printable_range); |
| 343 |
} |
| 344 |
|
| 345 |
StringRef PrettyPrint::Get_Source_Text_Raw(const SourceRange &range) |
| 346 |
{ |
| 347 |
SourceManager &SM = AST->getSourceManager(); |
| 348 |
return clang::Lexer::getSourceText(CharSourceRange::getCharRange(range), SM, LangOpts); |
| 349 |
} |
| 350 |
|
| 351 |
/** Compare if SourceLocation a is before SourceLocation b in the source code. */ |
| 352 |
bool PrettyPrint::Is_Before(const SourceLocation &a, const SourceLocation &b) |
| 353 |
{ |
| 354 |
BeforeThanCompare<SourceLocation> is_before(AST->getSourceManager()); |
| 355 |
|
| 356 |
assert(a.isValid()); |
| 357 |
assert(b.isValid()); |
| 358 |
|
| 359 |
return is_before(a, b); |
| 360 |
} |
| 361 |
|
| 362 |
void PrettyPrint::Debug_SourceLoc(const SourceLocation &loc) |
| 363 |
{ |
| 364 |
loc.dump(AST->getSourceManager()); |
| 365 |
} |
| 366 |
|
| 367 |
bool PrettyPrint::Contains_From_LineCol(const SourceRange &a, const SourceRange &b) |
| 368 |
{ |
| 369 |
SourceManager &SM = AST->getSourceManager(); |
| 370 |
PresumedLoc a_begin = SM.getPresumedLoc(a.getBegin()); |
| 371 |
PresumedLoc a_end = SM.getPresumedLoc(a.getEnd()); |
| 372 |
PresumedLoc b_begin = SM.getPresumedLoc(b.getBegin()); |
| 373 |
PresumedLoc b_end = SM.getPresumedLoc(b.getEnd()); |
| 374 |
|
| 375 |
assert(a_begin.getFileID() == a_end.getFileID()); |
| 376 |
assert(b_begin.getFileID() == b_end.getFileID()); |
| 377 |
|
| 378 |
if (a_begin.getFileID() != b_begin.getFileID()) { |
| 379 |
/* Files are distinct, thus we can't easily determine which comes first. */ |
| 380 |
return false; |
| 381 |
} |
| 382 |
|
| 383 |
bool a_begin_smaller = false; |
| 384 |
bool b_end_smaller = false; |
| 385 |
|
| 386 |
if ((a_begin.getLine() < b_begin.getLine()) || |
| 387 |
(a_begin.getLine() == b_begin.getLine() && a_begin.getColumn() <= b_begin.getColumn())) { |
| 388 |
a_begin_smaller = true; |
| 389 |
} |
| 390 |
|
| 391 |
if ((b_end.getLine() < a_end.getLine()) || |
| 392 |
(b_end.getLine() == a_end.getLine() && b_end.getColumn() <= a_end.getColumn())) { |
| 393 |
b_end_smaller = true; |
| 394 |
} |
| 395 |
|
| 396 |
return a_begin_smaller && b_end_smaller; |
| 397 |
} |
| 398 |
|
| 399 |
bool PrettyPrint::Contains(const SourceRange &a, const SourceRange &b) |
| 400 |
{ |
| 401 |
if (a.fullyContains(b)) { |
| 402 |
return true; |
| 403 |
} |
| 404 |
|
| 405 |
return Contains_From_LineCol(a, b); |
| 406 |
} |
| 407 |
|
| 408 |
/** Compare if SourceLocation a is after SourceLocation b in the source code. */ |
| 409 |
bool PrettyPrint::Is_After(const SourceLocation &a, const SourceLocation &b) |
| 410 |
{ |
| 411 |
BeforeThanCompare<SourceLocation> is_before(AST->getSourceManager()); |
| 412 |
return is_before(b, a); |
| 413 |
} |
| 414 |
|
| 415 |
SourceLocation PrettyPrint::Get_Expanded_Loc(Decl *decl) |
| 416 |
{ |
| 417 |
SourceRange decl_range = decl->getSourceRange(); |
| 418 |
SourceLocation furthest = decl_range.getEnd(); |
| 419 |
|
| 420 |
/* If a SourceManager was specified and the decl source range seems valid, |
| 421 |
then output based on the original source code. */ |
| 422 |
if (Is_Range_Valid(decl_range)) { |
| 423 |
|
| 424 |
/* The range given by getSourceRange ignores attributes. Example : |
| 425 |
|
| 426 |
int a __attribute__((aligned (8))); |
| 427 |
|
| 428 |
outputs: |
| 429 |
|
| 430 |
int a; |
| 431 |
|
| 432 |
Therefore we must check for the attributes of this declaration and compute |
| 433 |
the furthest location. */ |
| 434 |
|
| 435 |
bool has_attr = false; |
| 436 |
SourceManager &SM = AST->getSourceManager(); |
| 437 |
|
| 438 |
/* Vector type attributes are not encoded in the AttrVec structure, hence |
| 439 |
we have to check for its existence and expand until we match the ';' |
| 440 |
token. See small/attr-11.c testcase. */ |
| 441 |
if (TypedefDecl *tdecl = dyn_cast<TypedefDecl>(decl)) { |
| 442 |
if (isa<clang::VectorType>(tdecl->getTypeSourceInfo()->getType().getTypePtr())) { |
| 443 |
has_attr = true; |
| 444 |
} |
| 445 |
} |
| 446 |
|
| 447 |
/* Get atttributes vector if it exists. */ |
| 448 |
if(decl->hasAttrs()) { |
| 449 |
AttrVec &attrvec = decl->getAttrs(); |
| 450 |
for (size_t i = 0; i < attrvec.size(); i++) { |
| 451 |
const Attr *attr = attrvec[i]; |
| 452 |
SourceLocation loc = attr->getRange().getEnd(); |
| 453 |
loc = SM.getExpansionLoc(loc); |
| 454 |
|
| 455 |
if (loc.isValid() && Is_Before(furthest, loc)) { |
| 456 |
furthest = loc; |
| 457 |
has_attr = true; |
| 458 |
} |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
/* Find the last ';' token, once there are attributes right after the last |
| 463 |
'}'. On __attribute__((unused)), the previous code reach until |
| 464 |
__attribute__((unused), ignoring the last parenthesis. On macros, like |
| 465 |
__aligned(32), the code would skip (32). */ |
| 466 |
if (has_attr) { |
| 467 |
tok::TokenKind semicolon(tok::semi); |
| 468 |
SourceLocation head = furthest; |
| 469 |
|
| 470 |
/* Keep fetching tokens. */ |
| 471 |
while (true) { |
| 472 |
auto maybe_next_tok = Lexer::findNextToken(head, SM, LangOpts); |
| 473 |
Token *tok = ClangCompat_GetTokenPtr(maybe_next_tok); |
| 474 |
|
| 475 |
if (tok == nullptr) { |
| 476 |
break; |
| 477 |
} |
| 478 |
|
| 479 |
head = tok->getLastLoc(); |
| 480 |
|
| 481 |
/* Stop when we find the ';' token. */ |
| 482 |
if (tok->is(semicolon)) { |
| 483 |
/* Found the ';', now go back a character to *not* include the ';'. */ |
| 484 |
furthest = head.getLocWithOffset(-1); |
| 485 |
|
| 486 |
/* There is a bizarre case where a decl have an #endif at the end of |
| 487 |
it, just before the ';' (see small/attr-14.c). Try to workaround |
| 488 |
this case by emiting the ';' that was there, which will result in |
| 489 |
two ';' being print. This is visually not cute, but at least |
| 490 |
won't cause the compiler to cry. */ |
| 491 |
StringRef endstr = PrettyPrint::Get_Source_Text({furthest, furthest}); |
| 492 |
if (const char *data_str = endstr.data()) { |
| 493 |
/* Check for newline. */ |
| 494 |
if (*data_str != '\0' && *data_str == '\n') { |
| 495 |
/* Set 'furthest' back to where it was. */ |
| 496 |
furthest = furthest.getLocWithOffset(1); |
| 497 |
} |
| 498 |
} |
| 499 |
break; |
| 500 |
} |
| 501 |
} |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
/* In case this decl is a TypedefNameDecl, we also need to check for the |
| 506 |
struct definition for extra attributes. See small/attr-10.c testscase |
| 507 |
for an example where this happens. */ |
| 508 |
if (TypedefNameDecl *typedecl = dyn_cast<TypedefNameDecl>(decl)) { |
| 509 |
if (TagDecl *tag = typedecl->getAnonDeclWithTypedefName()) { |
| 510 |
SourceLocation tag_furthest = PrettyPrint::Get_Expanded_Loc(tag); |
| 511 |
if (Is_Before(furthest, tag_furthest)) { |
| 512 |
furthest = tag_furthest; |
| 513 |
} |
| 514 |
} |
| 515 |
} |
| 516 |
|
| 517 |
return furthest; |
| 518 |
} |
| 519 |
|
| 520 |
#undef Out |
| 521 |
|
| 522 |
/** Set output to file. */ |
| 523 |
void PrettyPrint::Set_Output_To(const std::string &path) |
| 524 |
{ |
| 525 |
std::error_code ec; |
| 526 |
static llvm::raw_fd_ostream out(path, ec); |
| 527 |
|
| 528 |
Set_Output_Ostream(&out); |
| 529 |
} |
| 530 |
|
| 531 |
StringRef PrettyPrint::Get_Filename_From_Loc(const SourceLocation &loc) |
| 532 |
{ |
| 533 |
return AST->getSourceManager().getFilename(loc); |
| 534 |
} |
| 535 |
|
| 536 |
OptionalFileEntryRef PrettyPrint::Get_FileEntry(const SourceLocation &loc) |
| 537 |
{ |
| 538 |
SourceManager &SM = AST->getSourceManager(); |
| 539 |
return SM.getFileEntryRefForID(SM.getFileID(loc)); |
| 540 |
} |
| 541 |
|
| 542 |
/* See PrettyPrint.hh for what they do. */ |
| 543 |
raw_ostream *PrettyPrint::Out = &llvm::outs(); |
| 544 |
LangOptions PrettyPrint::LangOpts; |
| 545 |
PrintingPolicy PrettyPrint::PPolicy(LangOpts); |
| 546 |
ASTUnit *PrettyPrint::AST; |
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
/** --- New RecursivePrint class code. */ |
| 551 |
|
| 552 |
RecursivePrint::RecursivePrint(ASTUnit *ast, |
| 553 |
std::unordered_set<Decl *> &deps, |
| 554 |
IncludeTree &it, |
| 555 |
bool keep_includes) |
| 556 |
: AST(ast), |
| 557 |
ASTIterator(ast, /*skip_macros_in_decl=*/false), |
| 558 |
MW(ast->getPreprocessor()), |
| 559 |
Decl_Deps(deps), |
| 560 |
IT(it), |
| 561 |
KeepIncludes(keep_includes) |
| 562 |
{ |
| 563 |
Analyze_Includes(); |
| 564 |
} |
| 565 |
|
| 566 |
void RecursivePrint::Analyze_Includes(void) |
| 567 |
{ |
| 568 |
/* If we do not want to keep the includes then quickly return. */ |
| 569 |
if (KeepIncludes == false) { |
| 570 |
return; |
| 571 |
} |
| 572 |
|
| 573 |
MacroWalker mw(AST->getPreprocessor()); |
| 574 |
|
| 575 |
/* Remove any macros that are provided by an include that should be output. */ |
| 576 |
PreprocessingRecord *rec = AST->getPreprocessor().getPreprocessingRecord(); |
| 577 |
for (PreprocessedEntity *entity : *rec) { |
| 578 |
if (MacroDefinitionRecord *def = dyn_cast<MacroDefinitionRecord>(entity)) { |
| 579 |
SourceLocation loc = def->getLocation(); |
| 580 |
if (IncludeNode *include = IT.Get(loc)) { |
| 581 |
if (include->Should_Be_Expanded() == false) { |
| 582 |
MacroDirective *macrodir = MW.Get_Macro_Directive(def); |
| 583 |
if (macrodir) { |
| 584 |
MacroInfo *macro = macrodir->getMacroInfo(); |
| 585 |
macro->setIsUsed(false); |
| 586 |
} |
| 587 |
} else { |
| 588 |
/* In the case the header should be expanded, insert the macro from HeaderGuard. */ |
| 589 |
MacroDefinitionRecord *guard = include->Get_HeaderGuard(); |
| 590 |
if (guard) { |
| 591 |
if (MacroInfo *guardinfo = mw.Get_Macro_Info(guard)) { |
| 592 |
guardinfo->setIsUsed(true); |
| 593 |
} |
| 594 |
} |
| 595 |
} |
| 596 |
} |
| 597 |
} |
| 598 |
} |
| 599 |
|
| 600 |
/* Remove any decls that are provided by an include that should be output. */ |
| 601 |
for (auto it = Decl_Deps.begin(); it != Decl_Deps.end();) { |
| 602 |
SourceLocation loc = (*it)->getLocation(); |
| 603 |
IncludeNode *include = IT.Get(loc); |
| 604 |
|
| 605 |
/* The location is covered by a include. Now check if this include is not |
| 606 |
marked for expansion. */ |
| 607 |
if (include != nullptr && include->Should_Be_Expanded() == false) { |
| 608 |
/* If not we can safely remove this decl. */ |
| 609 |
it = Decl_Deps.erase(it); |
| 610 |
} else { |
| 611 |
it++; |
| 612 |
} |
| 613 |
} |
| 614 |
} |
| 615 |
|
| 616 |
void RecursivePrint::Print_Preprocessed(PreprocessedEntity *prep) |
| 617 |
{ |
| 618 |
if (MacroDefinitionRecord *entity = dyn_cast<MacroDefinitionRecord>(prep)) { |
| 619 |
MacroInfo *info = MW.Get_Macro_Info(entity); |
| 620 |
if (Is_Macro_Marked(info) && !MW.Is_Builtin_Macro(info)) { |
| 621 |
PrettyPrint::Print_Macro_Def(entity); |
| 622 |
} |
| 623 |
|
| 624 |
return; |
| 625 |
} |
| 626 |
|
| 627 |
if (KeepIncludes) { |
| 628 |
if (InclusionDirective *inc = dyn_cast<InclusionDirective>(prep)) { |
| 629 |
IncludeNode *node = IT.Get(inc); |
| 630 |
if (node && node->Should_Be_Output()) { |
| 631 |
PrettyPrint::Print_InclusionDirective(inc); |
| 632 |
} |
| 633 |
|
| 634 |
return; |
| 635 |
} |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
void RecursivePrint::Print_Decl(Decl *decl) |
| 640 |
{ |
| 641 |
if (!Is_Decl_Marked(decl)) { |
| 642 |
return; |
| 643 |
} |
| 644 |
|
| 645 |
/* Handle namespaces. Namespace declaration can contain many functions |
| 646 |
that can be unused in the program. Hence we need to handle it |
| 647 |
carefully to remove what we don't need. */ |
| 648 |
if (NamespaceDecl *namespacedecl = dyn_cast<NamespaceDecl>(decl)) { |
| 649 |
if (namespacedecl->isInline()) { |
| 650 |
(*PrettyPrint::Out) << "inline "; |
| 651 |
} |
| 652 |
|
| 653 |
(*PrettyPrint::Out) <<"namespace " << namespacedecl->getName() << " {\n "; |
| 654 |
|
| 655 |
/* Iterate on each macro. */ |
| 656 |
for (auto child : namespacedecl->decls()) { |
| 657 |
Print_Decl(child); |
| 658 |
} |
| 659 |
(*PrettyPrint::Out) << "}\n"; |
| 660 |
} else { |
| 661 |
|
| 662 |
SourceManager &sm = AST->getSourceManager(); |
| 663 |
ASTContext &ctx = AST->getASTContext(); |
| 664 |
RawComment *comment = ctx.getRawCommentForDeclNoCache(decl); |
| 665 |
if (decl->getBeginLoc().isValid()) { |
| 666 |
if (!Have_Location_Comment(sm, comment)) { |
| 667 |
std::string comment = Build_CE_Location_Comment(sm, decl->getBeginLoc()); |
| 668 |
PrettyPrint::Print_Raw(comment); |
| 669 |
} else { |
| 670 |
/* Just output what it had. */ |
| 671 |
PrettyPrint::Print_RawComment(sm, comment); |
| 672 |
} |
| 673 |
} |
| 674 |
PrettyPrint::Print_Decl(decl, KeepIncludes); |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
void RecursivePrint::Print_Macro_Undef(MacroDirective *directive) |
| 679 |
{ |
| 680 |
const SourceLocation &undef_loc = directive->getDefinition().getUndefLocation(); |
| 681 |
|
| 682 |
/* In case the macro isn't undefined then its location is invalid. */ |
| 683 |
if (undef_loc.isValid()) { |
| 684 |
/* Check if the macro is marked for output. */ |
| 685 |
MacroInfo *info = directive->getMacroInfo(); |
| 686 |
if (Is_Macro_Marked(info)) { |
| 687 |
/* Check if the undef location actually points to an expanded include |
| 688 |
or the main file. */ |
| 689 |
if (KeepIncludes) { |
| 690 |
IncludeNode *node = IT.Get(undef_loc); |
| 691 |
if (KeepIncludes && node) { |
| 692 |
if (node->Should_Be_Expanded()) { |
| 693 |
PrettyPrint::Print_Macro_Undef(directive); |
| 694 |
} |
| 695 |
} |
| 696 |
} else { |
| 697 |
PrettyPrint::Print_Macro_Undef(directive); |
| 698 |
} |
| 699 |
} |
| 700 |
} |
| 701 |
} |
| 702 |
|
| 703 |
void RecursivePrint::Print(void) |
| 704 |
{ |
| 705 |
while (!ASTIterator.End()) { |
| 706 |
Decl *decl; |
| 707 |
|
| 708 |
switch ((*ASTIterator).Type) { |
| 709 |
case TopLevelASTIterator::ReturnType::TYPE_INVALID: |
| 710 |
assert(0 && "Invalid type in ASTIterator."); |
| 711 |
break; |
| 712 |
|
| 713 |
case TopLevelASTIterator::ReturnType::TYPE_DECL: |
| 714 |
decl = (*ASTIterator).AsDecl; |
| 715 |
Print_Decl(decl); |
| 716 |
++ASTIterator; |
| 717 |
ASTIterator.Skip_Until(decl->getEndLoc()); |
| 718 |
break; |
| 719 |
|
| 720 |
case TopLevelASTIterator::ReturnType::TYPE_PREPROCESSED_ENTITY: |
| 721 |
Print_Preprocessed((*ASTIterator).AsPrep); |
| 722 |
++ASTIterator; |
| 723 |
break; |
| 724 |
|
| 725 |
case TopLevelASTIterator::ReturnType::TYPE_MACRO_UNDEF: |
| 726 |
Print_Macro_Undef((*ASTIterator).AsUndef); |
| 727 |
++ASTIterator; |
| 728 |
break; |
| 729 |
} |
| 730 |
} |
| 731 |
} |