From: Rafael Espindola Date: Sat, 10 Aug 2013 01:40:10 +0000 (+0000) Subject: Simplify now that llvm::sys::current_path checks $PWD. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=a2148240bd94ef659a9195b87e9cd0023536c01f;p=clang Simplify now that llvm::sys::current_path checks $PWD. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@188128 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Tooling/Tooling.h b/include/clang/Tooling/Tooling.h index 97bc2ab146..9b3572a0fe 100644 --- a/include/clang/Tooling/Tooling.h +++ b/include/clang/Tooling/Tooling.h @@ -305,10 +305,8 @@ inline FrontendActionFactory *newFrontendActionFactory( /// Otherwise, the returned path will contain the literal path-concatenation of /// the current directory and \c File. /// -/// The difference to llvm::sys::fs::make_absolute is that we prefer -/// ::getenv("PWD") if available. -/// FIXME: Make this functionality available from llvm::sys::fs and delete -/// this function. +/// The difference to llvm::sys::fs::make_absolute is the canonicalization this +/// does by removing "./" and computing native paths. /// /// \param File Either an absolute or relative path. std::string getAbsolutePath(StringRef File); diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp index 2953612f64..510575b395 100644 --- a/lib/Driver/Tools.cpp +++ b/lib/Driver/Tools.cpp @@ -1780,24 +1780,8 @@ static bool shouldUseLeafFramePointer(const ArgList &Args, return true; } -/// If the PWD environment variable is set, add a CC1 option to specify the -/// debug compilation directory. +/// Add a CC1 option to specify the debug compilation directory. static void addDebugCompDirArg(const ArgList &Args, ArgStringList &CmdArgs) { - const char *pwd = ::getenv("PWD"); - if (!pwd) - return; - - llvm::sys::fs::file_status PWDStatus, DotStatus; - if (llvm::sys::path::is_absolute(pwd) && - !llvm::sys::fs::status(pwd, PWDStatus) && - !llvm::sys::fs::status(".", DotStatus) && - PWDStatus.getUniqueID() == DotStatus.getUniqueID()) { - CmdArgs.push_back("-fdebug-compilation-dir"); - CmdArgs.push_back(Args.MakeArgString(pwd)); - return; - } - - // Fall back to using getcwd. SmallString<128> cwd; if (!llvm::sys::fs::current_path(cwd)) { CmdArgs.push_back("-fdebug-compilation-dir"); @@ -2494,12 +2478,10 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA, CmdArgs.push_back("-coverage-file"); SmallString<128> CoverageFilename(Output.getFilename()); if (llvm::sys::path::is_relative(CoverageFilename.str())) { - if (const char *pwd = ::getenv("PWD")) { - if (llvm::sys::path::is_absolute(pwd)) { - SmallString<128> Pwd(pwd); - llvm::sys::path::append(Pwd, CoverageFilename.str()); - CoverageFilename.swap(Pwd); - } + SmallString<128> Pwd; + if (!llvm::sys::fs::current_path(Pwd)) { + llvm::sys::path::append(Pwd, CoverageFilename.str()); + CoverageFilename.swap(Pwd); } } CmdArgs.push_back(Args.MakeArgString(CoverageFilename)); diff --git a/lib/Tooling/Tooling.cpp b/lib/Tooling/Tooling.cpp index cd4fbc6b5d..c5fec68384 100644 --- a/lib/Tooling/Tooling.cpp +++ b/lib/Tooling/Tooling.cpp @@ -124,23 +124,16 @@ bool runToolOnCodeWithArgs(clang::FrontendAction *ToolAction, const Twine &Code, } std::string getAbsolutePath(StringRef File) { - SmallString<1024> BaseDirectory; - if (const char *PWD = ::getenv("PWD")) - BaseDirectory = PWD; - else - llvm::sys::fs::current_path(BaseDirectory); - SmallString<1024> PathStorage; - if (llvm::sys::path::is_absolute(File)) { - llvm::sys::path::native(File, PathStorage); - return PathStorage.str(); - } StringRef RelativePath(File); // FIXME: Should '.\\' be accepted on Win32? if (RelativePath.startswith("./")) { RelativePath = RelativePath.substr(strlen("./")); } - SmallString<1024> AbsolutePath(BaseDirectory); - llvm::sys::path::append(AbsolutePath, RelativePath); + + SmallString<1024> AbsolutePath = RelativePath; + llvm::error_code EC = llvm::sys::fs::make_absolute(AbsolutePath); + assert(!EC); + SmallString<1024> PathStorage; llvm::sys::path::native(Twine(AbsolutePath), PathStorage); return PathStorage.str(); } diff --git a/test/Tooling/auto-detect-from-source-parent-of-cwd.cpp b/test/Tooling/auto-detect-from-source-parent-of-cwd.cpp index f8b0bc9e27..d7ec3f60ac 100644 --- a/test/Tooling/auto-detect-from-source-parent-of-cwd.cpp +++ b/test/Tooling/auto-detect-from-source-parent-of-cwd.cpp @@ -2,11 +2,15 @@ // RUN: mkdir -p %t/abc/def/ijk/qwe // RUN: echo "[{\"directory\":\".\",\"command\":\"clang++ -c %t/abc/def/ijk/qwe/test.cpp\",\"file\":\"%t/abc/def/ijk/qwe/test.cpp\"}]" | sed -e 's/\\/\\\\/g' > %t/compile_commands.json // RUN: cp "%s" "%t/abc/def/ijk/qwe/test.cpp" +// RUN: ln -sf %t/abc/def %t/abc/def2 +// RUN: cd %t/abc/def2 // RUN: not env PWD="%t/abc/def" clang-check "ijk/qwe/test.cpp" 2>&1 | FileCheck %s // CHECK: C++ requires +// CHECK: /abc/def/ijk/qwe/test.cpp invalid; // REQUIRES: shell // PR15590 // XFAIL: win64 +// XFAIL: mingw diff --git a/test/Tooling/clang-check-pwd.cpp b/test/Tooling/clang-check-pwd.cpp index 1c5a6ab70e..d85b9b1ae4 100644 --- a/test/Tooling/clang-check-pwd.cpp +++ b/test/Tooling/clang-check-pwd.cpp @@ -2,12 +2,16 @@ // RUN: mkdir %t // RUN: echo "[{\"directory\":\".\",\"command\":\"clang++ -c %t/test.cpp\",\"file\":\"%t/test.cpp\"}]" | sed -e 's/\\/\\\\/g' > %t/compile_commands.json // RUN: cp "%s" "%t/test.cpp" -// RUN: not env PWD="%t" clang-check -p "%t" "test.cpp" 2>&1|FileCheck %s +// RUN: ln -sf %t %t.foobar +// RUN: cd %t +// RUN: not env PWD="%t.foobar" clang-check -p "%t" "test.cpp" 2>&1|FileCheck %s // FIXME: Make the above easier. // CHECK: C++ requires +// CHECK: .foobar/test.cpp invalid; // REQUIRES: shell // PR15590 // XFAIL: win64 +// XFAIL: mingw