|
| 1 | +//===- TreeDump.cpp - Dump IncludeTree to stdout --*- 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 | +/// Entrypoint of the IncludeTree dump tool. |
| 11 | +// |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | + |
| 14 | +/* Author: Giuliano Belinassi */ |
| 15 | + |
| 16 | +#include "ArgvParser.hh" |
| 17 | +#include "Passes.hh" |
| 18 | +#include "Error.hh" |
| 19 | + |
| 20 | +#include <iostream> |
| 21 | +#include <stdio.h> |
| 22 | + |
| 23 | +using namespace llvm; |
| 24 | +using namespace clang; |
| 25 | + |
| 26 | +void print_usage_message(void) |
| 27 | +{ |
| 28 | + llvm::outs() << |
| 29 | +"OVERVIEW: Tool to show the tree of includes of a C/C++ file.\n" |
| 30 | +" It should be invoked as a C/C++ compiler.\n" |
| 31 | +"\n" |
| 32 | +"USAGE: ce-includetree [options] file...\n" |
| 33 | +"\n" |
| 34 | +"CLANG-EXTRACT OPTIONS:\n" |
| 35 | +" <clang-switch> A clang switch, as specified by calling clang --help.\n" |
| 36 | +" -D__KERNEL__ Indicate that we are processing a Linux sourcefile.\n" |
| 37 | +" -DCE_KEEP_INCLUDES Keep all possible #include<file> directives.\n" |
| 38 | +" -DCE_KEEP_INCLUDES=<policy>\n" |
| 39 | +" Keep all possible #include<file> directives, but using the\n" |
| 40 | +" specified include expansion <policy>. Valid values are\n" |
| 41 | +" nothing, everything, kernel, system and compiler.\n" |
| 42 | +" -DCE_EXPAND_INCLUDES=<args>\n" |
| 43 | +" Force expansion of the headers provided in <args>.\n" |
| 44 | +" -DCE_NOT_EXPAND_INCLUDES=<args>\n" |
| 45 | +" Force the following headers to NOT be expanded.\n" |
| 46 | +"\n"; |
| 47 | +} |
| 48 | + |
| 49 | +int main(int argc, char **argv) |
| 50 | +{ |
| 51 | + ArgvParser args(argc, argv); |
| 52 | + PassManager::Context ctx(args); |
| 53 | + |
| 54 | + if (argc <= 1) { |
| 55 | + print_usage_message(); |
| 56 | + return 0; |
| 57 | + } |
| 58 | + |
| 59 | + /* Cleverly silence any error from clang. */ |
| 60 | + fclose(stderr); |
| 61 | + if (Build_ASTUnit(&ctx) == false){ |
| 62 | + llvm::outs() << "Unable to create ASTUnit of " << ctx.InputPath << '\n'; |
| 63 | + return 1; |
| 64 | + } |
| 65 | + |
| 66 | + const DiagnosticsEngine &de = ctx.AST->getDiagnostics(); |
| 67 | + if (de.hasErrorOccurred()) { |
| 68 | + llvm::outs() << "ASTUnit of " << ctx.InputPath << " contain errors. Aborting.\n"; |
| 69 | + return 1; |
| 70 | + } |
| 71 | + |
| 72 | + IncludeTree IT(ctx.AST.get(), ctx.IncExpansionPolicy, ctx.HeadersToExpand, |
| 73 | + ctx.HeadersToNotExpand); |
| 74 | + IT.Dump(llvm::outs()); |
| 75 | + |
| 76 | + return 0; |
| 77 | +} |
0 commit comments