2009-11-14 18:42:46 +08:00
|
|
|
//===--- FrontendActions.cpp ----------------------------------------------===//
|
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, 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
|
2009-11-14 18:42:46 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Frontend/FrontendActions.h"
|
|
|
|
#include "clang/AST/ASTConsumer.h"
|
|
|
|
#include "clang/Basic/FileManager.h"
|
2019-08-05 21:59:26 +08:00
|
|
|
#include "clang/Basic/LangStandard.h"
|
2009-11-14 18:42:46 +08:00
|
|
|
#include "clang/Frontend/ASTConsumers.h"
|
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Frontend/FrontendDiagnostic.h"
|
2015-06-21 02:53:08 +08:00
|
|
|
#include "clang/Frontend/MultiplexConsumer.h"
|
2009-11-14 18:42:46 +08:00
|
|
|
#include "clang/Frontend/Utils.h"
|
2019-06-04 06:59:17 +08:00
|
|
|
#include "clang/Lex/DependencyDirectivesSourceMinimizer.h"
|
2012-12-04 17:13:33 +08:00
|
|
|
#include "clang/Lex/HeaderSearch.h"
|
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2016-07-19 03:02:11 +08:00
|
|
|
#include "clang/Lex/PreprocessorOptions.h"
|
2018-02-10 22:04:45 +08:00
|
|
|
#include "clang/Sema/TemplateInstCallback.h"
|
2013-03-28 00:47:18 +08:00
|
|
|
#include "clang/Serialization/ASTReader.h"
|
2010-08-19 07:56:37 +08:00
|
|
|
#include "clang/Serialization/ASTWriter.h"
|
2011-12-09 03:11:24 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2010-07-21 04:18:03 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2016-07-19 03:02:11 +08:00
|
|
|
#include "llvm/Support/Path.h"
|
2018-02-10 22:04:45 +08:00
|
|
|
#include "llvm/Support/YAMLTraits.h"
|
2019-06-04 06:59:17 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2014-03-09 19:36:40 +08:00
|
|
|
#include <memory>
|
2014-06-13 01:19:42 +08:00
|
|
|
#include <system_error>
|
2011-09-24 07:43:36 +08:00
|
|
|
|
2009-11-14 18:42:46 +08:00
|
|
|
using namespace clang;
|
|
|
|
|
2018-02-10 22:04:45 +08:00
|
|
|
namespace {
|
|
|
|
CodeCompleteConsumer *GetCodeCompletionConsumer(CompilerInstance &CI) {
|
|
|
|
return CI.hasCodeCompletionConsumer() ? &CI.getCodeCompletionConsumer()
|
|
|
|
: nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void EnsureSemaIsCreated(CompilerInstance &CI, FrontendAction &Action) {
|
|
|
|
if (Action.hasCodeCompletionSupport() &&
|
|
|
|
!CI.getFrontendOpts().CodeCompletionAt.FileName.empty())
|
|
|
|
CI.createCodeCompletionConsumer();
|
|
|
|
|
|
|
|
if (!CI.hasSema())
|
|
|
|
CI.createSema(Action.getTranslationUnitKind(),
|
|
|
|
GetCodeCompletionConsumer(CI));
|
|
|
|
}
|
|
|
|
} // namespace
|
|
|
|
|
2010-03-20 03:44:04 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Custom Actions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-11 03:56:51 +08:00
|
|
|
std::unique_ptr<ASTConsumer>
|
|
|
|
InitOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
|
2019-08-15 07:04:18 +08:00
|
|
|
return std::make_unique<ASTConsumer>();
|
2010-03-20 03:44:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void InitOnlyAction::ExecuteAction() {
|
|
|
|
}
|
|
|
|
|
2009-11-14 18:42:57 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// AST Consumer Actions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-08-11 03:56:51 +08:00
|
|
|
std::unique_ptr<ASTConsumer>
|
|
|
|
ASTPrintAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
|
2016-07-15 08:55:40 +08:00
|
|
|
if (std::unique_ptr<raw_ostream> OS =
|
|
|
|
CI.createDefaultOutputFile(false, InFile))
|
|
|
|
return CreateASTPrinter(std::move(OS), CI.getFrontendOpts().ASTDumpFilter);
|
2014-05-22 12:46:25 +08:00
|
|
|
return nullptr;
|
2009-11-14 18:42:46 +08:00
|
|
|
}
|
|
|
|
|
2014-08-11 03:56:51 +08:00
|
|
|
std::unique_ptr<ASTConsumer>
|
|
|
|
ASTDumpAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
|
2019-05-14 05:39:55 +08:00
|
|
|
const FrontendOptions &Opts = CI.getFrontendOpts();
|
|
|
|
return CreateASTDumper(nullptr /*Dump to stdout.*/, Opts.ASTDumpFilter,
|
|
|
|
Opts.ASTDumpDecls, Opts.ASTDumpAll,
|
|
|
|
Opts.ASTDumpLookups, Opts.ASTDumpFormat);
|
2009-11-14 18:42:46 +08:00
|
|
|
}
|
|
|
|
|
2014-08-11 03:56:51 +08:00
|
|
|
std::unique_ptr<ASTConsumer>
|
|
|
|
ASTDeclListAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
|
2012-07-31 17:37:40 +08:00
|
|
|
return CreateASTDeclNodeLister();
|
|
|
|
}
|
|
|
|
|
2014-08-11 03:56:51 +08:00
|
|
|
std::unique_ptr<ASTConsumer>
|
|
|
|
ASTViewAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
|
2009-11-14 18:42:46 +08:00
|
|
|
return CreateASTViewer();
|
|
|
|
}
|
|
|
|
|
2014-08-11 03:56:51 +08:00
|
|
|
std::unique_ptr<ASTConsumer>
|
|
|
|
GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
|
2010-08-03 16:14:03 +08:00
|
|
|
std::string Sysroot;
|
2017-11-17 00:25:01 +08:00
|
|
|
if (!ComputeASTConsumerArguments(CI, /*ref*/ Sysroot))
|
|
|
|
return nullptr;
|
|
|
|
|
2011-02-16 01:54:22 +08:00
|
|
|
std::string OutputFile;
|
2016-07-15 08:55:40 +08:00
|
|
|
std::unique_ptr<raw_pwrite_stream> OS =
|
2017-11-17 00:25:01 +08:00
|
|
|
CreateOutputFile(CI, InFile, /*ref*/ OutputFile);
|
2015-04-10 20:54:53 +08:00
|
|
|
if (!OS)
|
2014-05-22 12:46:25 +08:00
|
|
|
return nullptr;
|
2010-08-03 16:14:03 +08:00
|
|
|
|
2011-07-23 00:35:34 +08:00
|
|
|
if (!CI.getFrontendOpts().RelocatablePCH)
|
|
|
|
Sysroot.clear();
|
2015-06-21 02:53:08 +08:00
|
|
|
|
2018-06-25 21:23:49 +08:00
|
|
|
const auto &FrontendOpts = CI.getFrontendOpts();
|
2015-06-21 02:53:08 +08:00
|
|
|
auto Buffer = std::make_shared<PCHBuffer>();
|
|
|
|
std::vector<std::unique_ptr<ASTConsumer>> Consumers;
|
2019-08-15 07:04:18 +08:00
|
|
|
Consumers.push_back(std::make_unique<PCHGenerator>(
|
2019-03-10 01:33:56 +08:00
|
|
|
CI.getPreprocessor(), CI.getModuleCache(), OutputFile, Sysroot, Buffer,
|
|
|
|
FrontendOpts.ModuleFileExtensions,
|
|
|
|
CI.getPreprocessorOpts().AllowPCHWithCompilerErrors,
|
2019-03-13 02:38:04 +08:00
|
|
|
FrontendOpts.IncludeTimestamps, +CI.getLangOpts().CacheGeneratedPCH));
|
2015-09-19 06:10:59 +08:00
|
|
|
Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator(
|
2016-07-15 08:55:40 +08:00
|
|
|
CI, InFile, OutputFile, std::move(OS), Buffer));
|
2015-06-21 02:53:08 +08:00
|
|
|
|
2019-08-15 07:04:18 +08:00
|
|
|
return std::make_unique<MultiplexConsumer>(std::move(Consumers));
|
2010-08-03 16:14:03 +08:00
|
|
|
}
|
|
|
|
|
2017-11-17 00:25:01 +08:00
|
|
|
bool GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI,
|
|
|
|
std::string &Sysroot) {
|
2010-08-03 16:14:03 +08:00
|
|
|
Sysroot = CI.getHeaderSearchOpts().Sysroot;
|
|
|
|
if (CI.getFrontendOpts().RelocatablePCH && Sysroot.empty()) {
|
2010-08-18 01:55:38 +08:00
|
|
|
CI.getDiagnostics().Report(diag::err_relocatable_without_isysroot);
|
2017-11-17 00:25:01 +08:00
|
|
|
return false;
|
2009-11-14 18:42:46 +08:00
|
|
|
}
|
|
|
|
|
2017-11-17 00:25:01 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<llvm::raw_pwrite_stream>
|
|
|
|
GeneratePCHAction::CreateOutputFile(CompilerInstance &CI, StringRef InFile,
|
|
|
|
std::string &OutputFile) {
|
2011-02-01 06:00:44 +08:00
|
|
|
// We use createOutputFile here because this is exposed via libclang, and we
|
|
|
|
// must disable the RemoveFileOnSignal behavior.
|
2011-07-28 08:45:10 +08:00
|
|
|
// We use a temporary to avoid race conditions.
|
2016-07-15 08:55:40 +08:00
|
|
|
std::unique_ptr<raw_pwrite_stream> OS =
|
2015-04-10 20:54:53 +08:00
|
|
|
CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
|
|
|
|
/*RemoveFileOnSignal=*/false, InFile,
|
2019-07-16 12:46:31 +08:00
|
|
|
/*Extension=*/"", /*UseTemporary=*/true);
|
2009-12-03 17:13:30 +08:00
|
|
|
if (!OS)
|
2015-04-10 20:54:53 +08:00
|
|
|
return nullptr;
|
2009-12-03 17:13:30 +08:00
|
|
|
|
2011-02-16 01:54:22 +08:00
|
|
|
OutputFile = CI.getFrontendOpts().OutputFile;
|
2015-04-10 20:54:53 +08:00
|
|
|
return OS;
|
2009-11-14 18:42:46 +08:00
|
|
|
}
|
|
|
|
|
2017-02-27 11:52:36 +08:00
|
|
|
bool GeneratePCHAction::shouldEraseOutputFiles() {
|
|
|
|
if (getCompilerInstance().getPreprocessorOpts().AllowPCHWithCompilerErrors)
|
|
|
|
return false;
|
|
|
|
return ASTFrontendAction::shouldEraseOutputFiles();
|
|
|
|
}
|
|
|
|
|
2017-06-09 09:36:10 +08:00
|
|
|
bool GeneratePCHAction::BeginSourceFileAction(CompilerInstance &CI) {
|
2017-01-10 03:20:18 +08:00
|
|
|
CI.getLangOpts().CompilingPCH = true;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-11 03:56:51 +08:00
|
|
|
std::unique_ptr<ASTConsumer>
|
|
|
|
GenerateModuleAction::CreateASTConsumer(CompilerInstance &CI,
|
|
|
|
StringRef InFile) {
|
2016-08-26 08:14:38 +08:00
|
|
|
std::unique_ptr<raw_pwrite_stream> OS = CreateOutputFile(CI, InFile);
|
2015-04-10 21:14:31 +08:00
|
|
|
if (!OS)
|
2014-05-22 12:46:25 +08:00
|
|
|
return nullptr;
|
|
|
|
|
2016-08-26 08:14:38 +08:00
|
|
|
std::string OutputFile = CI.getFrontendOpts().OutputFile;
|
|
|
|
std::string Sysroot;
|
|
|
|
|
2015-06-21 02:53:08 +08:00
|
|
|
auto Buffer = std::make_shared<PCHBuffer>();
|
|
|
|
std::vector<std::unique_ptr<ASTConsumer>> Consumers;
|
2015-11-04 02:33:07 +08:00
|
|
|
|
2019-08-15 07:04:18 +08:00
|
|
|
Consumers.push_back(std::make_unique<PCHGenerator>(
|
2019-03-10 01:33:56 +08:00
|
|
|
CI.getPreprocessor(), CI.getModuleCache(), OutputFile, Sysroot, Buffer,
|
|
|
|
CI.getFrontendOpts().ModuleFileExtensions,
|
|
|
|
/*AllowASTWithErrors=*/false,
|
|
|
|
/*IncludeTimestamps=*/
|
2019-03-13 02:38:04 +08:00
|
|
|
+CI.getFrontendOpts().BuildingImplicitModule,
|
|
|
|
/*ShouldCacheASTInMemory=*/
|
2019-03-10 01:33:56 +08:00
|
|
|
+CI.getFrontendOpts().BuildingImplicitModule));
|
2015-09-19 06:10:59 +08:00
|
|
|
Consumers.push_back(CI.getPCHContainerWriter().CreatePCHContainerGenerator(
|
2016-07-15 08:55:40 +08:00
|
|
|
CI, InFile, OutputFile, std::move(OS), Buffer));
|
2019-08-15 07:04:18 +08:00
|
|
|
return std::make_unique<MultiplexConsumer>(std::move(Consumers));
|
2011-11-16 08:09:06 +08:00
|
|
|
}
|
|
|
|
|
2017-07-07 05:05:56 +08:00
|
|
|
bool GenerateModuleFromModuleMapAction::BeginSourceFileAction(
|
|
|
|
CompilerInstance &CI) {
|
|
|
|
if (!CI.getLangOpts().Modules) {
|
|
|
|
CI.getDiagnostics().Report(diag::err_module_build_requires_fmodules);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GenerateModuleAction::BeginSourceFileAction(CI);
|
|
|
|
}
|
|
|
|
|
2016-07-15 08:55:40 +08:00
|
|
|
std::unique_ptr<raw_pwrite_stream>
|
2016-08-26 08:14:38 +08:00
|
|
|
GenerateModuleFromModuleMapAction::CreateOutputFile(CompilerInstance &CI,
|
|
|
|
StringRef InFile) {
|
2011-11-16 08:09:06 +08:00
|
|
|
// If no output file was provided, figure out where this module would go
|
|
|
|
// in the module cache.
|
|
|
|
if (CI.getFrontendOpts().OutputFile.empty()) {
|
2017-04-28 09:49:42 +08:00
|
|
|
StringRef ModuleMapFile = CI.getFrontendOpts().OriginalModuleMap;
|
|
|
|
if (ModuleMapFile.empty())
|
|
|
|
ModuleMapFile = InFile;
|
|
|
|
|
2011-11-16 08:09:06 +08:00
|
|
|
HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
|
2014-04-15 02:00:01 +08:00
|
|
|
CI.getFrontendOpts().OutputFile =
|
2017-08-31 14:26:43 +08:00
|
|
|
HS.getCachedModuleFileName(CI.getLangOpts().CurrentModule,
|
|
|
|
ModuleMapFile);
|
2011-11-16 08:09:06 +08:00
|
|
|
}
|
2015-04-10 21:14:31 +08:00
|
|
|
|
2011-11-16 08:09:06 +08:00
|
|
|
// We use createOutputFile here because this is exposed via libclang, and we
|
|
|
|
// must disable the RemoveFileOnSignal behavior.
|
|
|
|
// We use a temporary to avoid race conditions.
|
2016-08-26 08:14:38 +08:00
|
|
|
return CI.createOutputFile(CI.getFrontendOpts().OutputFile, /*Binary=*/true,
|
|
|
|
/*RemoveFileOnSignal=*/false, InFile,
|
2019-07-16 12:46:31 +08:00
|
|
|
/*Extension=*/"", /*UseTemporary=*/true,
|
2016-08-26 08:14:38 +08:00
|
|
|
/*CreateMissingDirectories=*/true);
|
|
|
|
}
|
2015-04-10 21:14:31 +08:00
|
|
|
|
2017-06-09 09:36:10 +08:00
|
|
|
bool GenerateModuleInterfaceAction::BeginSourceFileAction(
|
|
|
|
CompilerInstance &CI) {
|
2019-04-14 19:11:37 +08:00
|
|
|
if (!CI.getLangOpts().ModulesTS && !CI.getLangOpts().CPlusPlusModules) {
|
|
|
|
CI.getDiagnostics().Report(diag::err_module_interface_requires_cpp_modules);
|
2016-08-26 08:14:38 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleInterface);
|
|
|
|
|
2017-06-09 09:36:10 +08:00
|
|
|
return GenerateModuleAction::BeginSourceFileAction(CI);
|
2016-08-26 08:14:38 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<raw_pwrite_stream>
|
|
|
|
GenerateModuleInterfaceAction::CreateOutputFile(CompilerInstance &CI,
|
|
|
|
StringRef InFile) {
|
|
|
|
return CI.createDefaultOutputFile(/*Binary=*/true, InFile, "pcm");
|
2018-09-15 09:21:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GenerateHeaderModuleAction::PrepareToExecuteAction(
|
|
|
|
CompilerInstance &CI) {
|
2019-04-14 19:11:37 +08:00
|
|
|
if (!CI.getLangOpts().Modules) {
|
2018-09-15 09:21:15 +08:00
|
|
|
CI.getDiagnostics().Report(diag::err_header_module_requires_modules);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto &Inputs = CI.getFrontendOpts().Inputs;
|
|
|
|
if (Inputs.empty())
|
|
|
|
return GenerateModuleAction::BeginInvocation(CI);
|
|
|
|
|
|
|
|
auto Kind = Inputs[0].getKind();
|
|
|
|
|
|
|
|
// Convert the header file inputs into a single module input buffer.
|
|
|
|
SmallString<256> HeaderContents;
|
|
|
|
ModuleHeaders.reserve(Inputs.size());
|
|
|
|
for (const FrontendInputFile &FIF : Inputs) {
|
|
|
|
// FIXME: We should support re-compiling from an AST file.
|
|
|
|
if (FIF.getKind().getFormat() != InputKind::Source || !FIF.isFile()) {
|
|
|
|
CI.getDiagnostics().Report(diag::err_module_header_file_not_found)
|
|
|
|
<< (FIF.isFile() ? FIF.getFile()
|
|
|
|
: FIF.getBuffer()->getBufferIdentifier());
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
HeaderContents += "#include \"";
|
|
|
|
HeaderContents += FIF.getFile();
|
|
|
|
HeaderContents += "\"\n";
|
|
|
|
ModuleHeaders.push_back(FIF.getFile());
|
|
|
|
}
|
|
|
|
Buffer = llvm::MemoryBuffer::getMemBufferCopy(
|
|
|
|
HeaderContents, Module::getModuleInputBufferName());
|
|
|
|
|
|
|
|
// Set that buffer up as our "real" input.
|
|
|
|
Inputs.clear();
|
|
|
|
Inputs.push_back(FrontendInputFile(Buffer.get(), Kind, /*IsSystem*/false));
|
|
|
|
|
|
|
|
return GenerateModuleAction::PrepareToExecuteAction(CI);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool GenerateHeaderModuleAction::BeginSourceFileAction(
|
|
|
|
CompilerInstance &CI) {
|
|
|
|
CI.getLangOpts().setCompilingModule(LangOptions::CMK_HeaderModule);
|
|
|
|
|
|
|
|
// Synthesize a Module object for the given headers.
|
|
|
|
auto &HS = CI.getPreprocessor().getHeaderSearchInfo();
|
|
|
|
SmallVector<Module::Header, 16> Headers;
|
|
|
|
for (StringRef Name : ModuleHeaders) {
|
|
|
|
const DirectoryLookup *CurDir = nullptr;
|
Introduce FileEntryRef and use it when handling includes to report correct dependencies
when the FileManager is reused across invocations
This commit introduces a parallel API to FileManager's getFile: getFileEntryRef, which returns
a reference to the FileEntry, and the name that was used to access the file. In the case of
a VFS with 'use-external-names', the FileEntyRef contains the external name of the file,
not the filename that was used to access it.
The new API is adopted only in the HeaderSearch and Preprocessor for include file lookup, so that the
accessed path can be propagated to SourceManager's FileInfo. SourceManager's FileInfo now can report this accessed path, using
the new getName method. This API is then adopted in the dependency collector, which now correctly reports dependencies when a file
is included both using a symlink and a real path in the case when the FileManager is reused across multiple Preprocessor invocations.
Note that this patch does not fix all dependency collector issues, as the same problem is still present in other cases when dependencies
are obtained using FileSkipped, InclusionDirective, and HasInclude. This will be fixed in follow-up commits.
Differential Revision: https://reviews.llvm.org/D65907
llvm-svn: 369680
2019-08-23 02:15:50 +08:00
|
|
|
Optional<FileEntryRef> FE = HS.LookupFile(
|
|
|
|
Name, SourceLocation(), /*Angled*/ false, nullptr, CurDir, None,
|
|
|
|
nullptr, nullptr, nullptr, nullptr, nullptr, nullptr);
|
2018-09-15 09:21:15 +08:00
|
|
|
if (!FE) {
|
|
|
|
CI.getDiagnostics().Report(diag::err_module_header_file_not_found)
|
|
|
|
<< Name;
|
|
|
|
continue;
|
|
|
|
}
|
Introduce FileEntryRef and use it when handling includes to report correct dependencies
when the FileManager is reused across invocations
This commit introduces a parallel API to FileManager's getFile: getFileEntryRef, which returns
a reference to the FileEntry, and the name that was used to access the file. In the case of
a VFS with 'use-external-names', the FileEntyRef contains the external name of the file,
not the filename that was used to access it.
The new API is adopted only in the HeaderSearch and Preprocessor for include file lookup, so that the
accessed path can be propagated to SourceManager's FileInfo. SourceManager's FileInfo now can report this accessed path, using
the new getName method. This API is then adopted in the dependency collector, which now correctly reports dependencies when a file
is included both using a symlink and a real path in the case when the FileManager is reused across multiple Preprocessor invocations.
Note that this patch does not fix all dependency collector issues, as the same problem is still present in other cases when dependencies
are obtained using FileSkipped, InclusionDirective, and HasInclude. This will be fixed in follow-up commits.
Differential Revision: https://reviews.llvm.org/D65907
llvm-svn: 369680
2019-08-23 02:15:50 +08:00
|
|
|
Headers.push_back({Name, &FE->getFileEntry()});
|
2018-09-15 09:21:15 +08:00
|
|
|
}
|
|
|
|
HS.getModuleMap().createHeaderModule(CI.getLangOpts().CurrentModule, Headers);
|
|
|
|
|
|
|
|
return GenerateModuleAction::BeginSourceFileAction(CI);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<raw_pwrite_stream>
|
|
|
|
GenerateHeaderModuleAction::CreateOutputFile(CompilerInstance &CI,
|
|
|
|
StringRef InFile) {
|
|
|
|
return CI.createDefaultOutputFile(/*Binary=*/true, InFile, "pcm");
|
2011-11-16 08:09:06 +08:00
|
|
|
}
|
|
|
|
|
2016-05-13 03:51:18 +08:00
|
|
|
SyntaxOnlyAction::~SyntaxOnlyAction() {
|
|
|
|
}
|
|
|
|
|
2014-08-11 03:56:51 +08:00
|
|
|
std::unique_ptr<ASTConsumer>
|
|
|
|
SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
|
2019-08-15 07:04:18 +08:00
|
|
|
return std::make_unique<ASTConsumer>();
|
2009-11-14 18:42:46 +08:00
|
|
|
}
|
|
|
|
|
2014-08-11 03:56:51 +08:00
|
|
|
std::unique_ptr<ASTConsumer>
|
|
|
|
DumpModuleInfoAction::CreateASTConsumer(CompilerInstance &CI,
|
|
|
|
StringRef InFile) {
|
2019-08-15 07:04:18 +08:00
|
|
|
return std::make_unique<ASTConsumer>();
|
2013-03-28 00:47:18 +08:00
|
|
|
}
|
|
|
|
|
2014-08-11 03:56:51 +08:00
|
|
|
std::unique_ptr<ASTConsumer>
|
|
|
|
VerifyPCHAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
|
2019-08-15 07:04:18 +08:00
|
|
|
return std::make_unique<ASTConsumer>();
|
2014-02-06 06:21:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void VerifyPCHAction::ExecuteAction() {
|
2014-02-08 01:31:11 +08:00
|
|
|
CompilerInstance &CI = getCompilerInstance();
|
|
|
|
bool Preamble = CI.getPreprocessorOpts().PrecompiledPreambleBytes.first != 0;
|
|
|
|
const std::string &Sysroot = CI.getHeaderSearchOpts().Sysroot;
|
2015-06-21 02:53:08 +08:00
|
|
|
std::unique_ptr<ASTReader> Reader(new ASTReader(
|
2019-03-10 01:33:56 +08:00
|
|
|
CI.getPreprocessor(), CI.getModuleCache(), &CI.getASTContext(),
|
|
|
|
CI.getPCHContainerReader(), CI.getFrontendOpts().ModuleFileExtensions,
|
2015-06-21 02:53:08 +08:00
|
|
|
Sysroot.empty() ? "" : Sysroot.c_str(),
|
|
|
|
/*DisableValidation*/ false,
|
|
|
|
/*AllowPCHWithCompilerErrors*/ false,
|
|
|
|
/*AllowConfigurationMismatch*/ true,
|
|
|
|
/*ValidateSystemInputs*/ true));
|
2014-02-08 01:31:11 +08:00
|
|
|
|
|
|
|
Reader->ReadAST(getCurrentFile(),
|
|
|
|
Preamble ? serialization::MK_Preamble
|
|
|
|
: serialization::MK_PCH,
|
|
|
|
SourceLocation(),
|
|
|
|
ASTReader::ARR_ConfigurationMismatch);
|
2014-02-06 06:21:15 +08:00
|
|
|
}
|
|
|
|
|
2018-02-10 22:04:45 +08:00
|
|
|
namespace {
|
|
|
|
struct TemplightEntry {
|
|
|
|
std::string Name;
|
|
|
|
std::string Kind;
|
|
|
|
std::string Event;
|
|
|
|
std::string DefinitionLocation;
|
|
|
|
std::string PointOfInstantiation;
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
namespace yaml {
|
|
|
|
template <> struct MappingTraits<TemplightEntry> {
|
|
|
|
static void mapping(IO &io, TemplightEntry &fields) {
|
|
|
|
io.mapRequired("name", fields.Name);
|
|
|
|
io.mapRequired("kind", fields.Kind);
|
|
|
|
io.mapRequired("event", fields.Event);
|
|
|
|
io.mapRequired("orig", fields.DefinitionLocation);
|
|
|
|
io.mapRequired("poi", fields.PointOfInstantiation);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace yaml
|
|
|
|
} // namespace llvm
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class DefaultTemplateInstCallback : public TemplateInstantiationCallback {
|
|
|
|
using CodeSynthesisContext = Sema::CodeSynthesisContext;
|
|
|
|
|
|
|
|
public:
|
2018-02-10 22:26:53 +08:00
|
|
|
void initialize(const Sema &) override {}
|
2018-02-10 22:04:45 +08:00
|
|
|
|
2018-02-10 22:26:53 +08:00
|
|
|
void finalize(const Sema &) override {}
|
2018-02-10 22:04:45 +08:00
|
|
|
|
2018-02-10 22:26:53 +08:00
|
|
|
void atTemplateBegin(const Sema &TheSema,
|
|
|
|
const CodeSynthesisContext &Inst) override {
|
2018-02-10 22:04:45 +08:00
|
|
|
displayTemplightEntry<true>(llvm::outs(), TheSema, Inst);
|
|
|
|
}
|
|
|
|
|
2018-02-10 22:26:53 +08:00
|
|
|
void atTemplateEnd(const Sema &TheSema,
|
|
|
|
const CodeSynthesisContext &Inst) override {
|
2018-02-10 22:04:45 +08:00
|
|
|
displayTemplightEntry<false>(llvm::outs(), TheSema, Inst);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static std::string toString(CodeSynthesisContext::SynthesisKind Kind) {
|
|
|
|
switch (Kind) {
|
|
|
|
case CodeSynthesisContext::TemplateInstantiation:
|
|
|
|
return "TemplateInstantiation";
|
|
|
|
case CodeSynthesisContext::DefaultTemplateArgumentInstantiation:
|
|
|
|
return "DefaultTemplateArgumentInstantiation";
|
|
|
|
case CodeSynthesisContext::DefaultFunctionArgumentInstantiation:
|
|
|
|
return "DefaultFunctionArgumentInstantiation";
|
|
|
|
case CodeSynthesisContext::ExplicitTemplateArgumentSubstitution:
|
|
|
|
return "ExplicitTemplateArgumentSubstitution";
|
|
|
|
case CodeSynthesisContext::DeducedTemplateArgumentSubstitution:
|
|
|
|
return "DeducedTemplateArgumentSubstitution";
|
|
|
|
case CodeSynthesisContext::PriorTemplateArgumentSubstitution:
|
|
|
|
return "PriorTemplateArgumentSubstitution";
|
|
|
|
case CodeSynthesisContext::DefaultTemplateArgumentChecking:
|
|
|
|
return "DefaultTemplateArgumentChecking";
|
2018-09-06 06:30:37 +08:00
|
|
|
case CodeSynthesisContext::ExceptionSpecEvaluation:
|
|
|
|
return "ExceptionSpecEvaluation";
|
2018-02-10 22:04:45 +08:00
|
|
|
case CodeSynthesisContext::ExceptionSpecInstantiation:
|
|
|
|
return "ExceptionSpecInstantiation";
|
|
|
|
case CodeSynthesisContext::DeclaringSpecialMember:
|
|
|
|
return "DeclaringSpecialMember";
|
|
|
|
case CodeSynthesisContext::DefiningSynthesizedFunction:
|
|
|
|
return "DefiningSynthesizedFunction";
|
|
|
|
case CodeSynthesisContext::Memoization:
|
|
|
|
return "Memoization";
|
|
|
|
}
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
template <bool BeginInstantiation>
|
|
|
|
static void displayTemplightEntry(llvm::raw_ostream &Out, const Sema &TheSema,
|
|
|
|
const CodeSynthesisContext &Inst) {
|
|
|
|
std::string YAML;
|
|
|
|
{
|
|
|
|
llvm::raw_string_ostream OS(YAML);
|
|
|
|
llvm::yaml::Output YO(OS);
|
|
|
|
TemplightEntry Entry =
|
|
|
|
getTemplightEntry<BeginInstantiation>(TheSema, Inst);
|
|
|
|
llvm::yaml::EmptyContext Context;
|
|
|
|
llvm::yaml::yamlize(YO, Entry, true, Context);
|
|
|
|
}
|
|
|
|
Out << "---" << YAML << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
template <bool BeginInstantiation>
|
|
|
|
static TemplightEntry getTemplightEntry(const Sema &TheSema,
|
|
|
|
const CodeSynthesisContext &Inst) {
|
|
|
|
TemplightEntry Entry;
|
|
|
|
Entry.Kind = toString(Inst.Kind);
|
|
|
|
Entry.Event = BeginInstantiation ? "Begin" : "End";
|
|
|
|
if (auto *NamedTemplate = dyn_cast_or_null<NamedDecl>(Inst.Entity)) {
|
|
|
|
llvm::raw_string_ostream OS(Entry.Name);
|
|
|
|
NamedTemplate->getNameForDiagnostic(OS, TheSema.getLangOpts(), true);
|
2018-07-31 03:24:48 +08:00
|
|
|
const PresumedLoc DefLoc =
|
2018-02-10 22:04:45 +08:00
|
|
|
TheSema.getSourceManager().getPresumedLoc(Inst.Entity->getLocation());
|
|
|
|
if(!DefLoc.isInvalid())
|
|
|
|
Entry.DefinitionLocation = std::string(DefLoc.getFilename()) + ":" +
|
|
|
|
std::to_string(DefLoc.getLine()) + ":" +
|
|
|
|
std::to_string(DefLoc.getColumn());
|
|
|
|
}
|
|
|
|
const PresumedLoc PoiLoc =
|
|
|
|
TheSema.getSourceManager().getPresumedLoc(Inst.PointOfInstantiation);
|
|
|
|
if (!PoiLoc.isInvalid()) {
|
|
|
|
Entry.PointOfInstantiation = std::string(PoiLoc.getFilename()) + ":" +
|
|
|
|
std::to_string(PoiLoc.getLine()) + ":" +
|
|
|
|
std::to_string(PoiLoc.getColumn());
|
|
|
|
}
|
|
|
|
return Entry;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
std::unique_ptr<ASTConsumer>
|
|
|
|
TemplightDumpAction::CreateASTConsumer(CompilerInstance &CI, StringRef InFile) {
|
2019-08-15 07:04:18 +08:00
|
|
|
return std::make_unique<ASTConsumer>();
|
2018-02-10 22:04:45 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void TemplightDumpAction::ExecuteAction() {
|
|
|
|
CompilerInstance &CI = getCompilerInstance();
|
|
|
|
|
|
|
|
// This part is normally done by ASTFrontEndAction, but needs to happen
|
|
|
|
// before Templight observers can be created
|
|
|
|
// FIXME: Move the truncation aspect of this into Sema, we delayed this till
|
|
|
|
// here so the source manager would be initialized.
|
|
|
|
EnsureSemaIsCreated(CI, *this);
|
|
|
|
|
|
|
|
CI.getSema().TemplateInstCallbacks.push_back(
|
2019-08-15 07:04:18 +08:00
|
|
|
std::make_unique<DefaultTemplateInstCallback>());
|
2018-02-10 22:04:45 +08:00
|
|
|
ASTFrontendAction::ExecuteAction();
|
|
|
|
}
|
|
|
|
|
2013-03-28 00:47:18 +08:00
|
|
|
namespace {
|
2018-05-09 09:00:01 +08:00
|
|
|
/// AST reader listener that dumps module information for a module
|
2013-03-28 00:47:18 +08:00
|
|
|
/// file.
|
|
|
|
class DumpModuleInfoListener : public ASTReaderListener {
|
|
|
|
llvm::raw_ostream &Out;
|
|
|
|
|
|
|
|
public:
|
|
|
|
DumpModuleInfoListener(llvm::raw_ostream &Out) : Out(Out) { }
|
|
|
|
|
|
|
|
#define DUMP_BOOLEAN(Value, Text) \
|
|
|
|
Out.indent(4) << Text << ": " << (Value? "Yes" : "No") << "\n"
|
|
|
|
|
2014-03-13 14:07:04 +08:00
|
|
|
bool ReadFullVersionInformation(StringRef FullVersion) override {
|
2013-03-28 00:47:18 +08:00
|
|
|
Out.indent(2)
|
|
|
|
<< "Generated by "
|
|
|
|
<< (FullVersion == getClangFullRepositoryVersion()? "this"
|
|
|
|
: "a different")
|
|
|
|
<< " Clang: " << FullVersion << "\n";
|
|
|
|
return ASTReaderListener::ReadFullVersionInformation(FullVersion);
|
|
|
|
}
|
|
|
|
|
2014-04-15 06:12:44 +08:00
|
|
|
void ReadModuleName(StringRef ModuleName) override {
|
|
|
|
Out.indent(2) << "Module name: " << ModuleName << "\n";
|
|
|
|
}
|
|
|
|
void ReadModuleMapFile(StringRef ModuleMapPath) override {
|
|
|
|
Out.indent(2) << "Module map file: " << ModuleMapPath << "\n";
|
|
|
|
}
|
|
|
|
|
2014-10-31 10:28:58 +08:00
|
|
|
bool ReadLanguageOptions(const LangOptions &LangOpts, bool Complain,
|
|
|
|
bool AllowCompatibleDifferences) override {
|
2013-03-28 00:47:18 +08:00
|
|
|
Out.indent(2) << "Language options:\n";
|
|
|
|
#define LANGOPT(Name, Bits, Default, Description) \
|
|
|
|
DUMP_BOOLEAN(LangOpts.Name, Description);
|
|
|
|
#define ENUM_LANGOPT(Name, Type, Bits, Default, Description) \
|
|
|
|
Out.indent(4) << Description << ": " \
|
|
|
|
<< static_cast<unsigned>(LangOpts.get##Name()) << "\n";
|
|
|
|
#define VALUE_LANGOPT(Name, Bits, Default, Description) \
|
|
|
|
Out.indent(4) << Description << ": " << LangOpts.Name << "\n";
|
|
|
|
#define BENIGN_LANGOPT(Name, Bits, Default, Description)
|
|
|
|
#define BENIGN_ENUM_LANGOPT(Name, Type, Bits, Default, Description)
|
|
|
|
#include "clang/Basic/LangOptions.def"
|
2015-06-24 02:20:18 +08:00
|
|
|
|
|
|
|
if (!LangOpts.ModuleFeatures.empty()) {
|
|
|
|
Out.indent(4) << "Module features:\n";
|
|
|
|
for (StringRef Feature : LangOpts.ModuleFeatures)
|
|
|
|
Out.indent(6) << Feature << "\n";
|
|
|
|
}
|
|
|
|
|
2013-03-28 00:47:18 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-14 12:47:43 +08:00
|
|
|
bool ReadTargetOptions(const TargetOptions &TargetOpts, bool Complain,
|
|
|
|
bool AllowCompatibleDifferences) override {
|
2013-03-28 00:47:18 +08:00
|
|
|
Out.indent(2) << "Target options:\n";
|
|
|
|
Out.indent(4) << " Triple: " << TargetOpts.Triple << "\n";
|
|
|
|
Out.indent(4) << " CPU: " << TargetOpts.CPU << "\n";
|
|
|
|
Out.indent(4) << " ABI: " << TargetOpts.ABI << "\n";
|
|
|
|
|
|
|
|
if (!TargetOpts.FeaturesAsWritten.empty()) {
|
|
|
|
Out.indent(4) << "Target features:\n";
|
|
|
|
for (unsigned I = 0, N = TargetOpts.FeaturesAsWritten.size();
|
|
|
|
I != N; ++I) {
|
|
|
|
Out.indent(6) << TargetOpts.FeaturesAsWritten[I] << "\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-11 10:00:23 +08:00
|
|
|
bool ReadDiagnosticOptions(IntrusiveRefCntPtr<DiagnosticOptions> DiagOpts,
|
|
|
|
bool Complain) override {
|
2014-04-30 00:25:26 +08:00
|
|
|
Out.indent(2) << "Diagnostic options:\n";
|
|
|
|
#define DIAGOPT(Name, Bits, Default) DUMP_BOOLEAN(DiagOpts->Name, #Name);
|
|
|
|
#define ENUM_DIAGOPT(Name, Type, Bits, Default) \
|
|
|
|
Out.indent(4) << #Name << ": " << DiagOpts->get##Name() << "\n";
|
|
|
|
#define VALUE_DIAGOPT(Name, Bits, Default) \
|
|
|
|
Out.indent(4) << #Name << ": " << DiagOpts->Name << "\n";
|
|
|
|
#include "clang/Basic/DiagnosticOptions.def"
|
|
|
|
|
Use -Rblah, not -Wblah, to control remark diagnostics. This was always the
intent when we added remark support, but was never implemented in the general
case, because the first -R flags didn't need it. (-Rpass= had special handling
to accomodate its argument.)
-Rno-foo, -Reverything, and -Rno-everything can be used to turn off a remark,
or to turn on or off all remarks. Per discussion on cfe-commits, -Weverything
does not affect remarks, and -Reverything does not affect warnings or errors.
The only "real" -R flag we have right now is -Rmodule-build; that flag is
effectively renamed from -Wmodule-build to -Rmodule-build by this change.
-Wpass and -Wno-pass (and their friends) are also renamed to -Rpass and
-Rno-pass by this change; it's not completely clear whether we intended to have
a -Rpass (with no =pattern), but that is unchanged by this commit, other than
the flag name. The default pattern is effectively one which matches no passes.
In future, we may want to make the default pattern be .*, so that -Reverything
works for -Rpass properly.
llvm-svn: 215046
2014-08-07 08:24:21 +08:00
|
|
|
Out.indent(4) << "Diagnostic flags:\n";
|
|
|
|
for (const std::string &Warning : DiagOpts->Warnings)
|
2014-04-30 00:25:26 +08:00
|
|
|
Out.indent(6) << "-W" << Warning << "\n";
|
Use -Rblah, not -Wblah, to control remark diagnostics. This was always the
intent when we added remark support, but was never implemented in the general
case, because the first -R flags didn't need it. (-Rpass= had special handling
to accomodate its argument.)
-Rno-foo, -Reverything, and -Rno-everything can be used to turn off a remark,
or to turn on or off all remarks. Per discussion on cfe-commits, -Weverything
does not affect remarks, and -Reverything does not affect warnings or errors.
The only "real" -R flag we have right now is -Rmodule-build; that flag is
effectively renamed from -Wmodule-build to -Rmodule-build by this change.
-Wpass and -Wno-pass (and their friends) are also renamed to -Rpass and
-Rno-pass by this change; it's not completely clear whether we intended to have
a -Rpass (with no =pattern), but that is unchanged by this commit, other than
the flag name. The default pattern is effectively one which matches no passes.
In future, we may want to make the default pattern be .*, so that -Reverything
works for -Rpass properly.
llvm-svn: 215046
2014-08-07 08:24:21 +08:00
|
|
|
for (const std::string &Remark : DiagOpts->Remarks)
|
|
|
|
Out.indent(6) << "-R" << Remark << "\n";
|
2014-04-30 00:25:26 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-13 14:07:04 +08:00
|
|
|
bool ReadHeaderSearchOptions(const HeaderSearchOptions &HSOpts,
|
2015-02-20 04:12:20 +08:00
|
|
|
StringRef SpecificModuleCachePath,
|
2014-03-13 14:07:04 +08:00
|
|
|
bool Complain) override {
|
2013-03-28 00:47:18 +08:00
|
|
|
Out.indent(2) << "Header search options:\n";
|
|
|
|
Out.indent(4) << "System root [-isysroot=]: '" << HSOpts.Sysroot << "'\n";
|
2017-02-26 02:14:31 +08:00
|
|
|
Out.indent(4) << "Resource dir [ -resource-dir=]: '" << HSOpts.ResourceDir << "'\n";
|
2015-02-20 04:12:20 +08:00
|
|
|
Out.indent(4) << "Module Cache: '" << SpecificModuleCachePath << "'\n";
|
2013-03-28 00:47:18 +08:00
|
|
|
DUMP_BOOLEAN(HSOpts.UseBuiltinIncludes,
|
|
|
|
"Use builtin include directories [-nobuiltininc]");
|
|
|
|
DUMP_BOOLEAN(HSOpts.UseStandardSystemIncludes,
|
|
|
|
"Use standard system include directories [-nostdinc]");
|
|
|
|
DUMP_BOOLEAN(HSOpts.UseStandardCXXIncludes,
|
|
|
|
"Use standard C++ include directories [-nostdinc++]");
|
|
|
|
DUMP_BOOLEAN(HSOpts.UseLibcxx,
|
|
|
|
"Use libc++ (rather than libstdc++) [-stdlib=]");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-03-13 14:07:04 +08:00
|
|
|
bool ReadPreprocessorOptions(const PreprocessorOptions &PPOpts,
|
|
|
|
bool Complain,
|
|
|
|
std::string &SuggestedPredefines) override {
|
2013-03-28 00:47:18 +08:00
|
|
|
Out.indent(2) << "Preprocessor options:\n";
|
|
|
|
DUMP_BOOLEAN(PPOpts.UsePredefines,
|
|
|
|
"Uses compiler/target-specific predefines [-undef]");
|
|
|
|
DUMP_BOOLEAN(PPOpts.DetailedRecord,
|
|
|
|
"Uses detailed preprocessing record (for indexing)");
|
|
|
|
|
|
|
|
if (!PPOpts.Macros.empty()) {
|
|
|
|
Out.indent(4) << "Predefined macros:\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::vector<std::pair<std::string, bool/*isUndef*/> >::const_iterator
|
|
|
|
I = PPOpts.Macros.begin(), IEnd = PPOpts.Macros.end();
|
|
|
|
I != IEnd; ++I) {
|
|
|
|
Out.indent(6);
|
|
|
|
if (I->second)
|
|
|
|
Out << "-U";
|
|
|
|
else
|
|
|
|
Out << "-D";
|
|
|
|
Out << I->first << "\n";
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-04 02:33:07 +08:00
|
|
|
|
|
|
|
/// Indicates that a particular module file extension has been read.
|
|
|
|
void readModuleFileExtension(
|
|
|
|
const ModuleFileExtensionMetadata &Metadata) override {
|
|
|
|
Out.indent(2) << "Module file extension '"
|
|
|
|
<< Metadata.BlockName << "' " << Metadata.MajorVersion
|
|
|
|
<< "." << Metadata.MinorVersion;
|
|
|
|
if (!Metadata.UserInfo.empty()) {
|
|
|
|
Out << ": ";
|
|
|
|
Out.write_escaped(Metadata.UserInfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
Out << "\n";
|
|
|
|
}
|
2018-07-18 14:49:33 +08:00
|
|
|
|
|
|
|
/// Tells the \c ASTReaderListener that we want to receive the
|
|
|
|
/// input files of the AST file via \c visitInputFile.
|
|
|
|
bool needsInputFileVisitation() override { return true; }
|
|
|
|
|
|
|
|
/// Tells the \c ASTReaderListener that we want to receive the
|
|
|
|
/// input files of the AST file via \c visitInputFile.
|
|
|
|
bool needsSystemInputFileVisitation() override { return true; }
|
|
|
|
|
|
|
|
/// Indicates that the AST file contains particular input file.
|
|
|
|
///
|
|
|
|
/// \returns true to continue receiving the next input file, false to stop.
|
|
|
|
bool visitInputFile(StringRef Filename, bool isSystem,
|
|
|
|
bool isOverridden, bool isExplicitModule) override {
|
|
|
|
|
|
|
|
Out.indent(2) << "Input file: " << Filename;
|
|
|
|
|
|
|
|
if (isSystem || isOverridden || isExplicitModule) {
|
|
|
|
Out << " [";
|
|
|
|
if (isSystem) {
|
|
|
|
Out << "System";
|
|
|
|
if (isOverridden || isExplicitModule)
|
|
|
|
Out << ", ";
|
|
|
|
}
|
|
|
|
if (isOverridden) {
|
|
|
|
Out << "Overridden";
|
|
|
|
if (isExplicitModule)
|
|
|
|
Out << ", ";
|
|
|
|
}
|
|
|
|
if (isExplicitModule)
|
|
|
|
Out << "ExplicitModule";
|
|
|
|
|
|
|
|
Out << "]";
|
|
|
|
}
|
|
|
|
|
|
|
|
Out << "\n";
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-09-11 13:17:13 +08:00
|
|
|
|
|
|
|
/// Returns true if this \c ASTReaderListener wants to receive the
|
|
|
|
/// imports of the AST file via \c visitImport, false otherwise.
|
|
|
|
bool needsImportVisitation() const override { return true; }
|
|
|
|
|
|
|
|
/// If needsImportVisitation returns \c true, this is called for each
|
|
|
|
/// AST file imported by this AST file.
|
|
|
|
void visitImport(StringRef ModuleName, StringRef Filename) override {
|
|
|
|
Out.indent(2) << "Imports module '" << ModuleName
|
|
|
|
<< "': " << Filename.str() << "\n";
|
|
|
|
}
|
2013-03-28 00:47:18 +08:00
|
|
|
#undef DUMP_BOOLEAN
|
|
|
|
};
|
2015-06-23 07:07:51 +08:00
|
|
|
}
|
2013-03-28 00:47:18 +08:00
|
|
|
|
2016-08-18 07:13:53 +08:00
|
|
|
bool DumpModuleInfoAction::BeginInvocation(CompilerInstance &CI) {
|
|
|
|
// The Object file reader also supports raw ast files and there is no point in
|
|
|
|
// being strict about the module file format in -module-file-info mode.
|
|
|
|
CI.getHeaderSearchOpts().ModuleFormat = "obj";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-03-28 00:47:18 +08:00
|
|
|
void DumpModuleInfoAction::ExecuteAction() {
|
|
|
|
// Set up the output file.
|
2014-03-08 04:03:18 +08:00
|
|
|
std::unique_ptr<llvm::raw_fd_ostream> OutFile;
|
2013-03-28 00:47:18 +08:00
|
|
|
StringRef OutputFileName = getCompilerInstance().getFrontendOpts().OutputFile;
|
|
|
|
if (!OutputFileName.empty() && OutputFileName != "-") {
|
2014-08-26 02:17:04 +08:00
|
|
|
std::error_code EC;
|
|
|
|
OutFile.reset(new llvm::raw_fd_ostream(OutputFileName.str(), EC,
|
2019-08-05 13:43:48 +08:00
|
|
|
llvm::sys::fs::OF_Text));
|
2013-03-28 00:47:18 +08:00
|
|
|
}
|
|
|
|
llvm::raw_ostream &Out = OutFile.get()? *OutFile.get() : llvm::outs();
|
|
|
|
|
|
|
|
Out << "Information for module file '" << getCurrentFile() << "':\n";
|
2016-08-18 07:14:00 +08:00
|
|
|
auto &FileMgr = getCompilerInstance().getFileManager();
|
|
|
|
auto Buffer = FileMgr.getBufferForFile(getCurrentFile());
|
|
|
|
StringRef Magic = (*Buffer)->getMemBufferRef().getBuffer();
|
|
|
|
bool IsRaw = (Magic.size() >= 4 && Magic[0] == 'C' && Magic[1] == 'P' &&
|
|
|
|
Magic[2] == 'C' && Magic[3] == 'H');
|
|
|
|
Out << " Module format: " << (IsRaw ? "raw" : "obj") << "\n";
|
2016-08-18 07:13:53 +08:00
|
|
|
|
2016-07-27 01:12:17 +08:00
|
|
|
Preprocessor &PP = getCompilerInstance().getPreprocessor();
|
2013-03-28 00:47:18 +08:00
|
|
|
DumpModuleInfoListener Listener(Out);
|
2016-07-27 01:12:17 +08:00
|
|
|
HeaderSearchOptions &HSOpts =
|
|
|
|
PP.getHeaderSearchInfo().getHeaderSearchOpts();
|
2015-06-21 02:53:08 +08:00
|
|
|
ASTReader::readASTFileControlBlock(
|
2016-08-18 07:14:00 +08:00
|
|
|
getCurrentFile(), FileMgr, getCompilerInstance().getPCHContainerReader(),
|
2016-07-27 01:12:17 +08:00
|
|
|
/*FindModuleFileExtensions=*/true, Listener,
|
|
|
|
HSOpts.ModulesValidateDiagnosticOptions);
|
2013-03-28 00:47:18 +08:00
|
|
|
}
|
|
|
|
|
2009-11-14 18:42:57 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Preprocessor Actions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
void DumpRawTokensAction::ExecuteAction() {
|
|
|
|
Preprocessor &PP = getCompilerInstance().getPreprocessor();
|
|
|
|
SourceManager &SM = PP.getSourceManager();
|
|
|
|
|
|
|
|
// Start lexing the specified input file.
|
2009-11-30 12:18:44 +08:00
|
|
|
const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
|
2012-03-11 15:00:24 +08:00
|
|
|
Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOpts());
|
2009-11-14 18:42:57 +08:00
|
|
|
RawLex.SetKeepWhitespaceMode(true);
|
|
|
|
|
|
|
|
Token RawTok;
|
|
|
|
RawLex.LexFromRawLexer(RawTok);
|
|
|
|
while (RawTok.isNot(tok::eof)) {
|
|
|
|
PP.DumpToken(RawTok, true);
|
2009-11-25 18:27:48 +08:00
|
|
|
llvm::errs() << "\n";
|
2009-11-14 18:42:57 +08:00
|
|
|
RawLex.LexFromRawLexer(RawTok);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void DumpTokensAction::ExecuteAction() {
|
|
|
|
Preprocessor &PP = getCompilerInstance().getPreprocessor();
|
|
|
|
// Start preprocessing the specified input file.
|
|
|
|
Token Tok;
|
2010-04-21 04:35:58 +08:00
|
|
|
PP.EnterMainSourceFile();
|
2009-11-14 18:42:57 +08:00
|
|
|
do {
|
|
|
|
PP.Lex(Tok);
|
|
|
|
PP.DumpToken(Tok, true);
|
2009-11-25 18:27:48 +08:00
|
|
|
llvm::errs() << "\n";
|
2009-11-14 18:42:57 +08:00
|
|
|
} while (Tok.isNot(tok::eof));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PreprocessOnlyAction::ExecuteAction() {
|
|
|
|
Preprocessor &PP = getCompilerInstance().getPreprocessor();
|
|
|
|
|
2010-06-12 04:10:12 +08:00
|
|
|
// Ignore unknown pragmas.
|
2014-05-01 20:54:03 +08:00
|
|
|
PP.IgnorePragmas();
|
2010-06-12 04:10:12 +08:00
|
|
|
|
2009-11-14 18:42:57 +08:00
|
|
|
Token Tok;
|
|
|
|
// Start parsing the specified input file.
|
2010-04-21 04:35:58 +08:00
|
|
|
PP.EnterMainSourceFile();
|
2009-11-14 18:42:57 +08:00
|
|
|
do {
|
|
|
|
PP.Lex(Tok);
|
|
|
|
} while (Tok.isNot(tok::eof));
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrintPreprocessedAction::ExecuteAction() {
|
|
|
|
CompilerInstance &CI = getCompilerInstance();
|
2011-09-24 07:43:36 +08:00
|
|
|
// Output file may need to be set to 'Binary', to avoid converting Unix style
|
2010-01-06 01:33:23 +08:00
|
|
|
// line feeds (<LF>) to Microsoft style line feeds (<CR><LF>).
|
2011-09-24 07:43:36 +08:00
|
|
|
//
|
|
|
|
// Look to see what type of line endings the file uses. If there's a
|
|
|
|
// CRLF, then we won't open the file up in binary mode. If there is
|
|
|
|
// just an LF or CR, then we will open the file up in binary mode.
|
|
|
|
// In this fashion, the output format should match the input format, unless
|
|
|
|
// the input format has inconsistent line endings.
|
|
|
|
//
|
|
|
|
// This should be a relatively fast operation since most files won't have
|
2018-07-31 03:24:48 +08:00
|
|
|
// all of their source code on a single line. However, that is still a
|
2011-09-24 07:43:36 +08:00
|
|
|
// concern, so if we scan for too long, we'll just assume the file should
|
|
|
|
// be opened in binary mode.
|
|
|
|
bool BinaryMode = true;
|
|
|
|
bool InvalidFile = false;
|
|
|
|
const SourceManager& SM = CI.getSourceManager();
|
2018-07-31 03:24:48 +08:00
|
|
|
const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getMainFileID(),
|
2011-09-24 07:43:36 +08:00
|
|
|
&InvalidFile);
|
|
|
|
if (!InvalidFile) {
|
|
|
|
const char *cur = Buffer->getBufferStart();
|
|
|
|
const char *end = Buffer->getBufferEnd();
|
|
|
|
const char *next = (cur != end) ? cur + 1 : end;
|
|
|
|
|
|
|
|
// Limit ourselves to only scanning 256 characters into the source
|
2018-07-31 03:24:48 +08:00
|
|
|
// file. This is mostly a sanity check in case the file has no
|
2011-09-24 07:43:36 +08:00
|
|
|
// newlines whatsoever.
|
|
|
|
if (end - cur > 256) end = cur + 256;
|
2017-06-17 04:52:59 +08:00
|
|
|
|
2011-09-24 07:43:36 +08:00
|
|
|
while (next < end) {
|
|
|
|
if (*cur == 0x0D) { // CR
|
|
|
|
if (*next == 0x0A) // CRLF
|
|
|
|
BinaryMode = false;
|
|
|
|
|
|
|
|
break;
|
|
|
|
} else if (*cur == 0x0A) // LF
|
|
|
|
break;
|
|
|
|
|
2016-02-19 06:34:54 +08:00
|
|
|
++cur;
|
|
|
|
++next;
|
2011-09-24 07:43:36 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-15 08:55:40 +08:00
|
|
|
std::unique_ptr<raw_ostream> OS =
|
2018-09-15 09:21:18 +08:00
|
|
|
CI.createDefaultOutputFile(BinaryMode, getCurrentFileOrBufferName());
|
2009-12-03 17:13:30 +08:00
|
|
|
if (!OS) return;
|
|
|
|
|
2017-05-06 06:18:51 +08:00
|
|
|
// If we're preprocessing a module map, start by dumping the contents of the
|
|
|
|
// module itself before switching to the input buffer.
|
|
|
|
auto &Input = getCurrentInput();
|
|
|
|
if (Input.getKind().getFormat() == InputKind::ModuleMap) {
|
2017-06-02 04:10:35 +08:00
|
|
|
if (Input.isFile()) {
|
|
|
|
(*OS) << "# 1 \"";
|
|
|
|
OS->write_escaped(Input.getFile());
|
|
|
|
(*OS) << "\"\n";
|
|
|
|
}
|
2017-05-06 06:18:51 +08:00
|
|
|
getCurrentModule()->print(*OS);
|
|
|
|
(*OS) << "#pragma clang module contents\n";
|
|
|
|
}
|
|
|
|
|
2016-07-15 08:55:40 +08:00
|
|
|
DoPrintPreprocessedInput(CI.getPreprocessor(), OS.get(),
|
2009-11-14 18:42:57 +08:00
|
|
|
CI.getPreprocessorOutputOpts());
|
|
|
|
}
|
2010-07-21 04:18:03 +08:00
|
|
|
|
|
|
|
void PrintPreambleAction::ExecuteAction() {
|
2017-04-27 02:57:40 +08:00
|
|
|
switch (getCurrentFileKind().getLanguage()) {
|
2019-08-05 21:59:26 +08:00
|
|
|
case Language::C:
|
|
|
|
case Language::CXX:
|
|
|
|
case Language::ObjC:
|
|
|
|
case Language::ObjCXX:
|
|
|
|
case Language::OpenCL:
|
|
|
|
case Language::CUDA:
|
|
|
|
case Language::HIP:
|
2010-07-21 04:18:03 +08:00
|
|
|
break;
|
2018-07-31 03:24:48 +08:00
|
|
|
|
2019-08-05 21:59:26 +08:00
|
|
|
case Language::Unknown:
|
|
|
|
case Language::Asm:
|
|
|
|
case Language::LLVM_IR:
|
|
|
|
case Language::RenderScript:
|
2010-07-21 04:18:03 +08:00
|
|
|
// We can't do anything with these.
|
|
|
|
return;
|
|
|
|
}
|
2014-08-27 03:54:40 +08:00
|
|
|
|
2017-04-27 02:57:40 +08:00
|
|
|
// We don't expect to find any #include directives in a preprocessed input.
|
|
|
|
if (getCurrentFileKind().isPreprocessed())
|
|
|
|
return;
|
|
|
|
|
2010-11-04 06:45:23 +08:00
|
|
|
CompilerInstance &CI = getCompilerInstance();
|
2014-10-27 06:44:13 +08:00
|
|
|
auto Buffer = CI.getFileManager().getBufferForFile(getCurrentFile());
|
|
|
|
if (Buffer) {
|
2014-08-12 23:46:24 +08:00
|
|
|
unsigned Preamble =
|
2017-09-21 03:03:37 +08:00
|
|
|
Lexer::ComputePreamble((*Buffer)->getBuffer(), CI.getLangOpts()).Size;
|
2014-10-27 06:44:13 +08:00
|
|
|
llvm::outs().write((*Buffer)->getBufferStart(), Preamble);
|
2010-07-21 04:18:03 +08:00
|
|
|
}
|
|
|
|
}
|
2018-05-31 21:57:09 +08:00
|
|
|
|
|
|
|
void DumpCompilerOptionsAction::ExecuteAction() {
|
|
|
|
CompilerInstance &CI = getCompilerInstance();
|
|
|
|
std::unique_ptr<raw_ostream> OSP =
|
|
|
|
CI.createDefaultOutputFile(false, getCurrentFile());
|
|
|
|
if (!OSP)
|
|
|
|
return;
|
|
|
|
|
|
|
|
raw_ostream &OS = *OSP;
|
|
|
|
const Preprocessor &PP = CI.getPreprocessor();
|
|
|
|
const LangOptions &LangOpts = PP.getLangOpts();
|
|
|
|
|
|
|
|
// FIXME: Rather than manually format the JSON (which is awkward due to
|
|
|
|
// needing to remove trailing commas), this should make use of a JSON library.
|
|
|
|
// FIXME: Instead of printing enums as an integral value and specifying the
|
|
|
|
// type as a separate field, use introspection to print the enumerator.
|
|
|
|
|
|
|
|
OS << "{\n";
|
|
|
|
OS << "\n\"features\" : [\n";
|
|
|
|
{
|
|
|
|
llvm::SmallString<128> Str;
|
|
|
|
#define FEATURE(Name, Predicate) \
|
|
|
|
("\t{\"" #Name "\" : " + llvm::Twine(Predicate ? "true" : "false") + "},\n") \
|
|
|
|
.toVector(Str);
|
|
|
|
#include "clang/Basic/Features.def"
|
|
|
|
#undef FEATURE
|
|
|
|
// Remove the newline and comma from the last entry to ensure this remains
|
|
|
|
// valid JSON.
|
|
|
|
OS << Str.substr(0, Str.size() - 2);
|
|
|
|
}
|
|
|
|
OS << "\n],\n";
|
|
|
|
|
|
|
|
OS << "\n\"extensions\" : [\n";
|
|
|
|
{
|
|
|
|
llvm::SmallString<128> Str;
|
|
|
|
#define EXTENSION(Name, Predicate) \
|
|
|
|
("\t{\"" #Name "\" : " + llvm::Twine(Predicate ? "true" : "false") + "},\n") \
|
|
|
|
.toVector(Str);
|
|
|
|
#include "clang/Basic/Features.def"
|
|
|
|
#undef EXTENSION
|
|
|
|
// Remove the newline and comma from the last entry to ensure this remains
|
|
|
|
// valid JSON.
|
|
|
|
OS << Str.substr(0, Str.size() - 2);
|
|
|
|
}
|
|
|
|
OS << "\n]\n";
|
|
|
|
|
|
|
|
OS << "}";
|
|
|
|
}
|
2019-06-04 06:59:17 +08:00
|
|
|
|
|
|
|
void PrintDependencyDirectivesSourceMinimizerAction::ExecuteAction() {
|
|
|
|
CompilerInstance &CI = getCompilerInstance();
|
|
|
|
SourceManager &SM = CI.getPreprocessor().getSourceManager();
|
|
|
|
const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID());
|
|
|
|
|
|
|
|
llvm::SmallString<1024> Output;
|
|
|
|
llvm::SmallVector<minimize_source_to_dependency_directives::Token, 32> Toks;
|
|
|
|
if (minimizeSourceToDependencyDirectives(
|
|
|
|
FromFile->getBuffer(), Output, Toks, &CI.getDiagnostics(),
|
|
|
|
SM.getLocForStartOfFile(SM.getMainFileID()))) {
|
|
|
|
assert(CI.getDiagnostics().hasErrorOccurred() &&
|
|
|
|
"no errors reported for failure");
|
|
|
|
|
|
|
|
// Preprocess the source when verifying the diagnostics to capture the
|
|
|
|
// 'expected' comments.
|
|
|
|
if (CI.getDiagnosticOpts().VerifyDiagnostics) {
|
|
|
|
// Make sure we don't emit new diagnostics!
|
|
|
|
CI.getDiagnostics().setSuppressAllDiagnostics();
|
|
|
|
Preprocessor &PP = getCompilerInstance().getPreprocessor();
|
|
|
|
PP.EnterMainSourceFile();
|
|
|
|
Token Tok;
|
|
|
|
do {
|
|
|
|
PP.Lex(Tok);
|
|
|
|
} while (Tok.isNot(tok::eof));
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
llvm::outs() << Output;
|
|
|
|
}
|