From: Simon Marchi Date: Tue, 17 Jul 2018 14:13:05 +0000 (+0000) Subject: [Tooling] Add operator== to CompileCommand X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=5e12144b17b760c132a1afad02c0949c77354b7a;p=clang [Tooling] Add operator== to CompileCommand 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 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@337284 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Tooling/CompilationDatabase.h b/include/clang/Tooling/CompilationDatabase.h index 185e496879..37e515fdc0 100644 --- a/include/clang/Tooling/CompilationDatabase.h +++ b/include/clang/Tooling/CompilationDatabase.h @@ -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. diff --git a/unittests/Tooling/CompilationDatabaseTest.cpp b/unittests/Tooling/CompilationDatabaseTest.cpp index 42497f7069..ffc1d5b3a4 100644 --- a/unittests/Tooling/CompilationDatabaseTest.cpp +++ b/unittests/Tooling/CompilationDatabaseTest.cpp @@ -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