Skip to content

Revert "[clang][cas] Cherry-pick recent cas include-tree improvements" #6617

New issue

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

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

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ class DependencyScanningTool {

/// Collect dependency tree.
llvm::Expected<llvm::cas::ObjectProxy>
getDependencyTree(const std::vector<std::string> &CommandLine, StringRef CWD);
getDependencyTree(const std::vector<std::string> &CommandLine, StringRef CWD,
DepscanPrefixMapping PrefixMapping = {});

/// If \p DiagGenerationAsCompilation is true it will generate error
/// diagnostics same way as the normal compilation, with "N errors generated"
Expand All @@ -116,12 +117,14 @@ class DependencyScanningTool {
getDependencyTreeFromCompilerInvocation(
std::shared_ptr<CompilerInvocation> Invocation, StringRef CWD,
DiagnosticConsumer &DiagsConsumer, raw_ostream *VerboseOS,
bool DiagGenerationAsCompilation);
bool DiagGenerationAsCompilation,
DepscanPrefixMapping PrefixMapping = {});

Expected<cas::IncludeTreeRoot>
getIncludeTree(cas::ObjectStore &DB,
const std::vector<std::string> &CommandLine, StringRef CWD,
LookupModuleOutputCallback LookupModuleOutput);
LookupModuleOutputCallback LookupModuleOutput,
const DepscanPrefixMapping &PrefixMapping);

