[Tooling] Add operator== to CompileCommand
authorSimon Marchi <simon.marchi@ericsson.com>
Tue, 17 Jul 2018 14:13:05 +0000 (14:13 +0000)
committerSimon Marchi <simon.marchi@ericsson.com>
Tue, 17 Jul 2018 14:13:05 +0000 (14:13 +0000)
Summary:
It does the obvious thing of comparing all fields.  This will be needed
for a clangd patch I have in the pipeline.

Subscribers: dblaikie, ilya-biryukov, ioeric, cfe-commits

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

llvm-svn: 337284

clang/include/clang/Tooling/CompilationDatabase.h
clang/unittests/Tooling/CompilationDatabaseTest.cpp

index 185e496..37e515f 100644 (file)
@@ -59,6 +59,15 @@ struct CompileCommand {
 
   /// The output file associated with the command.
   std::string Output;
+
+  friend bool operator==(const CompileCommand &LHS, const CompileCommand &RHS) {
+    return LHS.Directory == RHS.Directory && LHS.Filename == RHS.Filename &&
+           LHS.CommandLine == RHS.CommandLine && LHS.Output == RHS.Output;
+  }
+
+  friend bool operator!=(const CompileCommand &LHS, const CompileCommand &RHS) {
+    return !(LHS == RHS);
+  }
 };
 
 /// Interface for compilation databases.
index 42497f7..ffc1d5b 100644 (file)
@@ -736,5 +736,33 @@ TEST_F(InterpolateTest, Case) {
   EXPECT_EQ(getCommand("foo/bar/baz/shout.C"), "clang -D FOO/BAR/BAZ/SHOUT.cc");
 }
 
+TEST(CompileCommandTest, EqualityOperator) {
+  CompileCommand CCRef("/foo/bar", "hello.c", {"a", "b"}, "hello.o");
+  CompileCommand CCTest = CCRef;
+
+  EXPECT_TRUE(CCRef == CCTest);
+  EXPECT_FALSE(CCRef != CCTest);
+
+  CCTest = CCRef;
+  CCTest.Directory = "/foo/baz";
+  EXPECT_FALSE(CCRef == CCTest);
+  EXPECT_TRUE(CCRef != CCTest);
+
+  CCTest = CCRef;
+  CCTest.Filename = "bonjour.c";
+  EXPECT_FALSE(CCRef == CCTest);
+  EXPECT_TRUE(CCRef != CCTest);
+
+  CCTest = CCRef;
+  CCTest.CommandLine.push_back("c");
+  EXPECT_FALSE(CCRef == CCTest);
+  EXPECT_TRUE(CCRef != CCTest);
+
+  CCTest = CCRef;
+  CCTest.Output = "bonjour.o";
+  EXPECT_FALSE(CCRef == CCTest);
+  EXPECT_TRUE(CCRef != CCTest);
+}
+
 } // end namespace tooling
 } // end namespace clang