[clang] 48aa781 - [Tooling] Drop leading/trailing whitespace from compile_flags.txt lines

Sam McCall via cfe-commits cfe-commits at lists.llvm.org
Tue May 19 02:20:18 PDT 2020


Author: Sam McCall
Date: 2020-05-19T11:20:02+02:00
New Revision: 48aa781ae44a611967084ca031fdbb3a0712c40c

URL: https://github.com/llvm/llvm-project/commit/48aa781ae44a611967084ca031fdbb3a0712c40c
DIFF: https://github.com/llvm/llvm-project/commit/48aa781ae44a611967084ca031fdbb3a0712c40c.diff

LOG: [Tooling] Drop leading/trailing whitespace from compile_flags.txt lines

Summary:
These files tend to be hand-authored, and people get very confused.
I can't think of any reason that such whitespace would be intended.

Reviewers: kadircet

Subscribers: cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D80160

Added: 
    

Modified: 
    clang/lib/Tooling/CompilationDatabase.cpp
    clang/test/Tooling/fixed-database.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Tooling/CompilationDatabase.cpp b/clang/lib/Tooling/CompilationDatabase.cpp
index 3590f19675c7..254314b1967f 100644
--- a/clang/lib/Tooling/CompilationDatabase.cpp
+++ b/clang/lib/Tooling/CompilationDatabase.cpp
@@ -368,8 +368,14 @@ FixedCompilationDatabase::loadFromFile(StringRef Path, std::string &ErrorMsg) {
     ErrorMsg = "Error while opening fixed database: " + Result.message();
     return nullptr;
   }
-  std::vector<std::string> Args{llvm::line_iterator(**File),
-                                llvm::line_iterator()};
+  std::vector<std::string> Args;
+  for (llvm::StringRef Line :
+       llvm::make_range(llvm::line_iterator(**File), llvm::line_iterator())) {
+    // Stray whitespace is almost certainly unintended.
+    Line = Line.trim();
+    if (!Line.empty())
+      Args.push_back(Line.str());
+  }
   return std::make_unique<FixedCompilationDatabase>(
       llvm::sys::path::parent_path(Path), std::move(Args));
 }

diff  --git a/clang/test/Tooling/fixed-database.cpp b/clang/test/Tooling/fixed-database.cpp
index 73d0779258b2..038ef5c470e2 100644
--- a/clang/test/Tooling/fixed-database.cpp
+++ b/clang/test/Tooling/fixed-database.cpp
@@ -5,7 +5,7 @@
 // RUN: cp "%S/Inputs/fixed-header.h" "%t/Include/"
 // -I flag is relative to %t (where compile_flags is), not Src/.
 // RUN: echo '-IInclude/' >> %t/compile_flags.txt
-// RUN: echo "-Dklazz=class" >> %t/compile_flags.txt
+// RUN: echo "  -Dklazz=class   " >> %t/compile_flags.txt
 // RUN: echo '-std=c++11' >> %t/compile_flags.txt
 // RUN: clang-check "%t/Src/test.cpp" 2>&1
 // RUN: echo > %t/compile_flags.txt


        


More information about the cfe-commits mailing list