diff options
author | Peter Wu <peter@lekensteyn.nl> | 2017-10-19 23:53:27 +0000 |
---|---|---|
committer | Peter Wu <peter@lekensteyn.nl> | 2017-10-19 23:53:27 +0000 |
commit | 90161dad75f4a47cd4af6891f1664bb04a1c52ec (patch) | |
tree | 7a23e7ed22ce8ec467bd35a762a6281ad9921e25 /clang/lib | |
parent | b2f894ff2275a04324b68dc7f327b9fa88dc50bd (diff) | |
download | bcm5719-llvm-90161dad75f4a47cd4af6891f1664bb04a1c52ec.tar.gz bcm5719-llvm-90161dad75f4a47cd4af6891f1664bb04a1c52ec.zip |
Try to shorten system header paths when using -MD depfiles
GCC tries to shorten system headers in depfiles using its real path
(resolving components like ".." and following symlinks). Mimic this
feature to ensure that the Ninja build tool detects the correct
dependencies when a symlink changes directory levels, see
https://github.com/ninja-build/ninja/issues/1330
An option to disable this feature is added in case "these changed header
paths may conflict with some compilation environments", see
https://gcc.gnu.org/ml/gcc-patches/2012-09/msg00287.html
Note that the original feature request for GCC
(https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52974) also included paths
preprocessed output (-E) and diagnostics. That is not implemented now
since I am not sure if it breaks something else.
Differential Revision: https://reviews.llvm.org/D37954
llvm-svn: 316193
Diffstat (limited to 'clang/lib')
-rw-r--r-- | clang/lib/Driver/Job.cpp | 4 | ||||
-rw-r--r-- | clang/lib/Driver/ToolChains/Clang.cpp | 7 | ||||
-rw-r--r-- | clang/lib/Frontend/CompilerInvocation.cpp | 1 | ||||
-rw-r--r-- | clang/lib/Frontend/DependencyFile.cpp | 11 | ||||
-rw-r--r-- | clang/lib/Tooling/ArgumentsAdjusters.cpp | 4 |
5 files changed, 24 insertions, 3 deletions
diff --git a/clang/lib/Driver/Job.cpp b/clang/lib/Driver/Job.cpp index 765c05752d8..241c72a2e94 100644 --- a/clang/lib/Driver/Job.cpp +++ b/clang/lib/Driver/Job.cpp @@ -73,8 +73,8 @@ static bool skipArgs(const char *Flag, bool HaveCrashVFS, int &SkipNum, // These flags are all of the form -Flag and have no second argument. ShouldSkip = llvm::StringSwitch<bool>(Flag) - .Cases("-M", "-MM", "-MG", "-MP", "-MD", true) - .Case("-MMD", true) + .Cases("-M", "-MM", "-MG", "-MP", "-MD", "-MMD", true) + .Cases("-fno-canonical-system-headers", "-fcanonical-system-headers", true) .Default(false); // Match found. diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp index 12713f8be2f..1304042c83a 100644 --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -964,6 +964,13 @@ void Clang::AddPreprocessingOptions(Compilation &C, const JobAction &JA, Args.AddLastArg(CmdArgs, options::OPT_C); Args.AddLastArg(CmdArgs, options::OPT_CC); + if (Arg *A = Args.getLastArg(options::OPT_fno_canonical_system_headers, + options::OPT_fcanonical_system_headers)) { + if (A->getOption().matches(options::OPT_fno_canonical_system_headers)) { + CmdArgs.push_back("-fno-canonical-system-headers"); + } + } + // Handle dependency file generation. if ((A = Args.getLastArg(options::OPT_M, options::OPT_MM)) || (A = Args.getLastArg(options::OPT_MD)) || diff --git a/clang/lib/Frontend/CompilerInvocation.cpp b/clang/lib/Frontend/CompilerInvocation.cpp index 19e26c18bfd..42fd2a13313 100644 --- a/clang/lib/Frontend/CompilerInvocation.cpp +++ b/clang/lib/Frontend/CompilerInvocation.cpp @@ -1002,6 +1002,7 @@ static void ParseDependencyOutputArgs(DependencyOutputOptions &Opts, Opts.Targets = Args.getAllArgValues(OPT_MT); Opts.IncludeSystemHeaders = Args.hasArg(OPT_sys_header_deps); Opts.IncludeModuleFiles = Args.hasArg(OPT_module_file_deps); + Opts.CanonicalSystemHeaders = !Args.hasArg(OPT_fno_canonical_system_headers); Opts.UsePhonyTargets = Args.hasArg(OPT_MP); Opts.ShowHeaderIncludes = Args.hasArg(OPT_H); Opts.HeaderIncludeOutputFile = Args.getLastArgValue(OPT_header_include_file); diff --git a/clang/lib/Frontend/DependencyFile.cpp b/clang/lib/Frontend/DependencyFile.cpp index 561eb9c4a31..b6e4cfa3385 100644 --- a/clang/lib/Frontend/DependencyFile.cpp +++ b/clang/lib/Frontend/DependencyFile.cpp @@ -161,6 +161,7 @@ class DFGImpl : public PPCallbacks { bool AddMissingHeaderDeps; bool SeenMissingHeader; bool IncludeModuleFiles; + bool CanonicalSystemHeaders; DependencyOutputFormat OutputFormat; private: @@ -176,6 +177,7 @@ public: AddMissingHeaderDeps(Opts.AddMissingHeaderDeps), SeenMissingHeader(false), IncludeModuleFiles(Opts.IncludeModuleFiles), + CanonicalSystemHeaders(Opts.CanonicalSystemHeaders), OutputFormat(Opts.OutputFormat) { for (const auto &ExtraDep : Opts.ExtraDeps) { AddFilename(ExtraDep); @@ -288,6 +290,15 @@ void DFGImpl::FileChanged(SourceLocation Loc, if (!FileMatchesDepCriteria(Filename.data(), FileType)) return; + // Try to shorten system header paths like GCC does (unless + // -fno-canonical-system-headers is given). + if (CanonicalSystemHeaders && isSystem(FileType)) { + StringRef RealPath = FE->tryGetRealPathName(); + if (!RealPath.empty() && RealPath.size() < Filename.size()) { + Filename = RealPath; + } + } + AddFilename(llvm::sys::path::remove_leading_dotslash(Filename)); } diff --git a/clang/lib/Tooling/ArgumentsAdjusters.cpp b/clang/lib/Tooling/ArgumentsAdjusters.cpp index ac9fd3c5cad..8fcaa8afe43 100644 --- a/clang/lib/Tooling/ArgumentsAdjusters.cpp +++ b/clang/lib/Tooling/ArgumentsAdjusters.cpp @@ -58,7 +58,9 @@ ArgumentsAdjuster getClangStripDependencyFileAdjuster() { StringRef Arg = Args[i]; // All dependency-file options begin with -M. These include -MM, // -MF, -MG, -MP, -MT, -MQ, -MD, and -MMD. - if (!Arg.startswith("-M")) + // The exception is -f[no-]canonical-system-headers. + if (!Arg.startswith("-M") && Arg != "-fno-canonical-system-headers" && + Arg != "-fcanonical-system-headers") AdjustedArgs.push_back(Args[i]); if ((Arg == "-MF") || (Arg == "-MT") || (Arg == "-MQ") || |