From 5df2a4e8dfa1ad9c2f91939a07fe3dfc809ed944 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Thu, 17 Feb 2011 02:14:49 +0000 Subject: [PATCH] fix clang -MM output to escape spaces in filenames. This seems to be the only character that GCC escapes. PR9224. llvm-svn: 125707 --- clang/lib/Frontend/DependencyFile.cpp | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/clang/lib/Frontend/DependencyFile.cpp b/clang/lib/Frontend/DependencyFile.cpp index cdff8077ee47..bc5a55df0860 100644 --- a/clang/lib/Frontend/DependencyFile.cpp +++ b/clang/lib/Frontend/DependencyFile.cpp @@ -13,7 +13,6 @@ #include "clang/Frontend/Utils.h" #include "clang/Basic/FileManager.h" -#include "clang/Basic/SourceLocation.h" #include "clang/Basic/SourceManager.h" #include "clang/Frontend/DependencyOutputOptions.h" #include "clang/Frontend/FrontendDiagnostic.h" @@ -22,7 +21,6 @@ #include "clang/Lex/Preprocessor.h" #include "llvm/ADT/StringSet.h" #include "llvm/Support/raw_ostream.h" -#include using namespace clang; @@ -117,6 +115,16 @@ void DependencyFileCallback::FileChanged(SourceLocation Loc, Files.push_back(Filename); } +/// PrintFilename - GCC escapes spaces, but apparently not ' or " or other +/// scary characters. +static void PrintFilename(llvm::raw_ostream &OS, llvm::StringRef Filename) { + for (unsigned i = 0, e = Filename.size(); i != e; ++i) { + if (Filename[i] == ' ') + OS << '\\'; + OS << Filename[i]; + } +} + void DependencyFileCallback::OutputDependencyFile() { // Write out the dependency targets, trying to avoid overly long // lines when possible. We try our best to emit exactly the same @@ -130,14 +138,15 @@ void DependencyFileCallback::OutputDependencyFile() { unsigned N = I->length(); if (Columns == 0) { Columns += N; - *OS << *I; } else if (Columns + N + 2 > MaxColumns) { Columns = N + 2; - *OS << " \\\n " << *I; + *OS << " \\\n "; } else { Columns += N + 1; - *OS << ' ' << *I; + *OS << ' '; } + // Targets already quoted as needed. + *OS << *I; } *OS << ':'; @@ -155,7 +164,8 @@ void DependencyFileCallback::OutputDependencyFile() { *OS << " \\\n "; Columns = 2; } - *OS << ' ' << *I; + *OS << ' '; + PrintFilename(*OS, *I); Columns += N + 1; } *OS << '\n'; @@ -166,7 +176,8 @@ void DependencyFileCallback::OutputDependencyFile() { for (std::vector::iterator I = Files.begin() + 1, E = Files.end(); I != E; ++I) { *OS << '\n'; - *OS << *I << ":\n"; + PrintFilename(*OS, *I); + *OS << ":\n"; } } }