Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow clang-extract to ignore errors pointed by clang #133

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ Clang-extract support many options which controls the output code:
- `-DCE_SYMVERS_PATH=<arg>` Path to kernel Modules.symvers file. Only used when `-D__KERNEL__` is specified.
- `-DCE_DSC_OUTPUT=<arg>` Libpulp .dsc file output, used for userspace livepatching.
- `-DCE_LATE_EXTERNALIZE` Enable late externalization (declare externalized variables later than the original). May reduce code output when `-DCE_KEEP_INCLUDES` is enabled.
- `-DCE_IGNORE_CLANG_ERRORS` Ignore clang compilation errors in a hope that code is generated even if it won't compile.

For more switches, see
```
Expand Down
50 changes: 50 additions & 0 deletions libcextract/ASTUnitHack.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//===- ASTUnitHack.cpp - Hacks for the ASTUnit class ------------------*- 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
/// Hacks for the ASTUnit class.
//
//===----------------------------------------------------------------------===//

/* Author: Giuliano Belinassi */

#include <clang/Frontend/ASTUnit.h>
#include <clang/Frontend/CompilerInvocation.h>
#include <clang/Serialization/InMemoryModuleCache.h>

using namespace clang;

/** Filesystem that will be used in our custom ASTUnit::create, so that way we
* don't break clang's API.
*/
IntrusiveRefCntPtr<llvm::vfs::FileSystem> _Hack_VFS;

/** Hack to create an ASTUnit from LoadFromCompilerInvocationAction with a
* virtual filesystem. That way we can use FrontendActions hooks when
* creating the ASTUnit.
*/
std::unique_ptr<ASTUnit>
ASTUnit::create(std::shared_ptr<CompilerInvocation> CI,
IntrusiveRefCntPtr<DiagnosticsEngine> Diags,
CaptureDiagsKind CaptureDiagnostics,
bool UserFilesAreVolatile) {

std::unique_ptr<ASTUnit> AST(new ASTUnit(false));
ConfigureDiags(Diags, *AST, CaptureDiagnostics);

AST->Diagnostics = Diags;
AST->FileSystemOpts = CI->getFileSystemOpts();
AST->Invocation = std::move(CI);
AST->FileMgr = new FileManager(AST->FileSystemOpts, _Hack_VFS);
AST->UserFilesAreVolatile = UserFilesAreVolatile;
AST->SourceMgr = new SourceManager(AST->getDiagnostics(), *AST->FileMgr,
UserFilesAreVolatile);
AST->ModuleCache = new InMemoryModuleCache;

return AST;
}
8 changes: 8 additions & 0 deletions libcextract/ArgvParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ ArgvParser::ArgvParser(int argc, char **argv)
SymbolsToExternalize(),
HeadersToExpand(),
OutputFile(),
IgnoreClangErrors(false),
DisableExternalization(false),
WithIncludes(false),
DumpPasses(false),
Expand Down Expand Up @@ -177,6 +178,8 @@ void ArgvParser::Print_Usage_Message(void)
" -DCE_LATE_EXTERNALIZE Enable late externalization (declare externalized variables\n"
" later than the original). May reduce code output when\n"
" -DCE_KEEP_INCLUDES is enabled\n"
" -DCE_IGNORE_CLANG_ERRORS Ignore clang compilation errors in a hope that code is\n"
" generated even if it won't compile.\n"
"\n";

llvm::outs() << "The following arguments are ignored by clang-extract:\n";
Expand Down Expand Up @@ -291,6 +294,11 @@ bool ArgvParser::Handle_Clang_Extract_Arg(const char *str)

return true;
}
if (!strcmp("-DCE_IGNORE_CLANG_ERRORS", str)) {
IgnoreClangErrors = true;

return true;
}

if (!strcmp("--help", str)) {
Print_Usage_Message();
Expand Down
6 changes: 6 additions & 0 deletions libcextract/ArgvParser.hh
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ class ArgvParser
return AllowLateExternalization;
}

inline bool Get_Ignore_Clang_Errors(void)
{
return IgnoreClangErrors;
}

const char *Get_Input_File(void);

/** Print help usage message. */
Expand All @@ -150,6 +155,7 @@ class ArgvParser
std::vector<std::string> HeadersToExpand;
std::string OutputFile;

bool IgnoreClangErrors;
bool DisableExternalization;
bool WithIncludes;
bool DumpPasses;
Expand Down
48 changes: 41 additions & 7 deletions libcextract/Passes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ void Print_AST(ASTUnit *ast)
}
}

/** Filesystem that will be used in our custom ASTUnit::create, so that way we
* don't break clang's API. See ASTUnitHack.cpp.
*/
extern IntrusiveRefCntPtr<llvm::vfs::FileSystem> _Hack_VFS;

static bool Build_ASTUnit(PassManager::Context *ctx, IntrusiveRefCntPtr<vfs::FileSystem> fs = nullptr)
{
ctx->AST.reset();
Expand All @@ -65,6 +70,8 @@ static bool Build_ASTUnit(PassManager::Context *ctx, IntrusiveRefCntPtr<vfs::Fil
fs = ctx->OFS;
}

_Hack_VFS = fs;

/* Built the ASTUnit from the passed command line and set its SourceManager
to the PrettyPrint class. */
DiagnosticOptions *diagopts = new DiagnosticOptions();
Expand All @@ -73,17 +80,44 @@ static bool Build_ASTUnit(PassManager::Context *ctx, IntrusiveRefCntPtr<vfs::Fil
}

Diags = CompilerInstance::createDiagnostics(diagopts);

if (ctx->IgnoreClangErrors) {
Diags->setWarningsAsErrors(false);
Diags->setErrorsAsFatal(false);
Diags->setIgnoreAllWarnings(true);
}

CInvok = ClangCompat::createInvocationFromCommandLine(ctx->ClangArgs, Diags);

FileManager *FileMgr = new FileManager(FileSystemOptions(), fs);
PCHContainerOps = std::make_shared<PCHContainerOperations>();

auto AU = ASTUnit::LoadFromCompilerInvocation(
CInvok, PCHContainerOps, Diags, FileMgr, false, CaptureDiagsKind::None, 0,
TU_Complete, false, false, false);

/* Hacked function call, see ASTUnitHack.cpp. */
auto AU = ASTUnit::create(CInvok, Diags, CaptureDiagsKind::None, false);
std::unique_ptr<ASTUnit> *ErrAST = nullptr;

ASTUnit::LoadFromCompilerInvocationAction(CInvok, PCHContainerOps,
Diags, nullptr, AU.get(),
/*Persistent=*/true,
/*ResourceFilesPath=*/StringRef(),
/*OnlyLocalDecls=*/false,
/*CaptureDiagnostics=*/CaptureDiagsKind::None,
/*PrecompilePreambleAfterNParses=*/0,
/*CacheCodeCompletionResults=*/false,
/*UserFilesAreVolatile=*/false,
ErrAST);

_Hack_VFS = nullptr;

if (AU == nullptr) {
DiagsClass::Emit_Error("Unable to create ASTUnit object.");
if (ctx->IgnoreClangErrors && ErrAST) {
PrettyPrint::Set_AST(ErrAST->get());
ctx->AST = std::move(*ErrAST);

return true;
}

throw std::runtime_error("Unable to create ASTUnit object.");
return false;
}

Expand Down Expand Up @@ -336,7 +370,7 @@ class ClosurePass : public Pass

/* If there was an error on building the AST here, don't continue. */
const DiagnosticsEngine &de = ctx->AST->getDiagnostics();
if (de.hasErrorOccurred()) {
if (ctx->IgnoreClangErrors == false && de.hasErrorOccurred()) {
return false;
}

Expand Down Expand Up @@ -689,7 +723,7 @@ int PassManager::Run_Passes(ArgvParser &args)
pass->Dump_Result(&ctx);
}

if (pass_success == false) {
if (ctx.IgnoreClangErrors == false && pass_success == false) {
std::cerr << '\n' << "Error on pass: " << pass->PassName << '\n';
return -1;
}
Expand Down
4 changes: 4 additions & 0 deletions libcextract/Passes.hh
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class PassManager {
: FuncExtractNames(args.Get_Functions_To_Extract()),
Externalize(args.Get_Symbols_To_Externalize()),
OutputFile(args.Get_Output_File()),
IgnoreClangErrors(args.Get_Ignore_Clang_Errors()),
ExternalizationDisabled(args.Is_Externalization_Disabled()),
KeepIncludes(args.Should_Keep_Includes()),
DumpPasses(args.Should_Dump_Passes()),
Expand Down Expand Up @@ -90,6 +91,9 @@ class PassManager {
/** The final output file name. */
std::string &OutputFile;

/** Should we ignore compilation errors from clang? */
bool IgnoreClangErrors;

/** Is the externalization passes disabled? */
bool ExternalizationDisabled;

Expand Down
3 changes: 2 additions & 1 deletion libcextract/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ libcextract_sources = [
'TopLevelASTIterator.cpp',
'ExpansionPolicy.cpp',
'HeaderGenerate.cpp',
'Closure.cpp'
'Closure.cpp',
'ASTUnitHack.cpp'
]

libcextract_static = static_library('cextract', libcextract_sources)
9 changes: 9 additions & 0 deletions testsuite/small/ignore-errors.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/* { dg-options "-DCE_EXTRACT_FUNCTIONS=f -DCE_NO_EXTERNALIZATION -DCE_IGNORE_CLANG_ERRORS" }*/

void f(void)
{
return 3;
}

/* { dg-final { scan-tree-dump "void f" } } */
/* { dg-final { scan-tree-dump "return 3;" } } */
Loading