Debian Codegraph @

libcextract/HeaderGenerate.cpp

raw
1 //===- HeaderGenerate.hh - Output a header file for generated output. *- 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 /// Remove the body of all functions and only outputs the foward declaration.
11 //
12 //===----------------------------------------------------------------------===//
13
14 /* Author: Giuliano Belinassi */
15
16 #include "HeaderGenerate.hh"
17 #include "FunctionDepsFinder.hh"
18 #include "IncludeTree.hh"
19 #include "PrettyPrint.hh"
20
21 HeaderGeneration::HeaderGeneration(PassManager::Context *ctx)
22 : AST(ctx->AST.get())
23 {
24 Run_Analysis(ctx->NamesLog);
25 }
26
27 void HeaderGeneration::Print(void)
28 {
29 IncludeTree IT(AST); // Not necessary, just to use the RecursivePrint class.
30 RecursivePrint(AST, Closure.Get_Set(), IT, false).Print();
31 }
32
33 static bool Contains(const std::vector<ExternalizerLogEntry> &v, const std::string &name)
34 {
35 for (const ExternalizerLogEntry &x : v) {
36 if ((x.Type == ExternalizationType::RENAME || x.Type == ExternalizationType::WEAK)
37 && x.NewName == name) {
38 return true;
39 }
40 }
41
42 return false;
43 }
44
45 bool HeaderGeneration::Run_Analysis(const std::vector<ExternalizerLogEntry> &set)
46 {
47 ASTUnit::top_level_iterator it;
48 std::unordered_set<std::string> nameset;
49
50 for (it = AST->top_level_begin(); it != AST->top_level_end(); ++it) {
51 Decl *decl = *it;
52
53 if (FunctionDecl *fdecl = dyn_cast<FunctionDecl>(decl)) {
54 const std::string &fname = fdecl->getNameAsString();
55 /* If the function was already issued, then do not issue it again. */
56 if (Contains(set, fname) && nameset.find(fname) == nameset.end()) {
57 if (fdecl->doesThisDeclarationHaveABody() && fdecl->hasBody()) {
58 Stmt *body = fdecl->getBody();
59 fdecl->setRangeEnd(body->getBeginLoc().getLocWithOffset(-1));
60 fdecl->setBody(nullptr);
61 }
62 Closure.Add_Single_Decl(fdecl);
63 nameset.insert(fname);
64 }
65 }
66 }
67
68 /* Do not output any macros. */
69 MacroWalker mw(AST->getPreprocessor());
70 PreprocessingRecord *rec = AST->getPreprocessor().getPreprocessingRecord();
71 for (PreprocessedEntity *entity : *rec) {
72 if (MacroDefinitionRecord *def = dyn_cast<MacroDefinitionRecord>(entity)) {
73 if (MacroInfo *info = mw.Get_Macro_Info(def)) {
74 info->setIsUsed(false);
75 }
76 }
77 }
78
79 return true;
80 }