2009-11-14 18:42:46 +08:00
|
|
|
//===--- FrontendActions.cpp ----------------------------------------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Frontend/FrontendActions.h"
|
|
|
|
#include "clang/AST/ASTConsumer.h"
|
|
|
|
#include "clang/Basic/FileManager.h"
|
|
|
|
#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"
|
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"
|
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"
|
2009-11-14 18:42:46 +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;
|
|
|
|
|
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) {
|
|
|
|
return llvm::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) {
|
2013-06-24 09:45:33 +08:00
|
|
|
return CreateASTDumper(CI.getFrontendOpts().ASTDumpFilter,
|
2014-08-12 06:11:07 +08:00
|
|
|
CI.getFrontendOpts().ASTDumpDecls,
|
2013-06-24 09:45:33 +08:00
|
|
|
CI.getFrontendOpts().ASTDumpLookups);
|
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>
|
|
|
|
DeclContextPrintAction::CreateASTConsumer(CompilerInstance &CI,
|
|
|
|
StringRef InFile) {
|
2009-11-14 18:42:46 +08:00
|
|
|
return CreateDeclContextPrinter();
|
|
|
|
}
|
|
|
|
|
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;
|
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 =
|
2015-04-10 20:54:53 +08:00
|
|
|
ComputeASTConsumerArguments(CI, InFile, Sysroot, OutputFile);
|
|
|
|
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
|
|
|
|
|
|
|
auto Buffer = std::make_shared<PCHBuffer>();
|
|
|
|
std::vector<std::unique_ptr<ASTConsumer>> Consumers;
|
|
|
|
Consumers.push_back(llvm::make_unique<PCHGenerator>(
|
2016-08-26 02:26:30 +08:00
|
|
|
CI.getPreprocessor(), OutputFile, Sysroot,
|
2016-07-13 22:21:11 +08:00
|
|
|
Buffer, CI.getFrontendOpts().ModuleFileExtensions,
|
|
|
|
/*AllowASTWithErrors*/false,
|
|
|
|
/*IncludeTimestamps*/
|
|
|
|
+CI.getFrontendOpts().IncludeTimestamps));
|
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
|
|
|
|
|
|
|
return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
|
2010-08-03 16:14:03 +08:00
|
|
|
}
|
|
|
|
|
2016-07-15 08:55:40 +08:00
|
|
|
std::unique_ptr<raw_pwrite_stream>
|
|
|
|
GeneratePCHAction::ComputeASTConsumerArguments(CompilerInstance &CI,
|
|
|
|
StringRef InFile,
|
|
|
|
std::string &Sysroot,
|
|
|
|
std::string &OutputFile) {
|
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);
|
2015-04-10 20:54:53 +08:00
|
|
|
return nullptr;
|
2009-11-14 18:42:46 +08:00
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
/*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
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2015-06-21 02:53:08 +08:00
|
|
|
Consumers.push_back(llvm::make_unique<PCHGenerator>(
|
2016-08-26 02:26:30 +08:00
|
|
|
CI.getPreprocessor(), OutputFile, Sysroot,
|
2015-11-04 02:33:07 +08:00
|
|
|
Buffer, CI.getFrontendOpts().ModuleFileExtensions,
|
|
|
|
/*AllowASTWithErrors=*/false,
|
|
|
|
/*IncludeTimestamps=*/
|
|
|
|
+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));
|
2015-06-21 02:53:08 +08:00
|
|
|
return llvm::make_unique<MultiplexConsumer>(std::move(Consumers));
|
2011-11-16 08:09:06 +08:00
|
|
|
}
|
|
|
|
|
2016-08-26 08:14:38 +08:00
|
|
|
bool GenerateModuleAction::BeginSourceFileAction(CompilerInstance &CI,
|
|
|
|
StringRef Filename) {
|
|
|
|
// Set up embedding for any specified files. Do this before we load any
|
|
|
|
// source files, including the primary module map for the compilation.
|
|
|
|
for (const auto &F : CI.getFrontendOpts().ModulesEmbedFiles) {
|
|
|
|
if (const auto *FE = CI.getFileManager().getFile(F, /*openFile*/true))
|
|
|
|
CI.getSourceManager().setFileIsTransient(FE);
|
|
|
|
else
|
|
|
|
CI.getDiagnostics().Report(diag::err_modules_embed_file_not_found) << F;
|
|
|
|
}
|
|
|
|
if (CI.getFrontendOpts().ModulesEmbedAllFiles)
|
|
|
|
CI.getSourceManager().setAllFilesAreTransient(true);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-10-10 10:12:39 +08:00
|
|
|
static SmallVectorImpl<char> &
|
|
|
|
operator+=(SmallVectorImpl<char> &Includes, StringRef RHS) {
|
|
|
|
Includes.append(RHS.begin(), RHS.end());
|
|
|
|
return Includes;
|
|
|
|
}
|
|
|
|
|
2016-03-02 14:09:18 +08:00
|
|
|
static void addHeaderInclude(StringRef HeaderName,
|
|
|
|
SmallVectorImpl<char> &Includes,
|
|
|
|
const LangOptions &LangOpts,
|
|
|
|
bool IsExternC) {
|
2014-03-08 08:03:56 +08:00
|
|
|
if (IsExternC && LangOpts.CPlusPlus)
|
2014-03-02 13:58:18 +08:00
|
|
|
Includes += "extern \"C\" {\n";
|
2012-10-10 10:12:39 +08:00
|
|
|
if (LangOpts.ObjC1)
|
|
|
|
Includes += "#import \"";
|
|
|
|
else
|
|
|
|
Includes += "#include \"";
|
2014-12-02 08:08:08 +08:00
|
|
|
|
|
|
|
Includes += HeaderName;
|
|
|
|
|
2012-10-10 10:12:39 +08:00
|
|
|
Includes += "\"\n";
|
2014-03-08 08:03:56 +08:00
|
|
|
if (IsExternC && LangOpts.CPlusPlus)
|
2014-03-02 13:58:18 +08:00
|
|
|
Includes += "}\n";
|
2012-10-10 10:12:39 +08:00
|
|
|
}
|
|
|
|
|
2011-11-17 01:04:00 +08:00
|
|
|
/// \brief Collect the set of header includes needed to construct the given
|
2012-10-05 08:22:33 +08:00
|
|
|
/// module and update the TopHeaders file set of the module.
|
2011-11-17 01:04:00 +08:00
|
|
|
///
|
|
|
|
/// \param Module The module we're collecting includes from.
|
2011-12-07 01:15:11 +08:00
|
|
|
///
|
2012-06-16 05:48:19 +08:00
|
|
|
/// \param Includes Will be augmented with the set of \#includes or \#imports
|
2011-12-07 01:15:11 +08:00
|
|
|
/// needed to load all of the named headers.
|
2014-06-12 22:02:15 +08:00
|
|
|
static std::error_code
|
2014-03-11 10:02:47 +08:00
|
|
|
collectModuleHeaderIncludes(const LangOptions &LangOpts, FileManager &FileMgr,
|
|
|
|
ModuleMap &ModMap, clang::Module *Module,
|
|
|
|
SmallVectorImpl<char> &Includes) {
|
2011-12-31 12:05:44 +08:00
|
|
|
// Don't collect any headers for unavailable modules.
|
|
|
|
if (!Module->isAvailable())
|
2014-06-12 22:02:15 +08:00
|
|
|
return std::error_code();
|
2011-12-31 12:05:44 +08:00
|
|
|
|
2011-12-07 01:15:11 +08:00
|
|
|
// Add includes for each of these headers.
|
2016-01-09 06:36:45 +08:00
|
|
|
for (auto HK : {Module::HK_Normal, Module::HK_Private}) {
|
|
|
|
for (Module::Header &H : Module->Headers[HK]) {
|
|
|
|
Module->addTopHeader(H.Entry);
|
|
|
|
// Use the path as specified in the module map file. We'll look for this
|
|
|
|
// file relative to the module build directory (the directory containing
|
|
|
|
// the module map file) so this will find the same file that we found
|
|
|
|
// while parsing the module map.
|
2016-03-02 14:09:18 +08:00
|
|
|
addHeaderInclude(H.NameAsWritten, Includes, LangOpts, Module->IsExternC);
|
2016-01-09 06:36:45 +08:00
|
|
|
}
|
2012-02-03 02:42:48 +08:00
|
|
|
}
|
2013-06-21 05:14:14 +08:00
|
|
|
// Note that Module->PrivateHeaders will not be a TopHeader.
|
2011-12-07 01:15:11 +08:00
|
|
|
|
2015-05-16 10:28:53 +08:00
|
|
|
if (Module::Header UmbrellaHeader = Module->getUmbrellaHeader()) {
|
|
|
|
Module->addTopHeader(UmbrellaHeader.Entry);
|
2016-03-02 14:09:18 +08:00
|
|
|
if (Module->Parent)
|
2012-02-03 02:42:48 +08:00
|
|
|
// Include the umbrella header for submodules.
|
2016-03-02 14:09:18 +08:00
|
|
|
addHeaderInclude(UmbrellaHeader.NameAsWritten, Includes, LangOpts,
|
|
|
|
Module->IsExternC);
|
2015-05-16 10:28:53 +08:00
|
|
|
} else if (Module::DirectoryName UmbrellaDir = Module->getUmbrellaDir()) {
|
2012-01-05 08:04:05 +08:00
|
|
|
// Add all of the headers we find in this subdirectory.
|
2014-06-12 22:02:15 +08:00
|
|
|
std::error_code EC;
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<128> DirNative;
|
2015-05-16 10:28:53 +08:00
|
|
|
llvm::sys::path::native(UmbrellaDir.Entry->getName(), DirNative);
|
2016-05-17 00:46:01 +08:00
|
|
|
|
|
|
|
vfs::FileSystem &FS = *FileMgr.getVirtualFileSystem();
|
|
|
|
for (vfs::recursive_directory_iterator Dir(FS, DirNative, EC), End;
|
|
|
|
Dir != End && !EC; Dir.increment(EC)) {
|
2011-12-09 03:11:24 +08:00
|
|
|
// Check whether this entry has an extension typically associated with
|
|
|
|
// headers.
|
2016-05-17 00:46:01 +08:00
|
|
|
if (!llvm::StringSwitch<bool>(llvm::sys::path::extension(Dir->getName()))
|
2011-12-09 03:11:24 +08:00
|
|
|
.Cases(".h", ".H", ".hh", ".hpp", true)
|
|
|
|
.Default(false))
|
|
|
|
continue;
|
2014-12-02 08:08:08 +08:00
|
|
|
|
2016-05-17 00:46:01 +08:00
|
|
|
const FileEntry *Header = FileMgr.getFile(Dir->getName());
|
2014-12-02 08:08:08 +08:00
|
|
|
// FIXME: This shouldn't happen unless there is a file system race. Is
|
|
|
|
// that worth diagnosing?
|
|
|
|
if (!Header)
|
|
|
|
continue;
|
|
|
|
|
2012-01-05 08:04:05 +08:00
|
|
|
// If this header is marked 'unavailable' in this module, don't include
|
|
|
|
// it.
|
2014-12-02 08:08:08 +08:00
|
|
|
if (ModMap.isHeaderUnavailableInModule(Header, Module))
|
|
|
|
continue;
|
|
|
|
|
2015-05-16 10:28:53 +08:00
|
|
|
// Compute the relative path from the directory to this file.
|
|
|
|
SmallVector<StringRef, 16> Components;
|
2016-05-17 00:46:01 +08:00
|
|
|
auto PathIt = llvm::sys::path::rbegin(Dir->getName());
|
2015-05-16 10:28:53 +08:00
|
|
|
for (int I = 0; I != Dir.level() + 1; ++I, ++PathIt)
|
|
|
|
Components.push_back(*PathIt);
|
|
|
|
SmallString<128> RelativeHeader(UmbrellaDir.NameAsWritten);
|
|
|
|
for (auto It = Components.rbegin(), End = Components.rend(); It != End;
|
|
|
|
++It)
|
|
|
|
llvm::sys::path::append(RelativeHeader, *It);
|
|
|
|
|
2014-03-11 10:02:47 +08:00
|
|
|
// Include this header as part of the umbrella directory.
|
2014-12-02 08:08:08 +08:00
|
|
|
Module->addTopHeader(Header);
|
2016-03-02 14:09:18 +08:00
|
|
|
addHeaderInclude(RelativeHeader, Includes, LangOpts, Module->IsExternC);
|
2011-12-09 03:11:24 +08:00
|
|
|
}
|
2014-03-11 10:02:47 +08:00
|
|
|
|
|
|
|
if (EC)
|
|
|
|
return EC;
|
2011-11-17 01:04:00 +08:00
|
|
|
}
|
2014-12-02 08:08:08 +08:00
|
|
|
|
2011-11-17 01:04:00 +08:00
|
|
|
// Recurse into submodules.
|
2012-01-05 07:32:19 +08:00
|
|
|
for (clang::Module::submodule_iterator Sub = Module->submodule_begin(),
|
|
|
|
SubEnd = Module->submodule_end();
|
2011-12-07 01:15:11 +08:00
|
|
|
Sub != SubEnd; ++Sub)
|
2014-06-12 22:02:15 +08:00
|
|
|
if (std::error_code Err = collectModuleHeaderIncludes(
|
2014-03-11 10:02:47 +08:00
|
|
|
LangOpts, FileMgr, ModMap, *Sub, Includes))
|
|
|
|
return Err;
|
|
|
|
|
2014-06-12 22:02:15 +08:00
|
|
|
return std::error_code();
|
2011-11-17 01:04:00 +08:00
|
|
|
}
|
|
|
|
|
2016-08-26 08:14:38 +08:00
|
|
|
bool GenerateModuleFromModuleMapAction::BeginSourceFileAction(
|
|
|
|
CompilerInstance &CI, StringRef Filename) {
|
|
|
|
CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleMap);
|
|
|
|
|
|
|
|
if (!GenerateModuleAction::BeginSourceFileAction(CI, Filename))
|
|
|
|
return false;
|
2016-02-20 06:25:36 +08:00
|
|
|
|
2015-08-14 13:02:58 +08:00
|
|
|
// Find the module map file.
|
|
|
|
const FileEntry *ModuleMap =
|
|
|
|
CI.getFileManager().getFile(Filename, /*openFile*/true);
|
2011-11-16 08:09:06 +08:00
|
|
|
if (!ModuleMap) {
|
|
|
|
CI.getDiagnostics().Report(diag::err_module_map_not_found)
|
|
|
|
<< Filename;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse the module map file.
|
|
|
|
HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
|
2013-06-22 00:28:10 +08:00
|
|
|
if (HS.loadModuleMapFile(ModuleMap, IsSystem))
|
2011-11-16 08:09:06 +08:00
|
|
|
return false;
|
|
|
|
|
|
|
|
if (CI.getLangOpts().CurrentModule.empty()) {
|
|
|
|
CI.getDiagnostics().Report(diag::err_missing_module_name);
|
|
|
|
|
|
|
|
// FIXME: Eventually, we could consider asking whether there was just
|
|
|
|
// a single module described in the module map, and use that as a
|
|
|
|
// default. Then it would be fairly trivial to just "compile" a module
|
|
|
|
// map with a single module (the common case).
|
|
|
|
return false;
|
|
|
|
}
|
2014-03-06 04:55:36 +08:00
|
|
|
|
|
|
|
// If we're being run from the command-line, the module build stack will not
|
|
|
|
// have been filled in yet, so complete it now in order to allow us to detect
|
|
|
|
// module cycles.
|
|
|
|
SourceManager &SourceMgr = CI.getSourceManager();
|
|
|
|
if (SourceMgr.getModuleBuildStack().empty())
|
|
|
|
SourceMgr.pushModuleBuildStack(CI.getLangOpts().CurrentModule,
|
|
|
|
FullSourceLoc(SourceLocation(), SourceMgr));
|
|
|
|
|
2011-11-16 08:09:06 +08:00
|
|
|
// Dig out the module definition.
|
2012-01-30 01:08:11 +08:00
|
|
|
Module = HS.lookupModule(CI.getLangOpts().CurrentModule,
|
|
|
|
/*AllowSearch=*/false);
|
2011-11-16 08:09:06 +08:00
|
|
|
if (!Module) {
|
|
|
|
CI.getDiagnostics().Report(diag::err_missing_module)
|
|
|
|
<< CI.getLangOpts().CurrentModule << Filename;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2011-12-31 12:05:44 +08:00
|
|
|
|
|
|
|
// Check whether we can build this module at all.
|
2013-10-29 06:18:19 +08:00
|
|
|
clang::Module::Requirement Requirement;
|
2014-12-02 08:08:08 +08:00
|
|
|
clang::Module::UnresolvedHeaderDirective MissingHeader;
|
2013-12-17 18:31:37 +08:00
|
|
|
if (!Module->isAvailable(CI.getLangOpts(), CI.getTarget(), Requirement,
|
|
|
|
MissingHeader)) {
|
|
|
|
if (MissingHeader.FileNameLoc.isValid()) {
|
2014-04-19 06:07:31 +08:00
|
|
|
CI.getDiagnostics().Report(MissingHeader.FileNameLoc,
|
|
|
|
diag::err_module_header_missing)
|
2013-12-17 18:31:37 +08:00
|
|
|
<< MissingHeader.IsUmbrella << MissingHeader.FileName;
|
|
|
|
} else {
|
|
|
|
CI.getDiagnostics().Report(diag::err_module_unavailable)
|
|
|
|
<< Module->getFullModuleName()
|
|
|
|
<< Requirement.second << Requirement.first;
|
|
|
|
}
|
2011-12-31 12:05:44 +08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-08-09 08:57:23 +08:00
|
|
|
if (ModuleMapForUniquing && ModuleMapForUniquing != ModuleMap) {
|
|
|
|
Module->IsInferred = true;
|
|
|
|
HS.getModuleMap().setInferredModuleAllowedBy(Module, ModuleMapForUniquing);
|
|
|
|
} else {
|
2014-04-15 02:00:01 +08:00
|
|
|
ModuleMapForUniquing = ModuleMap;
|
2014-08-09 08:57:23 +08:00
|
|
|
}
|
2014-04-15 02:00:01 +08:00
|
|
|
|
2012-10-10 10:12:39 +08:00
|
|
|
FileManager &FileMgr = CI.getFileManager();
|
|
|
|
|
2011-11-17 01:04:00 +08:00
|
|
|
// Collect the set of #includes we need to build the module.
|
2012-02-05 10:13:05 +08:00
|
|
|
SmallString<256> HeaderContents;
|
2014-06-13 04:37:59 +08:00
|
|
|
std::error_code Err = std::error_code();
|
2015-05-16 10:28:53 +08:00
|
|
|
if (Module::Header UmbrellaHeader = Module->getUmbrellaHeader())
|
2016-03-02 14:09:18 +08:00
|
|
|
addHeaderInclude(UmbrellaHeader.NameAsWritten, HeaderContents,
|
|
|
|
CI.getLangOpts(), Module->IsExternC);
|
|
|
|
Err = collectModuleHeaderIncludes(
|
2014-03-11 10:02:47 +08:00
|
|
|
CI.getLangOpts(), FileMgr,
|
|
|
|
CI.getPreprocessor().getHeaderSearchInfo().getModuleMap(), Module,
|
|
|
|
HeaderContents);
|
|
|
|
|
|
|
|
if (Err) {
|
|
|
|
CI.getDiagnostics().Report(diag::err_module_cannot_create_includes)
|
|
|
|
<< Module->getFullModuleName() << Err.message();
|
|
|
|
return false;
|
|
|
|
}
|
2011-11-17 01:04:00 +08:00
|
|
|
|
2014-12-02 08:08:08 +08:00
|
|
|
// Inform the preprocessor that includes from within the input buffer should
|
|
|
|
// be resolved relative to the build directory of the module map file.
|
|
|
|
CI.getPreprocessor().setMainFileDir(Module->Directory);
|
|
|
|
|
2014-08-28 04:03:29 +08:00
|
|
|
std::unique_ptr<llvm::MemoryBuffer> InputBuffer =
|
2012-11-16 02:57:27 +08:00
|
|
|
llvm::MemoryBuffer::getMemBufferCopy(HeaderContents,
|
|
|
|
Module::getModuleInputBufferName());
|
2013-12-06 00:25:25 +08:00
|
|
|
// Ownership of InputBuffer will be transferred to the SourceManager.
|
2014-08-28 04:03:29 +08:00
|
|
|
setCurrentInput(FrontendInputFile(InputBuffer.release(), getCurrentFileKind(),
|
2012-01-28 03:52:33 +08:00
|
|
|
Module->IsSystem));
|
2011-11-16 08:09:06 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
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()) {
|
|
|
|
HeaderSearch &HS = CI.getPreprocessor().getHeaderSearchInfo();
|
2014-04-15 02:00:01 +08:00
|
|
|
CI.getFrontendOpts().OutputFile =
|
|
|
|
HS.getModuleFileName(CI.getLangOpts().CurrentModule,
|
2016-08-19 01:42:15 +08:00
|
|
|
ModuleMapForUniquing->getName(),
|
|
|
|
/*UsePrebuiltPath=*/false);
|
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,
|
|
|
|
/*Extension=*/"", /*useTemporary=*/true,
|
|
|
|
/*CreateMissingDirectories=*/true);
|
|
|
|
}
|
2015-04-10 21:14:31 +08:00
|
|
|
|
2016-08-26 08:14:38 +08:00
|
|
|
bool GenerateModuleInterfaceAction::BeginSourceFileAction(CompilerInstance &CI,
|
|
|
|
StringRef Filename) {
|
|
|
|
if (!CI.getLangOpts().ModulesTS) {
|
|
|
|
CI.getDiagnostics().Report(diag::err_module_interface_requires_modules_ts);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
CI.getLangOpts().setCompilingModule(LangOptions::CMK_ModuleInterface);
|
|
|
|
|
|
|
|
return GenerateModuleAction::BeginSourceFileAction(CI, Filename);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::unique_ptr<raw_pwrite_stream>
|
|
|
|
GenerateModuleInterfaceAction::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) {
|
|
|
|
return llvm::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) {
|
|
|
|
return llvm::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) {
|
|
|
|
return llvm::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(
|
2015-07-17 09:19:54 +08:00
|
|
|
CI.getPreprocessor(), CI.getASTContext(), CI.getPCHContainerReader(),
|
2015-11-04 02:33:07 +08:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-03-28 00:47:18 +08:00
|
|
|
namespace {
|
|
|
|
/// \brief AST reader listener that dumps module information for a module
|
|
|
|
/// 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";
|
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";
|
|
|
|
}
|
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,
|
|
|
|
llvm::sys::fs::F_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 GeneratePTHAction::ExecuteAction() {
|
|
|
|
CompilerInstance &CI = getCompilerInstance();
|
2016-07-15 08:55:40 +08:00
|
|
|
std::unique_ptr<raw_pwrite_stream> OS =
|
|
|
|
CI.createDefaultOutputFile(true, getCurrentFile());
|
2015-04-13 16:43:40 +08:00
|
|
|
if (!OS)
|
|
|
|
return;
|
2009-12-03 17:13:30 +08:00
|
|
|
|
2016-07-15 08:55:40 +08:00
|
|
|
CacheTokens(CI.getPreprocessor(), OS.get());
|
2009-11-14 18:42:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
// all of their source code on a single line. However, that is still a
|
|
|
|
// 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();
|
|
|
|
const llvm::MemoryBuffer *Buffer = SM.getBuffer(SM.getMainFileID(),
|
|
|
|
&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
|
|
|
|
// file. This is mostly a sanity check in case the file has no
|
|
|
|
// newlines whatsoever.
|
|
|
|
if (end - cur > 256) end = cur + 256;
|
|
|
|
|
|
|
|
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 =
|
|
|
|
CI.createDefaultOutputFile(BinaryMode, getCurrentFile());
|
2009-12-03 17:13:30 +08:00
|
|
|
if (!OS) return;
|
|
|
|
|
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() {
|
|
|
|
switch (getCurrentFileKind()) {
|
|
|
|
case IK_C:
|
|
|
|
case IK_CXX:
|
|
|
|
case IK_ObjC:
|
|
|
|
case IK_ObjCXX:
|
|
|
|
case IK_OpenCL:
|
2010-12-01 11:15:20 +08:00
|
|
|
case IK_CUDA:
|
2010-07-21 04:18:03 +08:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IK_None:
|
|
|
|
case IK_Asm:
|
|
|
|
case IK_PreprocessedC:
|
2015-03-20 01:32:06 +08:00
|
|
|
case IK_PreprocessedCuda:
|
2010-07-21 04:18:03 +08:00
|
|
|
case IK_PreprocessedCXX:
|
|
|
|
case IK_PreprocessedObjC:
|
|
|
|
case IK_PreprocessedObjCXX:
|
|
|
|
case IK_AST:
|
|
|
|
case IK_LLVM_IR:
|
2016-06-10 07:34:20 +08:00
|
|
|
case IK_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
|
|
|
|
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 =
|
2014-10-27 06:44:13 +08:00
|
|
|
Lexer::ComputePreamble((*Buffer)->getBuffer(), CI.getLangOpts()).first;
|
|
|
|
llvm::outs().write((*Buffer)->getBufferStart(), Preamble);
|
2010-07-21 04:18:03 +08:00
|
|
|
}
|
|
|
|
}
|