reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122
  123
//===- 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 "llvm/Remarks/RemarkStringTable.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.");
}

TEST(RemarksAPI, StringTableInternalize) {
  remarks::StringTable StrTab;

  // Empty table.
  EXPECT_EQ(StrTab.SerializedSize, 0UL);

  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.Args.emplace_back();
  R.Args.back().Key = "keydebug";
  R.Args.back().Val = "valuedebug";
  R.Args.back().Loc = remarks::RemarkLocation{"argpath", 6, 7};

  // Check that internalize starts using the strings from the string table.
  remarks::Remark R2 = R.clone();
  StrTab.internalize(R2);

  // Check that the pointers in the remarks are different.
  EXPECT_NE(R.PassName.data(), R2.PassName.data());
  EXPECT_NE(R.RemarkName.data(), R2.RemarkName.data());
  EXPECT_NE(R.FunctionName.data(), R2.FunctionName.data());
  EXPECT_NE(R.Loc->SourceFilePath.data(), R2.Loc->SourceFilePath.data());
  EXPECT_NE(R.Args.back().Key.data(), R2.Args.back().Key.data());
  EXPECT_NE(R.Args.back().Val.data(), R2.Args.back().Val.data());
  EXPECT_NE(R.Args.back().Loc->SourceFilePath.data(),
            R2.Args.back().Loc->SourceFilePath.data());

  // Check that the internalized remark is using the pointers from the string table.
  EXPECT_EQ(StrTab.add(R.PassName).second.data(), R2.PassName.data());
  EXPECT_EQ(StrTab.add(R.RemarkName).second.data(), R2.RemarkName.data());
  EXPECT_EQ(StrTab.add(R.FunctionName).second.data(), R2.FunctionName.data());
  EXPECT_EQ(StrTab.add(R.Loc->SourceFilePath).second.data(),
            R2.Loc->SourceFilePath.data());
  EXPECT_EQ(StrTab.add(R.Args.back().Key).second.data(),
            R2.Args.back().Key.data());
  EXPECT_EQ(StrTab.add(R.Args.back().Val).second.data(),
            R2.Args.back().Val.data());
  EXPECT_EQ(StrTab.add(R.Args.back().Loc->SourceFilePath).second.data(),
            R2.Args.back().Loc->SourceFilePath.data());
}