[Remarks] Add comparison operators to the Remark object
authorFrancis Visoiu Mistrih <francisvm@yahoo.com>
Thu, 5 Sep 2019 22:35:37 +0000 (22:35 +0000)
committerFrancis Visoiu Mistrih <francisvm@yahoo.com>
Thu, 5 Sep 2019 22:35:37 +0000 (22:35 +0000)
and related structs.

This also adds tests for the remarks::Remark object in general.

llvm-svn: 371134

llvm/include/llvm/Remarks/Remark.h
llvm/unittests/Remarks/CMakeLists.txt
llvm/unittests/Remarks/RemarksAPITest.cpp [new file with mode: 0644]

index 9cfc8a8..1243311 100644 (file)
@@ -109,6 +109,36 @@ private:
 // Create wrappers for C Binding types (see CBindingWrapping.h).
 DEFINE_SIMPLE_CONVERSION_FUNCTIONS(Remark, LLVMRemarkEntryRef)
 
+/// Comparison operators for Remark objects and dependent objects.
+inline bool operator==(const RemarkLocation &LHS, const RemarkLocation &RHS) {
+  return LHS.SourceFilePath == RHS.SourceFilePath &&
+         LHS.SourceLine == RHS.SourceLine &&
+         LHS.SourceColumn == RHS.SourceColumn;
+}
+
+inline bool operator!=(const RemarkLocation &LHS, const RemarkLocation &RHS) {
+  return !(LHS == RHS);
+}
+
+inline bool operator==(const Argument &LHS, const Argument &RHS) {
+  return LHS.Key == RHS.Key && LHS.Val == RHS.Val && LHS.Loc == RHS.Loc;
+}
+
+inline bool operator!=(const Argument &LHS, const Argument &RHS) {
+  return !(LHS == RHS);
+}
+
+inline bool operator==(const Remark &LHS, const Remark &RHS) {
+  return LHS.RemarkType == RHS.RemarkType && LHS.PassName == RHS.PassName &&
+         LHS.RemarkName == RHS.RemarkName &&
+         LHS.FunctionName == RHS.FunctionName && LHS.Loc == RHS.Loc &&
+         LHS.Hotness == RHS.Hotness && LHS.Args == RHS.Args;
+}
+
+inline bool operator!=(const Remark &LHS, const Remark &RHS) {
+  return !(LHS == RHS);
+}
+
 } // end namespace remarks
 } // end namespace llvm
 
index 5731650..0bc67fc 100644 (file)
@@ -7,6 +7,7 @@ set(LLVM_LINK_COMPONENTS
 add_llvm_unittest(RemarksTests
   BitstreamRemarksFormatTest.cpp
   BitstreamRemarksSerializerTest.cpp
+  RemarksAPITest.cpp
   RemarksStrTabParsingTest.cpp
   YAMLRemarksParsingTest.cpp
   YAMLRemarksSerializerTest.cpp
diff --git a/llvm/unittests/Remarks/RemarksAPITest.cpp b/llvm/unittests/Remarks/RemarksAPITest.cpp
new file mode 100644 (file)
index 0000000..23a45e9
--- /dev/null
@@ -0,0 +1,77 @@
+//===- unittest/Support/RemarksAPITest.cpp - C++ API tests ----------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Remarks/Remark.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+
+TEST(RemarksAPI, Comparison) {
+  remarks::Remark R;
+  R.RemarkType = remarks::Type::Missed;
+  R.PassName = "pass";
+  R.RemarkName = "name";
+  R.FunctionName = "func";
+  R.Loc = remarks::RemarkLocation{"path", 3, 4};
+  R.Hotness = 5;
+  R.Args.emplace_back();
+  R.Args.back().Key = "key";
+  R.Args.back().Val = "value";
+  R.Args.emplace_back();
+  R.Args.back().Key = "keydebug";
+  R.Args.back().Val = "valuedebug";
+  R.Args.back().Loc = remarks::RemarkLocation{"argpath", 6, 7};
+
+  // Check that == works.
+  EXPECT_EQ(R, R);
+
+  // Check that != works.
+  remarks::Remark R2 = R.clone();
+  R2.FunctionName = "func0";
+  EXPECT_NE(R, R2);
+
+  // Check that we iterate through all the arguments.
+  remarks::Remark R3 = R.clone();
+  R3.Args.back().Val = "not";
+  EXPECT_NE(R, R3);
+}
+
+TEST(RemarksAPI, Clone) {
+  remarks::Remark R;
+  R.RemarkType = remarks::Type::Missed;
+  R.PassName = "pass";
+  R.RemarkName = "name";
+  R.FunctionName = "func";
+  R.Loc = remarks::RemarkLocation{"path", 3, 4};
+  R.Hotness = 5;
+  R.Args.emplace_back();
+  R.Args.back().Key = "key";
+  R.Args.back().Val = "value";
+  R.Args.emplace_back();
+  R.Args.back().Key = "keydebug";
+  R.Args.back().Val = "valuedebug";
+  R.Args.back().Loc = remarks::RemarkLocation{"argpath", 6, 7};
+
+  // Check that clone works.
+  remarks::Remark R2 = R.clone();
+  EXPECT_EQ(R, R2);
+}
+
+TEST(RemarksAPI, ArgsAsMsg) {
+  remarks::Remark R;
+  R.RemarkType = remarks::Type::Missed;
+  R.Args.emplace_back();
+  R.Args.back().Key = "key";
+  R.Args.back().Val = "can not do this ";
+  R.Args.emplace_back();
+  R.Args.back().Key = "keydebug";
+  R.Args.back().Val = "because of that.";
+  R.Args.back().Loc = remarks::RemarkLocation{"argpath", 6, 7};
+
+  EXPECT_EQ(R.getArgsAsMsg(), "can not do this because of that.");
+}