本文整理汇总了C++中CompilerInvocation类的典型用法代码示例。如果您正苦于以下问题:C++ CompilerInvocation类的具体用法?C++ CompilerInvocation怎么用?C++ CompilerInvocation使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CompilerInvocation类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: createCompilerInvocation
static CompilerInvocation *
createCompilerInvocation(llvm::opt::ArgStringList CFlags, StringRef& Path,
DiagnosticsEngine& Diags)
{
llvm::opt::ArgStringList CCArgs {
"-cc1",
"-triple", "bpf-pc-linux",
"-fsyntax-only",
"-ferror-limit", "19",
"-fmessage-length", "127",
"-O2",
"-nostdsysteminc",
"-nobuiltininc",
"-vectorize-loops",
"-vectorize-slp",
"-Wno-unused-value",
"-Wno-pointer-sign",
"-x", "c"};
CCArgs.append(CFlags.begin(), CFlags.end());
CompilerInvocation *CI = tooling::newInvocation(&Diags, CCArgs);
FrontendOptions& Opts = CI->getFrontendOpts();
Opts.Inputs.clear();
Opts.Inputs.emplace_back(Path,
FrontendOptions::getInputKindForExtension("c"));
return CI;
}
示例2: checkForMigration
static bool checkForMigration(StringRef resourcesPath,
ArrayRef<const char *> Args) {
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
DiagnosticConsumer *DiagClient =
new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
IntrusiveRefCntPtr<DiagnosticsEngine> Diags(
new DiagnosticsEngine(DiagID, &*DiagOpts, DiagClient));
// Chain in -verify checker, if requested.
VerifyDiagnosticConsumer *verifyDiag = nullptr;
if (VerifyDiags) {
verifyDiag = new VerifyDiagnosticConsumer(*Diags);
Diags->setClient(verifyDiag);
}
CompilerInvocation CI;
if (!CompilerInvocation::CreateFromArgs(CI, Args.begin(), Args.end(), *Diags))
return true;
if (CI.getFrontendOpts().Inputs.empty()) {
llvm::errs() << "error: no input files\n";
return true;
}
if (!CI.getLangOpts()->ObjC1)
return false;
arcmt::checkForManualIssues(CI, CI.getFrontendOpts().Inputs[0],
std::make_shared<PCHContainerOperations>(),
Diags->getClient());
return Diags->getClient()->getNumErrors() > 0;
}
示例3: HasARCRuntime
static bool HasARCRuntime(CompilerInvocation &origCI) {
// This duplicates some functionality from Darwin::AddDeploymentTarget
// but this function is well defined, so keep it decoupled from the driver
// and avoid unrelated complications.
for (unsigned i = 0, e = origCI.getPreprocessorOpts().Macros.size();
i != e; ++i) {
StringRef define = origCI.getPreprocessorOpts().Macros[i].first;
bool isUndef = origCI.getPreprocessorOpts().Macros[i].second;
if (isUndef)
continue;
if (!define.startswith(SimulatorVersionDefineName()))
continue;
unsigned Major = 0, Minor = 0, Micro = 0;
if (GetVersionFromSimulatorDefine(define, Major, Minor, Micro) &&
Major < 10 && Minor < 100 && Micro < 100)
return Major >= 5;
}
llvm::Triple triple(origCI.getTargetOpts().Triple);
if (triple.getOS() == llvm::Triple::IOS)
return triple.getOSMajorVersion() >= 5;
if (triple.getOS() == llvm::Triple::Darwin)
return triple.getOSMajorVersion() >= 11;
if (triple.getOS() == llvm::Triple::MacOSX) {
unsigned Major, Minor, Micro;
triple.getOSVersion(Major, Minor, Micro);
return Major > 10 || (Major == 10 && Minor >= 7);
}
return false;
}
示例4: initCompilerInvocation
bool SwiftASTManager::initCompilerInvocation(CompilerInvocation &Invocation,
ArrayRef<const char *> OrigArgs,
DiagnosticEngine &Diags,
StringRef UnresolvedPrimaryFile,
std::string &Error) {
SmallVector<const char *, 16> Args;
sanitizeCompilerArgs(OrigArgs, Args);
Invocation.setRuntimeResourcePath(Impl.RuntimeResourcePath);
bool Err = Invocation.parseArgs(Args, Diags);
if (Err) {
// FIXME: Get the actual diagnostic.
Error = "error when parsing the compiler arguments";
return Err;
}
// FIXME: The frontend should be dealing with symlinks, maybe similar to
// clang's FileManager ?
std::string PrimaryFile =
SwiftLangSupport::resolvePathSymlinks(UnresolvedPrimaryFile);
for (auto &InputFile : Invocation.getFrontendOptions().InputFilenames) {
InputFile = SwiftLangSupport::resolvePathSymlinks(InputFile);
}
ClangImporterOptions &ImporterOpts = Invocation.getClangImporterOptions();
ImporterOpts.DetailedPreprocessingRecord = true;
setModuleName(Invocation);
Invocation.setSerializedDiagnosticsPath(StringRef());
Invocation.getLangOptions().AttachCommentsToDecls = true;
auto &FrontendOpts = Invocation.getFrontendOptions();
if (FrontendOpts.PlaygroundTransform) {
// The playground instrumenter changes the AST in ways that disrupt the
// SourceKit functionality. Since we don't need the instrumenter, and all we
// actually need is the playground semantics visible to the user, like
// silencing the "expression resolves to an unused l-value" error, disable it.
FrontendOpts.PlaygroundTransform = false;
}
if (!PrimaryFile.empty()) {
Optional<unsigned> PrimaryIndex;
for (auto i : indices(Invocation.getFrontendOptions().InputFilenames)) {
auto &CurFile = Invocation.getFrontendOptions().InputFilenames[i];
if (PrimaryFile == CurFile) {
PrimaryIndex = i;
break;
}
}
if (!PrimaryIndex) {
llvm::SmallString<64> Err;
llvm::raw_svector_ostream OS(Err);
OS << "'" << PrimaryFile << "' is not part of the input files";
Error = OS.str();
return true;
}
Invocation.getFrontendOptions().PrimaryInput = SelectedInput(*PrimaryIndex);
}
return Err;
}
示例5: scopedCloseFiles
SwiftInterfaceGenContextRef
SwiftInterfaceGenContext::create(StringRef DocumentName,
bool IsModule,
StringRef ModuleOrHeaderName,
CompilerInvocation Invocation,
std::string &ErrMsg) {
SwiftInterfaceGenContextRef IFaceGenCtx{ new SwiftInterfaceGenContext() };
IFaceGenCtx->Impl.DocumentName = DocumentName;
IFaceGenCtx->Impl.IsModule = IsModule;
IFaceGenCtx->Impl.ModuleOrHeaderName = ModuleOrHeaderName;
IFaceGenCtx->Impl.Invocation = Invocation;
CompilerInstance &CI = IFaceGenCtx->Impl.Instance;
// Display diagnostics to stderr.
CI.addDiagnosticConsumer(&IFaceGenCtx->Impl.DiagConsumer);
Invocation.clearInputs();
if (CI.setup(Invocation)) {
ErrMsg = "Error during invocation setup";
return nullptr;
}
ASTContext &Ctx = CI.getASTContext();
CloseClangModuleFiles scopedCloseFiles(*Ctx.getClangModuleLoader());
// Load standard library so that Clang importer can use it.
auto *Stdlib = getModuleByFullName(Ctx, Ctx.StdlibModuleName);
if (!Stdlib) {
ErrMsg = "Could not load the stdlib module";
return nullptr;
}
if (IsModule) {
if (getModuleInterfaceInfo(Ctx, ModuleOrHeaderName, IFaceGenCtx->Impl,
ErrMsg))
return nullptr;
} else {
auto &FEOpts = Invocation.getFrontendOptions();
if (FEOpts.ImplicitObjCHeaderPath.empty()) {
ErrMsg = "Implicit ObjC header path is empty";
return nullptr;
}
auto &Importer = static_cast<ClangImporter &>(*Ctx.getClangModuleLoader());
Importer.importBridgingHeader(FEOpts.ImplicitObjCHeaderPath,
CI.getMainModule(),
/*diagLoc=*/{},
/*trackParsedSymbols=*/true);
if (getHeaderInterfaceInfo(Ctx, ModuleOrHeaderName,
IFaceGenCtx->Impl.Info, ErrMsg))
return nullptr;
}
if (makeParserAST(IFaceGenCtx->Impl.TextCI, IFaceGenCtx->Impl.Info.Text)) {
ErrMsg = "Error during syntactic parsing";
return nullptr;
}
return IFaceGenCtx;
}
示例6: CreateFromArgs
void CompilerInvocation::CreateFromArgs(CompilerInvocation &Res,
const char **ArgBegin,
const char **ArgEnd,
Diagnostic &Diags) {
// Parse the arguments.
llvm::OwningPtr<OptTable> Opts(createCC1OptTable());
unsigned MissingArgIndex, MissingArgCount;
llvm::OwningPtr<InputArgList> Args(
Opts->ParseArgs(ArgBegin, ArgEnd,MissingArgIndex, MissingArgCount));
// Check for missing argument error.
if (MissingArgCount)
Diags.Report(diag::err_drv_missing_argument)
<< Args->getArgString(MissingArgIndex) << MissingArgCount;
// Issue errors on unknown arguments.
for (arg_iterator it = Args->filtered_begin(OPT_UNKNOWN),
ie = Args->filtered_end(); it != ie; ++it)
Diags.Report(diag::err_drv_unknown_argument) << it->getAsString(*Args);
ParseAnalyzerArgs(Res.getAnalyzerOpts(), *Args, Diags);
ParseCodeGenArgs(Res.getCodeGenOpts(), *Args, Diags);
ParseDependencyOutputArgs(Res.getDependencyOutputOpts(), *Args);
ParseDiagnosticArgs(Res.getDiagnosticOpts(), *Args, Diags);
FrontendOptions::InputKind DashX =
ParseFrontendArgs(Res.getFrontendOpts(), *Args, Diags);
ParseHeaderSearchArgs(Res.getHeaderSearchOpts(), *Args);
if (DashX != FrontendOptions::IK_AST)
ParseLangArgs(Res.getLangOpts(), *Args, DashX, Diags);
ParsePreprocessorArgs(Res.getPreprocessorOpts(), *Args, Diags);
ParsePreprocessorOutputArgs(Res.getPreprocessorOutputOpts(), *Args);
ParseTargetArgs(Res.getTargetOpts(), *Args);
}
示例7: editorOpenTypeInterface
//===----------------------------------------------------------------------===//
// EditorOpenTypeInterface
//===----------------------------------------------------------------------===//
void SwiftLangSupport::editorOpenTypeInterface(EditorConsumer &Consumer,
ArrayRef<const char *> Args,
StringRef TypeUSR) {
CompilerInstance CI;
// Display diagnostics to stderr.
PrintingDiagnosticConsumer PrintDiags;
CI.addDiagnosticConsumer(&PrintDiags);
CompilerInvocation Invocation;
std::string Error;
if (getASTManager().initCompilerInvocation(Invocation, Args, CI.getDiags(),
StringRef(), Error)) {
Consumer.handleRequestError(Error.c_str());
return;
}
Invocation.getClangImporterOptions().ImportForwardDeclarations = true;
std::string ErrMsg;
auto IFaceGenRef = SwiftInterfaceGenContext::createForTypeInterface(
Invocation,
TypeUSR,
ErrMsg);
if (!IFaceGenRef) {
Consumer.handleRequestError(ErrMsg.c_str());
return;
}
IFaceGenRef->reportEditorInfo(Consumer);
// reportEditorInfo requires exclusive access to the AST, so don't add this
// to the service cache until it has returned.
IFaceGenContexts.set(TypeUSR, IFaceGenRef);
}
示例8: getASTManager
void SwiftLangSupport::getDocInfo(llvm::MemoryBuffer *InputBuf,
StringRef ModuleName,
ArrayRef<const char *> Args,
DocInfoConsumer &Consumer) {
CompilerInstance CI;
// Display diagnostics to stderr.
PrintingDiagnosticConsumer PrintDiags;
CI.addDiagnosticConsumer(&PrintDiags);
CompilerInvocation Invocation;
std::string Error;
bool Failed = getASTManager().initCompilerInvocation(Invocation, Args,
CI.getDiags(),
StringRef(),
Error);
if (Failed) {
Consumer.failed(Error);
return;
}
Invocation.getClangImporterOptions().ImportForwardDeclarations = true;
if (!ModuleName.empty()) {
bool Error = reportModuleDocInfo(Invocation, ModuleName, Consumer);
if (Error)
Consumer.failed("Error occurred");
return;
}
Failed = reportSourceDocInfo(Invocation, InputBuf, Consumer);
if (Failed)
Consumer.failed("Error occurred");
}
示例9: performTransformations
static bool performTransformations(StringRef resourcesPath,
ArrayRef<const char *> Args) {
// Check first.
if (checkForMigration(resourcesPath, Args))
return true;
IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts = new DiagnosticOptions();
DiagnosticConsumer *DiagClient =
new TextDiagnosticPrinter(llvm::errs(), &*DiagOpts);
IntrusiveRefCntPtr<DiagnosticIDs> DiagID(new DiagnosticIDs());
IntrusiveRefCntPtr<DiagnosticsEngine> TopDiags(
new DiagnosticsEngine(DiagID, &*DiagOpts, &*DiagClient));
CompilerInvocation origCI;
if (!CompilerInvocation::CreateFromArgs(origCI, Args.begin(), Args.end(),
*TopDiags))
return true;
if (origCI.getFrontendOpts().Inputs.empty()) {
llvm::errs() << "error: no input files\n";
return true;
}
if (!origCI.getLangOpts()->ObjC1)
return false;
MigrationProcess migration(origCI, std::make_shared<PCHContainerOperations>(),
DiagClient);
std::vector<TransformFn>
transforms = arcmt::getAllTransformations(origCI.getLangOpts()->getGC(),
origCI.getMigratorOpts().NoFinalizeRemoval);
assert(!transforms.empty());
std::unique_ptr<PrintTransforms> transformPrinter;
if (OutputTransformations)
transformPrinter.reset(new PrintTransforms(llvm::outs()));
for (unsigned i=0, e = transforms.size(); i != e; ++i) {
bool err = migration.applyTransform(transforms[i], transformPrinter.get());
if (err) return true;
if (VerboseOpt) {
if (i == e-1)
llvm::errs() << "\n##### FINAL RESULT #####\n";
else
llvm::errs() << "\n##### OUTPUT AFTER "<< i+1 <<". TRANSFORMATION #####\n";
printResult(migration.getRemapper(), llvm::errs());
llvm::errs() << "\n##########################\n\n";
}
}
if (!OutputTransformations)
printResult(migration.getRemapper(), llvm::outs());
// FIXME: TestResultForARC
return false;
}
示例10: printResult
static void printResult(FileRemapper &remapper, raw_ostream &OS) {
CompilerInvocation CI;
remapper.applyMappings(CI);
PreprocessorOptions &PPOpts = CI.getPreprocessorOpts();
// The changed files will be in memory buffers, print them.
for (unsigned i = 0, e = PPOpts.RemappedFileBuffers.size(); i != e; ++i) {
const llvm::MemoryBuffer *mem = PPOpts.RemappedFileBuffers[i].second;
OS << mem->getBuffer();
}
}
示例11: updateCodeAndEmitRemap
bool migrator::updateCodeAndEmitRemap(CompilerInstance *Instance,
const CompilerInvocation &Invocation) {
// Delete the remap file, in case someone is re-running the Migrator. If the
// file fails to compile and we don't get a chance to overwrite it, the old
// changes may get picked up.
llvm::sys::fs::remove(Invocation.getMigratorOptions().EmitRemapFilePath);
Migrator M { Instance, Invocation }; // Provide inputs and configuration
// Phase 1: Pre Fix-it passes
// These uses the initial frontend invocation to apply any obvious fix-its
// to see if we can get an error-free AST to get to Phase 2.
std::unique_ptr<swift::CompilerInstance> PreFixItInstance;
if (Instance->getASTContext().hadError()) {
PreFixItInstance = M.repeatFixitMigrations(2,
Invocation.getLangOptions().EffectiveLanguageVersion);
// If we still couldn't fix all of the errors, give up.
if (PreFixItInstance == nullptr ||
!PreFixItInstance->hasASTContext() ||
PreFixItInstance->getASTContext().hadError()) {
return true;
}
M.StartInstance = PreFixItInstance.get();
}
// Phase 2: Syntactic Transformations
auto FailedSyntacticPasses = M.performSyntacticPasses();
if (FailedSyntacticPasses) {
return true;
}
// Phase 3: Post Fix-it Passes
// Perform fix-it based migrations on the compiler, some number of times in
// order to give the compiler an opportunity to
// take its time reaching a fixed point.
// This is the end of the pipeline, so we throw away the compiler instance(s)
// we used in these fix-it runs.
if (M.getMigratorOptions().EnableMigratorFixits) {
M.repeatFixitMigrations(Migrator::MaxCompilerFixitPassIterations,
{4, 0, 0});
}
// OK, we have a final resulting text. Now we compare against the input
// to calculate a replacement map describing the changes to the input
// necessary to get the output.
// TODO: Document replacement map format.
auto EmitRemapFailed = M.emitRemap();
auto EmitMigratedFailed = M.emitMigratedFile();
auto DumpMigrationStatesFailed = M.dumpStates();
return EmitRemapFailed || EmitMigratedFailed || DumpMigrationStatesFailed;
}
示例12: editorOpenHeaderInterface
void SwiftLangSupport::editorOpenHeaderInterface(EditorConsumer &Consumer,
StringRef Name,
StringRef HeaderName,
ArrayRef<const char *> Args,
bool UsingSwiftArgs,
bool SynthesizedExtensions,
StringRef swiftVersion) {
CompilerInstance CI;
// Display diagnostics to stderr.
PrintingDiagnosticConsumer PrintDiags;
CI.addDiagnosticConsumer(&PrintDiags);
CompilerInvocation Invocation;
std::string Error;
ArrayRef<const char *> SwiftArgs = UsingSwiftArgs ? Args : llvm::None;
if (getASTManager().initCompilerInvocationNoInputs(Invocation, SwiftArgs,
CI.getDiags(), Error)) {
Consumer.handleRequestError(Error.c_str());
return;
}
if (!UsingSwiftArgs && initInvocationByClangArguments(Args, Invocation, Error)) {
Consumer.handleRequestError(Error.c_str());
return;
}
Invocation.getClangImporterOptions().ImportForwardDeclarations = true;
if (!swiftVersion.empty()) {
auto swiftVer = version::Version::parseVersionString(swiftVersion,
SourceLoc(), nullptr);
if (swiftVer.hasValue())
Invocation.getLangOptions().EffectiveLanguageVersion =
swiftVer.getValue();
}
auto IFaceGenRef = SwiftInterfaceGenContext::create(Name,
/*IsModule=*/false,
HeaderName,
None,
Invocation,
Error,
SynthesizedExtensions,
None);
if (!IFaceGenRef) {
Consumer.handleRequestError(Error.c_str());
return;
}
IFaceGenRef->reportEditorInfo(Consumer);
// reportEditorInfo requires exclusive access to the AST, so don't add this
// to the service cache until it has returned.
IFaceGenContexts.set(Name, IFaceGenRef);
}
示例13: editorOpenInterface
//===----------------------------------------------------------------------===//
// EditorOpenInterface
//===----------------------------------------------------------------------===//
void SwiftLangSupport::editorOpenInterface(EditorConsumer &Consumer,
StringRef Name,
StringRef ModuleName,
Optional<StringRef> Group,
ArrayRef<const char *> Args,
bool SynthesizedExtensions,
Optional<StringRef> InterestedUSR) {
CompilerInstance CI;
// Display diagnostics to stderr.
PrintingDiagnosticConsumer PrintDiags;
CI.addDiagnosticConsumer(&PrintDiags);
CompilerInvocation Invocation;
std::string Error;
if (getASTManager().initCompilerInvocationNoInputs(Invocation, Args,
CI.getDiags(), Error)) {
Consumer.handleRequestError(Error.c_str());
return;
}
trace::TracedOperation TracedOp;
if (trace::enabled()) {
trace::SwiftInvocation SwiftArgs;
SwiftArgs.Args.Args.assign(Args.begin(), Args.end());
// NOTE: do not use primary file
// NOTE: do not use files
TracedOp.start(trace::OperationKind::OpenInterface, SwiftArgs,
{std::make_pair("Name", Name),
std::make_pair("ModuleName", ModuleName)});
}
Invocation.getClangImporterOptions().ImportForwardDeclarations = true;
std::string ErrMsg;
auto IFaceGenRef = SwiftInterfaceGenContext::create(Name,
/*IsModule=*/true,
ModuleName,
Group,
Invocation,
ErrMsg,
SynthesizedExtensions,
InterestedUSR);
if (!IFaceGenRef) {
Consumer.handleRequestError(ErrMsg.c_str());
return;
}
IFaceGenRef->reportEditorInfo(Consumer);
// reportEditorInfo requires exclusive access to the AST, so don't add this
// to the service cache until it has returned.
IFaceGenContexts.set(Name, IFaceGenRef);
}
示例14: makeParserAST
static bool makeParserAST(CompilerInstance &CI, StringRef Text) {
CompilerInvocation Invocation;
Invocation.setModuleName("main");
Invocation.setInputKind(InputFileKind::IFK_Swift);
std::unique_ptr<llvm::MemoryBuffer> Buf;
Buf = llvm::MemoryBuffer::getMemBuffer(Text, "<module-interface>");
Invocation.addInputBuffer(Buf.get());
if (CI.setup(Invocation))
return true;
CI.performParseOnly();
return false;
}
示例15: doFormat
int doFormat(ArrayRef<StringRef> InputFiles) {
for (auto InputFilename : InputFiles) {
CompilerInvocation Invocation;
Invocation.addInputFilename(InputFilename);
Invocation.setModuleName("Format");
CompilerInstance Instance;
auto &SourceMgr = Instance.getSourceMgr();
auto &Diags = Instance.getDiags();
PrintingDiagnosticConsumer DiagPrinter;
Instance.addDiagnosticConsumer(&DiagPrinter);
if (Instance.setup(Invocation)) {
return EXIT_FAILURE;
}
// First, parse the file normally and get the regular old AST.
Instance.performParseOnly();
auto BufferID = Instance.getInputBufferIDs().back();
auto &SF = Instance.getMainModule()
->getMainSourceFile(SourceFileKind::Main);
auto Buffer = llvm::MemoryBuffer::getFile(InputFilename);
if (!Buffer) {
Diags.diagnose(SourceLoc(), diag::cannot_open_file,
InputFilename, Buffer.getError().message());
return EXIT_FAILURE;
}
auto Tokens = tokenizeWithTrivia(Invocation.getLangOptions(),
SourceMgr, BufferID);
SmallVector<Decl *, 256> FileDecls;
SF.getTopLevelDecls(FileDecls);
sema::Semantics Sema;
for (auto *Decl : FileDecls) {
if (Decl->escapedFromIfConfig()) {
continue;
}
auto NewNode = transformAST(ASTNode(Decl), Sema, SourceMgr,
BufferID, Tokens);
if (NewNode.hasValue()) {
auto Reformatted = format(NewNode.getValue());
Reformatted.print(llvm::outs());
}
}
}
return EXIT_SUCCESS;
}