diff --git a/libcextract/Closure.hh b/libcextract/Closure.hh index e4a364c..e0953eb 100644 --- a/libcextract/Closure.hh +++ b/libcextract/Closure.hh @@ -132,10 +132,10 @@ class ClosureSet class DeclClosureVisitor : public RecursiveASTVisitor { public: - DeclClosureVisitor(ASTUnit *ast, EnumConstantTable &table) + DeclClosureVisitor(ASTUnit *ast) : RecursiveASTVisitor(), AST(ast), - EnumTable(table) + EnumTable(ast) { } @@ -520,7 +520,7 @@ class DeclClosureVisitor : public RecursiveASTVisitor std::unordered_set *matched_names = nullptr); private: - + /** The ASTUnit object. */ ASTUnit *AST; @@ -533,7 +533,7 @@ class DeclClosureVisitor : public RecursiveASTVisitor /** The table maping EnumConstantDecl to its original EnumDecl, used to find out where a certain EnumConstantDecl was defined. */ - EnumConstantTable &EnumTable; + EnumConstantTable EnumTable; /** The set of all analyzed Decls. */ std::unordered_set AnalyzedDecls; diff --git a/libcextract/FunctionDepsFinder.cpp b/libcextract/FunctionDepsFinder.cpp index 6f93755..c9fcabe 100644 --- a/libcextract/FunctionDepsFinder.cpp +++ b/libcextract/FunctionDepsFinder.cpp @@ -24,10 +24,9 @@ /** FunctionDependencyFinder class methods implementation. */ FunctionDependencyFinder::FunctionDependencyFinder(PassManager::Context *ctx) : AST(ctx->AST.get()), - EnumTable(AST), IT(AST, ctx->IncExpansionPolicy, ctx->HeadersToExpand), KeepIncludes(ctx->KeepIncludes), - Visitor(AST, EnumTable) + Visitor(AST) { } diff --git a/libcextract/FunctionDepsFinder.hh b/libcextract/FunctionDepsFinder.hh index 3f662cb..97bab8c 100644 --- a/libcextract/FunctionDepsFinder.hh +++ b/libcextract/FunctionDepsFinder.hh @@ -82,10 +82,6 @@ class FunctionDependencyFinder /** The AST that are being used in our analysis. */ ASTUnit* AST; - /** Object holding information about constant enum symbols and a mapping to - they original enum object. */ - EnumConstantTable EnumTable; - /* Tree of #includes. */ IncludeTree IT; diff --git a/libcextract/Passes.cpp b/libcextract/Passes.cpp index d347fc4..86361d6 100644 --- a/libcextract/Passes.cpp +++ b/libcextract/Passes.cpp @@ -434,7 +434,9 @@ class FunctionExternalizerPass : public Pass /* Issue externalization. */ SymbolExternalizer externalizer(ctx->AST.get(), ctx->IA, ctx->Ibt, ctx->AllowLateExternalizations, - ctx->PatchObject, ctx->DumpPasses); + ctx->PatchObject, + ctx->FuncExtractNames, + ctx->DumpPasses); if (ctx->RenameSymbols) /* The FuncExtractNames will be modified, as the function will be renamed. */ externalizer.Externalize_Symbols(ctx->Externalize, ctx->FuncExtractNames); diff --git a/libcextract/SymbolExternalizer.cpp b/libcextract/SymbolExternalizer.cpp index 0f82c1d..9171188 100644 --- a/libcextract/SymbolExternalizer.cpp +++ b/libcextract/SymbolExternalizer.cpp @@ -19,6 +19,7 @@ #include "ClangCompat.hh" #include "LLVMMisc.hh" #include "IntervalTree.hh" +#include "Closure.hh" #include #include @@ -205,9 +206,20 @@ class ExternalizerVisitor: public RecursiveASTVisitor if (sym == nullptr || !sym->Done) return VISITOR_CONTINUE; - /* If this is the first use of the symbol, then remember it. */ + /* Get the first effective use, which means an DeclRefExpr of a decl that + will not be removed by the Closure. */ if (sym->FirstUse == nullptr) { - sym->FirstUse = expr; + ClosureSet &closure = SE.ClosureVisitor.Get_Closure(); + ASTUnit *ast = SE.AST; + SourceManager &sm = ast->getSourceManager(); + SourceLocation loc = sm.getExpansionLoc(expr->getLocation()); + Decl *topdecl = Get_Toplevel_Decl_At_Location(ast, loc); + + /* If the declaration is reachable from the functions we want to extract, + then we mark it as FirstUse. */ + if (closure.Is_Decl_Marked(topdecl)) { + sym->FirstUse = expr; + } } /* We must be careful to ensure that the reference we got is actually @@ -787,6 +799,12 @@ void SymbolExternalizer::Compute_SymbolsMap_Late_Insert_Locations(std::vectorFirstUse; + + /* In case FirstUse is nullptr, then there is nothing to externalize + because it will be removed by the ClosurePass. */ + if (first_use == nullptr) + continue; + SourceLocation loc_1stuse = SM.getExpansionLoc(first_use->getLocation()); Decl *topdecl = Get_Toplevel_Decl_At_Location(AST, loc_1stuse); diff --git a/libcextract/SymbolExternalizer.hh b/libcextract/SymbolExternalizer.hh index c71efcd..fff6ec2 100644 --- a/libcextract/SymbolExternalizer.hh +++ b/libcextract/SymbolExternalizer.hh @@ -17,6 +17,7 @@ #include "MacroWalker.hh" #include "InlineAnalysis.hh" +#include "Closure.hh" #include #include @@ -96,7 +97,7 @@ struct SymbolUpdateStatus attempt of externalize an symbol which was removed by the ClosurePass. */ inline bool Is_Used(void) { - return OldDecl && NewDecl; + return OldDecl && NewDecl && FirstUse; } void Dump(SourceManager &SM) @@ -284,6 +285,7 @@ class SymbolExternalizer public: SymbolExternalizer(ASTUnit *ast, InlineAnalysis &ia, bool ibt, bool allow_late_externalize, std::string patch_object, + const std::vector &functions_to_extract, bool dump = false) : AST(ast), MW(ast->getPreprocessor()), @@ -292,8 +294,10 @@ class SymbolExternalizer Ibt(ibt), AllowLateExternalization(allow_late_externalize), PatchObject(patch_object), - SymbolsMap({}) + SymbolsMap({}), + ClosureVisitor(ast) { + ClosureVisitor.Compute_Closure_Of_Symbols(functions_to_extract); } friend class ExternalizerVisitor; @@ -387,4 +391,7 @@ class SymbolExternalizer /** Symbols and its externalization type */ llvm::StringMap SymbolsMap; + + /* ClosureVisitor to compute the closure. */ + DeclClosureVisitor ClosureVisitor; };