/// If \p DiagGenerationAsCompilation is true it will generate error
/// diagnostics same way as the normal compilation, with "N errors generated"
Expand All @@ -130,6 +133,7 @@ class DependencyScanningTool {
Expected<cas::IncludeTreeRoot> getIncludeTreeFromCompilerInvocation(
cas::ObjectStore &DB, std::shared_ptr<CompilerInvocation> Invocation,
StringRef CWD, LookupModuleOutputCallback LookupModuleOutput,
const DepscanPrefixMapping &PrefixMapping,
DiagnosticConsumer &DiagsConsumer, raw_ostream *VerboseOS,
bool DiagGenerationAsCompilation);

Expand All @@ -151,7 +155,8 @@ class DependencyScanningTool {
getTranslationUnitDependencies(const std::vector<std::string> &CommandLine,
StringRef CWD,
const llvm::StringSet<> &AlreadySeen,
LookupModuleOutputCallback LookupModuleOutput);
LookupModuleOutputCallback LookupModuleOutput,
DepscanPrefixMapping PrefixMapping);

/// Given a compilation context specified via the Clang driver command-line,
/// gather modular dependencies of module with the given name, and return the
Expand All @@ -160,7 +165,8 @@ class DependencyScanningTool {
getModuleDependencies(StringRef ModuleName,
const std::vector<std::string> &CommandLine,
StringRef CWD, const llvm::StringSet<> &AlreadySeen,
LookupModuleOutputCallback LookupModuleOutput);
LookupModuleOutputCallback LookupModuleOutput,
DepscanPrefixMapping PrefixMapping);

ScanningOutputFormat getScanningFormat() const {
return Worker.getScanningFormat();
Expand All @@ -181,11 +187,13 @@ class DependencyScanningTool {

static std::unique_ptr<DependencyActionController>
createActionController(DependencyScanningWorker &Worker,
LookupModuleOutputCallback LookupModuleOutput);
LookupModuleOutputCallback LookupModuleOutput,
DepscanPrefixMapping PrefixMapping);

private:
std::unique_ptr<DependencyActionController>
createActionController(LookupModuleOutputCallback LookupModuleOutput);
createActionController(LookupModuleOutputCallback LookupModuleOutput,
DepscanPrefixMapping PrefixMapping);

private:
DependencyScanningWorker Worker;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ using CachingOnDiskFileSystemPtr =
llvm::IntrusiveRefCntPtr<llvm::cas::CachingOnDiskFileSystem>;

class DependencyScanningWorkerFilesystem;
struct DepscanPrefixMapping;

/// A command-line tool invocation that is part of building a TU.
///
Expand Down Expand Up @@ -98,6 +99,12 @@ class DependencyActionController {
const ModuleDeps &MD) {
return llvm::Error::success();
}

/// FIXME: This is temporary until we eliminate the split between consumers in
/// \p DependencyScanningTool and collectors in \p DependencyScanningWorker
/// and have them both in the same file. see FIXME in \p
/// DependencyScanningAction::runInvocation.
virtual const DepscanPrefixMapping *getPrefixMapping() { return nullptr; }
};

/// An individual dependency scanning worker that is able to run on its own
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ void configureInvocationForCaching(CompilerInvocation &CI, CASOptions CASOpts,
bool ProduceIncludeTree);

struct DepscanPrefixMapping {
/// Add path mappings to the \p Mapper.
static void configurePrefixMapper(const CompilerInvocation &Invocation,
llvm::PrefixMapper &Mapper);
Optional<std::string> NewSDKPath;
Optional<std::string> NewToolchainPath;
SmallVector<std::string> PrefixMap;

/// Add path mappings to the \p Mapper.
static void configurePrefixMapper(ArrayRef<std::string> PathPrefixMappings,
llvm::PrefixMapper &Mapper);
/// Add path mappings from the current path in \p Invocation to the new path
/// from \c DepscanPrefixMapping to the \p Mapper.
llvm::Error configurePrefixMapper(const CompilerInvocation &Invocation,
llvm::PrefixMapper &Mapper) const;

/// Apply the mappings from \p Mapper to \p Invocation.
static void remapInvocationPaths(CompilerInvocation &Invocation,
Expand All @@ -60,6 +61,7 @@ Expected<llvm::cas::CASID> scanAndUpdateCC1InlineWithTool(
tooling::dependencies::DependencyScanningTool &Tool,
DiagnosticConsumer &DiagsConsumer, raw_ostream *VerboseOS,
CompilerInvocation &Invocation, StringRef WorkingDirectory,
const tooling::dependencies::DepscanPrefixMapping &PrefixMapping,
llvm::cas::ObjectStore &DB);

} // end namespace clang
Expand Down
84 changes: 16 additions & 68 deletions clang/lib/Driver/ToolChains/Clang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1190,8 +1190,7 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
const Driver &D, const ArgList &Args,
ArgStringList &CmdArgs,
const InputInfo &Output,
const InputInfoList &Inputs,
std::optional<StringRef> &Sysroot) const {
const InputInfoList &Inputs) const {
const bool IsIAMCU = getToolChain().getTriple().isOSIAMCU();

CheckPreprocessingOptions(D, Args);
Expand Down Expand Up @@ -1429,15 +1428,10 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA,
// -isysroot to the CC1 invocation.
StringRef sysroot = C.getSysRoot();
if (sysroot != "") {
if (Arg *A = Args.getLastArg(options::OPT_isysroot)) {
Sysroot = A->getValue();
} else {
if (!Args.hasArg(options::OPT_isysroot)) {
CmdArgs.push_back("-isysroot");
CmdArgs.push_back(C.getArgs().MakeArgString(sysroot));
Sysroot = sysroot;
}
} else if (Arg *A = Args.getLastArg(options::OPT_isysroot)) {
Sysroot = A->getValue();
}

// Parse additional include paths from environment variables.
Expand Down Expand Up @@ -4530,52 +4524,6 @@ static void ProcessVSRuntimeLibrary(const ArgList &Args,
}
}

void Clang::AddPrefixMappingOptions(const ArgList &Args, ArgStringList &CmdArgs,
const Driver &D,
std::optional<StringRef> Sysroot) const {
auto IsPathApplicableAsPrefix = [](std::optional<StringRef> Path) {
return Path && llvm::sys::path::is_absolute(*Path) &&
*Path != llvm::sys::path::root_path(*Path);
};

if (Arg *A = Args.getLastArg(options::OPT_fdepscan_prefix_map_sdk_EQ)) {
if (IsPathApplicableAsPrefix(Sysroot)) {
CmdArgs.push_back(Args.MakeArgString(Twine("-fdepscan-prefix-map=") +
*Sysroot + "=" + A->getValue()));
} else {
// FIXME: warning if we cannot infer sdk
}
}

if (Arg *A = Args.getLastArg(options::OPT_fdepscan_prefix_map_toolchain_EQ)) {
StringRef ResourceDir = D.ResourceDir;
StringRef Guess = llvm::sys::path::parent_path(ResourceDir);
for (StringRef Dir : {"clang", "lib", "usr"}) {
if (llvm::sys::path::filename(Guess) != Dir)
break;
Guess = llvm::sys::path::parent_path(Guess);
}
if (IsPathApplicableAsPrefix(Guess)) {
CmdArgs.push_back(Args.MakeArgString(Twine("-fdepscan-prefix-map=") +
Guess + "=" + A->getValue()));
} else {
// FIXME: warning if we cannot infer toolchain
}
}

for (const Arg *A : Args.filtered(options::OPT_fdepscan_prefix_map_EQ)) {
A->claim();
StringRef Map = A->getValue();
StringRef Prefix = Map.split('=').first;
if (Prefix.size() == Map.size() || !IsPathApplicableAsPrefix(Prefix)) {
D.Diag(diag::err_drv_invalid_argument_to_option)
<< A->getValue() << A->getOption().getName();
} else {
A->render(Args, CmdArgs);
}
}
}

void Clang::ConstructJob(Compilation &C, const JobAction &Job,
const InputInfo &Output, const InputInfoList &Inputs,
const ArgList &Args, const char *LinkingOutput) const {
Expand Down Expand Up @@ -4692,15 +4640,19 @@ void Clang::ConstructJob(Compilation &C, const JobAction &Job,
D.Diag(diag::err_drv_clang_unsupported) << "C++ for IAMCU";

{
const OptSpecifier DepScanOpts[] = {options::OPT_fdepscan_EQ,
options::OPT_fdepscan_include_tree,
options::OPT_fdepscan_share_EQ,
options::OPT_fdepscan_share_identifier,
options::OPT_fdepscan_share_parent,
options::OPT_fdepscan_share_parent_EQ,
options::OPT_fno_depscan_share,
options::OPT_fdepscan_share_stop_EQ,
options::OPT_fdepscan_daemon_EQ};
const OptSpecifier DepScanOpts[] = {
options::OPT_fdepscan_EQ,
options::OPT_fdepscan_include_tree,
options::OPT_fdepscan_share_EQ,
options::OPT_fdepscan_share_identifier,
options::OPT_fdepscan_share_parent,
options::OPT_fdepscan_share_parent_EQ,
options::OPT_fno_depscan_share,
options::OPT_fdepscan_share_stop_EQ,
options::OPT_fdepscan_daemon_EQ,
options::OPT_fdepscan_prefix_map_EQ,
options::OPT_fdepscan_prefix_map_sdk_EQ,
options::OPT_fdepscan_prefix_map_toolchain_EQ};

// Handle depscan.
if (Job.getKind() == Action::DepscanJobClass) {
Expand Down Expand Up @@ -5938,12 +5890,8 @@ void Clang::ConstructJob(Compilation &C, const JobAction &Job,
// preprocessor.
//
// FIXME: Support -fpreprocessed
std::optional<StringRef> Sysroot;
if (types::getPreprocessedType(InputType) != types::TY_INVALID)
AddPreprocessingOptions(C, JA, D, Args, CmdArgs, Output, Inputs, Sysroot);

// Handle -fdepscan-prefix-map-* options
AddPrefixMappingOptions(Args, CmdArgs, D, Sysroot);
AddPreprocessingOptions(C, JA, D, Args, CmdArgs, Output, Inputs);

// Don't warn about "clang -c -DPIC -fPIC test.i" because libtool.m4 assumes
// that "The compiler can only warn and ignore the option if not recognized".
Expand Down
8 changes: 1 addition & 7 deletions clang/lib/Driver/ToolChains/Clang.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ class LLVM_LIBRARY_VISIBILITY Clang : public Tool {
const Driver &D, const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &CmdArgs,
const InputInfo &Output,
const InputInfoList &Inputs,
std::optional<StringRef> &Sysroot) const;
const InputInfoList &Inputs) const;

void RenderTargetOptions(const llvm::Triple &EffectiveTriple,
const llvm::opt::ArgList &Args, bool KernelOrKext,
Expand Down Expand Up @@ -105,11 +104,6 @@ class LLVM_LIBRARY_VISIBILITY Clang : public Tool {
StringRef Dir, Compilation &C, StringRef Target, const InputInfo &Output,
const InputInfo &Input, const llvm::opt::ArgList &Args) const;

void AddPrefixMappingOptions(const llvm::opt::ArgList &Args,
llvm::opt::ArgStringList &CmdArgs,
const Driver &D,
std::optional<StringRef> Sysroot) const;

public:
Clang(const ToolChain &TC, bool HasIntegratedBackend = true);
~Clang() override;
Expand Down
6 changes: 1 addition & 5 deletions clang/lib/Frontend/CompilerInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2081,12 +2081,8 @@ CompilerInstance::loadModule(SourceLocation ImportLoc,
PrivateModule, PP->getIdentifierInfo(Module->Name)->getTokenID());
PrivPath.push_back(std::make_pair(&II, Path[0].second));

std::string FileName;
// If there is a modulemap module or prebuilt module, load it.
if (PP->getHeaderSearchInfo().lookupModule(PrivateModule, ImportLoc, true,
!IsInclusionDirective) ||
selectModuleSource(nullptr, PrivateModule, FileName, BuiltModules,
PP->getHeaderSearchInfo()) != MS_ModuleNotFound)
!IsInclusionDirective))
Sub = loadModule(ImportLoc, PrivPath, Visibility, IsInclusionDirective);
if (Sub) {
MapPrivateSubModToTopLevel = true;
Expand Down
20 changes: 13 additions & 7 deletions clang/lib/Tooling/DependencyScanning/CASFSActionController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ namespace {
class CASFSActionController : public CallbackActionController {
public:
CASFSActionController(LookupModuleOutputCallback LookupModuleOutput,
llvm::cas::CachingOnDiskFileSystem &CacheFS);
llvm::cas::CachingOnDiskFileSystem &CacheFS,
DepscanPrefixMapping PrefixMapping);

llvm::Error initialize(CompilerInstance &ScanInstance,
CompilerInvocation &NewInvocation) override;
Expand All @@ -37,22 +38,25 @@ class CASFSActionController : public CallbackActionController {

private:
llvm::cas::CachingOnDiskFileSystem &CacheFS;
DepscanPrefixMapping PrefixMapping;
std::optional<llvm::TreePathPrefixMapper> Mapper;
CASOptions CASOpts;
};
} // anonymous namespace

CASFSActionController::CASFSActionController(
LookupModuleOutputCallback LookupModuleOutput,
llvm::cas::CachingOnDiskFileSystem &CacheFS)
: CallbackActionController(std::move(LookupModuleOutput)),
CacheFS(CacheFS) {}
llvm::cas::CachingOnDiskFileSystem &CacheFS,
DepscanPrefixMapping PrefixMapping)
: CallbackActionController(std::move(LookupModuleOutput)), CacheFS(CacheFS),
PrefixMapping(std::move(PrefixMapping)) {}

Error CASFSActionController::initialize(CompilerInstance &ScanInstance,
CompilerInvocation &NewInvocation) {
// Setup prefix mapping.
Mapper.emplace(&CacheFS);
DepscanPrefixMapping::configurePrefixMapper(NewInvocation, *Mapper);
if (Error E = PrefixMapping.configurePrefixMapper(NewInvocation, *Mapper))
return E;

const PreprocessorOptions &PPOpts = ScanInstance.getPreprocessorOpts();
if (!PPOpts.Includes.empty() || !PPOpts.ImplicitPCHInclude.empty())
Expand Down Expand Up @@ -193,6 +197,8 @@ Error CASFSActionController::finalizeModuleInvocation(CompilerInvocation &CI,
std::unique_ptr<DependencyActionController>
dependencies::createCASFSActionController(
LookupModuleOutputCallback LookupModuleOutput,
llvm::cas::CachingOnDiskFileSystem &CacheFS) {
return std::make_unique<CASFSActionController>(LookupModuleOutput, CacheFS);
llvm::cas::CachingOnDiskFileSystem &CacheFS,
DepscanPrefixMapping PrefixMapping) {
return std::make_unique<CASFSActionController>(LookupModuleOutput, CacheFS,
std::move(PrefixMapping));
}
6 changes: 4 additions & 2 deletions clang/lib/Tooling/DependencyScanning/CachingActions.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ namespace clang::tooling::dependencies {

std::unique_ptr<DependencyActionController>
createIncludeTreeActionController(LookupModuleOutputCallback LookupModuleOutput,
cas::ObjectStore &DB);
cas::ObjectStore &DB,
DepscanPrefixMapping PrefixMapping);

std::unique_ptr<DependencyActionController>
createCASFSActionController(LookupModuleOutputCallback LookupModuleOutput,
llvm::cas::CachingOnDiskFileSystem &CacheFS);
llvm::cas::CachingOnDiskFileSystem &CacheFS,
DepscanPrefixMapping PrefixMapping);

/// The PCH recorded file paths with canonical paths, create a VFS that
/// allows remapping back to the non-canonical source paths so that they are
Expand Down
Loading