Skip to content

Instantly share code, notes, and snippets.

@vitonzhangtt
Last active July 4, 2022 04:06
Show Gist options
  • Save vitonzhangtt/f9eb2e138566ff53554c7c8a297f9947 to your computer and use it in GitHub Desktop.
Save vitonzhangtt/f9eb2e138566ff53554c7c8a297f9947 to your computer and use it in GitHub Desktop.
CompilerInstance.cpp in oclint-22.02
#include "oclint/CompilerInstance.h"
#include <clang/Basic/TargetInfo.h>
#include <clang/Frontend/FrontendActions.h>
#include <clang/StaticAnalyzer/Frontend/FrontendActions.h>
#include "oclint/Options.h"
using namespace oclint;
static clang::FrontendAction *getFrontendAction() {
if (option::enableClangChecker()) {
/**
*/
return new clang::ento::AnalysisAction();
}
/**
LibTooling
https://clang.llvm.org/docs/LibTooling.html
1. SyntaxOnlyAction
In this tutorial, we’ll demonstrate the different ways of running
Clang’s SyntaxOnlyAction, which runs a quick syntax check,
over a bunch of code.
*/
return new clang::SyntaxOnlyAction();
}
void CompilerInstance::setupTarget() {
if ((getLangOpts().CUDA || getLangOpts().OpenMPIsDevice) &&
!getFrontendOpts().AuxTriple.empty())
{
auto targetOptions = std::make_shared<clang::TargetOptions>();
targetOptions->Triple = llvm::Triple::normalize(getFrontendOpts().AuxTriple);
targetOptions->HostTriple = getTarget().getTriple().str();
setAuxTarget(clang::TargetInfo::CreateTargetInfo(getDiagnostics(), targetOptions));
}
/**
TargetInfo::adjust() - Set forced language options.
Apply changes to the target information with respect to certain language options
which change the target configuration and adjust the language based on the
target options where applicable.
*/
getTarget().adjust(getDiagnostics(), getLangOpts());
/*
CompilerInstance::getAuxTarget()
Get the Auxiliary Target info.
*/
if (auto *auxTarget = getAuxTarget()) {
// Set the `Auxiliary target` for the Target.
getTarget().setAuxTarget(auxTarget);
}
}
void CompilerInstance::start() {
assert(hasDiagnostics() && "Diagnostics engine is not initialized!");
assert(!getFrontendOpts().ShowHelp && "Client must handle '-help'!");
assert(!getFrontendOpts().ShowVersion && "Client must handle '-version'!");
setTarget(clang::TargetInfo::CreateTargetInfo(getDiagnostics(), getInvocation().TargetOpts));
if (!hasTarget()) { return; }
setupTarget();
/**
clang::CompilerInstance::getFrontendOpts() return clang::FrontendOptions instance.
1. clang::FrontendOptions
Options for controlling the behavior of the frontend.
2. clang::FrontendOptions::Inputs
SmallVector<FrontendInputFile, 0> clang::FrontendOptions::Inputs
The input files and their types.
3. clang::FrontendAction
Abstract base class for actions which can be performed by the frontend.
*/
for (const auto& input : getFrontendOpts().Inputs) {
if (hasSourceManager()) {
getSourceManager().clearIDTables();
}
clang::FrontendAction *frontendAction = getFrontendAction();
/**
1. FrontendAction::BeginSourceFile()
bool FrontendAction::BeginSourceFile(CompilerInstance & CI,
const FrontendInputFile & Input)
Prepare the action for processing the input file Input.
This is run after the options and frontend have been initialized,
but prior to executing any per-file processing.
*/
if(frontendAction->BeginSourceFile(*this, input)) {
/**
1. FrontendAction::Execute()
llvm::Error FrontendAction::Execute()
Set the source manager's main input file, and run the action.
*/
static_cast<void>(frontendAction->Execute());
_actions.emplace_back(frontendAction);
}
}
}
void CompilerInstance::end() {
for (const auto& action : _actions) {
/**
*/
action->EndSourceFile();
}
getDiagnostics().getClient()->finish();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment