-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new option to create a header with foward declaration of all exte…
…rnalized functions This commit adds a way for clang-extract to generate a header file containing foward declarations of all externalized functions. This can be enabled by using -DCE_OUTPUT_FUNCTION_PROTOTYPE_HEADER=<arg>. The generated file is not self-compilable. Signed-off-by: Giuliano Belinassi <[email protected]>
- Loading branch information
1 parent
db616a3
commit cc3166e
Showing
9 changed files
with
186 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
//===- HeaderGenerate.hh - Output a header file for generated output. *- C++ -*-===// | ||
// | ||
// This project is licensed under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
/// \file | ||
/// Remove the body of all functions and only outputs the foward declaration. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
/* Author: Giuliano Belinassi */ | ||
|
||
#include "HeaderGenerate.hh" | ||
#include "FunctionDepsFinder.hh" | ||
#include "IncludeTree.hh" | ||
#include "PrettyPrint.hh" | ||
|
||
HeaderGeneration::HeaderGeneration(PassManager::Context *ctx) | ||
: AST(ctx->AST.get()) | ||
{ | ||
Run_Analysis(ctx->NamesLog); | ||
} | ||
|
||
void HeaderGeneration::Print(void) | ||
{ | ||
IncludeTree IT(AST); // Not necessary, just to use the RecursivePrint class. | ||
RecursivePrint(AST, Closure.Get_Set(), IT, false).Print(); | ||
} | ||
|
||
static bool Contains(const std::vector<ExternalizerLogEntry> &v, const std::string &name) | ||
{ | ||
for (const ExternalizerLogEntry &x : v) { | ||
if (x.NewName == name) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
|
||
bool HeaderGeneration::Run_Analysis(const std::vector<ExternalizerLogEntry> &set) | ||
{ | ||
ASTUnit::top_level_iterator it; | ||
for (it = AST->top_level_begin(); it != AST->top_level_end(); ++it) { | ||
Decl *decl = *it; | ||
|
||
if (FunctionDecl *fdecl = dyn_cast<FunctionDecl>(decl)) { | ||
if (Contains(set, fdecl->getNameAsString())) { | ||
if (fdecl->hasBody()) { | ||
Stmt *body = fdecl->getBody(); | ||
fdecl->setRangeEnd(body->getBeginLoc().getLocWithOffset(-1)); | ||
fdecl->setBody(nullptr); | ||
} | ||
Closure.Add_Single_Decl(fdecl); | ||
} | ||
} | ||
} | ||
|
||
/* Do not output any macros. */ | ||
MacroWalker mw(AST->getPreprocessor()); | ||
PreprocessingRecord *rec = AST->getPreprocessor().getPreprocessingRecord(); | ||
for (PreprocessedEntity *entity : *rec) { | ||
if (MacroDefinitionRecord *def = dyn_cast<MacroDefinitionRecord>(entity)) { | ||
if (MacroInfo *info = mw.Get_Macro_Info(def)) { | ||
info->setIsUsed(false); | ||
} | ||
} | ||
} | ||
|
||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
//===- HeaderGenerate.hh - Output a header file for generated output. *- C++ -*-===// | ||
// | ||
// This project is licensed under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
/// \file | ||
/// Remove the body of all functions and only outputs the foward declaration. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
/* Author: Giuliano Belinassi */ | ||
|
||
#pragma once | ||
|
||
#include "Passes.hh" | ||
#include "FunctionDepsFinder.hh" | ||
#include "SymbolExternalizer.hh" | ||
|
||
|
||
using namespace clang; | ||
|
||
/** Outputs a header file with a foward declarations of all functions in the current | ||
* AST. | ||
* | ||
* WARNING: This class modifies the AST. | ||
*/ | ||
class HeaderGeneration | ||
{ | ||
public: | ||
HeaderGeneration(PassManager::Context *); | ||
|
||
bool Run_Analysis(const std::vector<ExternalizerLogEntry> &set); | ||
|
||
void Print(void); | ||
|
||
protected: | ||
ASTUnit *AST; | ||
ClosureSet Closure; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters