Francis Visoiu Mistrih | 78c92d2 | 2019-07-23 18:09:12 +0000 | [diff] [blame] | 1 | //===- unittest/Support/YAMLRemarksSerializerTest.cpp --------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "llvm/Remarks/Remark.h" |
Francis Visoiu Mistrih | cbbdc41 | 2019-07-23 19:28:03 +0000 | [diff] [blame] | 10 | #include "llvm/Remarks/YAMLRemarkSerializer.h" |
Francis Visoiu Mistrih | c5cc9ef | 2019-07-24 16:36:35 +0000 | [diff] [blame] | 11 | #include "llvm/Support/Error.h" |
Francis Visoiu Mistrih | 78c92d2 | 2019-07-23 18:09:12 +0000 | [diff] [blame] | 12 | #include "gtest/gtest.h" |
| 13 | |
Francis Visoiu Mistrih | 2d8fdca | 2019-07-26 01:33:30 +0000 | [diff] [blame^] | 14 | // We need to supprt Windows paths as well. In order to have paths with the same |
| 15 | // length, use a different path according to the platform. |
| 16 | #ifdef _WIN32 |
| 17 | #define EXTERNALFILETESTPATH "C:/externalfi" |
| 18 | #else |
| 19 | #define EXTERNALFILETESTPATH "/externalfile" |
| 20 | #endif |
| 21 | |
Francis Visoiu Mistrih | 78c92d2 | 2019-07-23 18:09:12 +0000 | [diff] [blame] | 22 | using namespace llvm; |
| 23 | |
Francis Visoiu Mistrih | c5cc9ef | 2019-07-24 16:36:35 +0000 | [diff] [blame] | 24 | static void check(const remarks::Remark &R, StringRef ExpectedR, |
Francis Visoiu Mistrih | 2d8fdca | 2019-07-26 01:33:30 +0000 | [diff] [blame^] | 25 | StringRef ExpectedMeta, bool UseStrTab = false, |
Francis Visoiu Mistrih | c5cc9ef | 2019-07-24 16:36:35 +0000 | [diff] [blame] | 26 | Optional<remarks::StringTable> StrTab = None) { |
Francis Visoiu Mistrih | 78c92d2 | 2019-07-23 18:09:12 +0000 | [diff] [blame] | 27 | std::string Buf; |
| 28 | raw_string_ostream OS(Buf); |
Francis Visoiu Mistrih | ff4b515 | 2019-07-24 19:47:57 +0000 | [diff] [blame] | 29 | Expected<std::unique_ptr<remarks::RemarkSerializer>> MaybeS = [&] { |
Francis Visoiu Mistrih | c5cc9ef | 2019-07-24 16:36:35 +0000 | [diff] [blame] | 30 | if (UseStrTab) { |
| 31 | if (StrTab) |
| 32 | return createRemarkSerializer(remarks::Format::YAMLStrTab, OS, |
| 33 | std::move(*StrTab)); |
| 34 | else |
| 35 | return createRemarkSerializer(remarks::Format::YAMLStrTab, OS); |
| 36 | } else |
| 37 | return createRemarkSerializer(remarks::Format::YAML, OS); |
| 38 | }(); |
| 39 | EXPECT_FALSE(errorToBool(MaybeS.takeError())); |
Francis Visoiu Mistrih | ff4b515 | 2019-07-24 19:47:57 +0000 | [diff] [blame] | 40 | std::unique_ptr<remarks::RemarkSerializer> S = std::move(*MaybeS); |
Francis Visoiu Mistrih | c5b5cc4 | 2019-07-23 20:42:46 +0000 | [diff] [blame] | 41 | |
| 42 | S->emit(R); |
Francis Visoiu Mistrih | c5cc9ef | 2019-07-24 16:36:35 +0000 | [diff] [blame] | 43 | EXPECT_EQ(OS.str(), ExpectedR); |
Francis Visoiu Mistrih | 2d8fdca | 2019-07-26 01:33:30 +0000 | [diff] [blame^] | 44 | |
| 45 | Buf.clear(); |
| 46 | std::unique_ptr<remarks::MetaSerializer> MS = |
| 47 | S->metaSerializer(OS, StringRef(EXTERNALFILETESTPATH)); |
| 48 | MS->emit(); |
| 49 | EXPECT_EQ(OS.str(), ExpectedMeta); |
Francis Visoiu Mistrih | 78c92d2 | 2019-07-23 18:09:12 +0000 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | TEST(YAMLRemarks, SerializerRemark) { |
| 53 | remarks::Remark R; |
| 54 | R.RemarkType = remarks::Type::Missed; |
| 55 | R.PassName = "pass"; |
| 56 | R.RemarkName = "name"; |
| 57 | R.FunctionName = "func"; |
| 58 | R.Loc = remarks::RemarkLocation{"path", 3, 4}; |
| 59 | R.Hotness = 5; |
| 60 | R.Args.emplace_back(); |
| 61 | R.Args.back().Key = "key"; |
| 62 | R.Args.back().Val = "value"; |
| 63 | R.Args.emplace_back(); |
| 64 | R.Args.back().Key = "keydebug"; |
| 65 | R.Args.back().Val = "valuedebug"; |
| 66 | R.Args.back().Loc = remarks::RemarkLocation{"argpath", 6, 7}; |
Francis Visoiu Mistrih | 2d8fdca | 2019-07-26 01:33:30 +0000 | [diff] [blame^] | 67 | check(R, |
| 68 | "--- !Missed\n" |
| 69 | "Pass: pass\n" |
| 70 | "Name: name\n" |
| 71 | "DebugLoc: { File: path, Line: 3, Column: 4 }\n" |
| 72 | "Function: func\n" |
| 73 | "Hotness: 5\n" |
| 74 | "Args:\n" |
| 75 | " - key: value\n" |
| 76 | " - keydebug: valuedebug\n" |
| 77 | " DebugLoc: { File: argpath, Line: 6, Column: 7 }\n" |
| 78 | "...\n", |
| 79 | StringRef("REMARKS\0" |
| 80 | "\0\0\0\0\0\0\0\0" |
| 81 | "\0\0\0\0\0\0\0\0" |
| 82 | EXTERNALFILETESTPATH"\0", |
| 83 | 38)); |
Francis Visoiu Mistrih | 78c92d2 | 2019-07-23 18:09:12 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | TEST(YAMLRemarks, SerializerRemarkStrTab) { |
| 87 | remarks::Remark R; |
| 88 | R.RemarkType = remarks::Type::Missed; |
| 89 | R.PassName = "pass"; |
| 90 | R.RemarkName = "name"; |
| 91 | R.FunctionName = "func"; |
| 92 | R.Loc = remarks::RemarkLocation{"path", 3, 4}; |
| 93 | R.Hotness = 5; |
| 94 | R.Args.emplace_back(); |
| 95 | R.Args.back().Key = "key"; |
| 96 | R.Args.back().Val = "value"; |
| 97 | R.Args.emplace_back(); |
| 98 | R.Args.back().Key = "keydebug"; |
| 99 | R.Args.back().Val = "valuedebug"; |
| 100 | R.Args.back().Loc = remarks::RemarkLocation{"argpath", 6, 7}; |
| 101 | check(R, |
| 102 | "--- !Missed\n" |
| 103 | "Pass: 0\n" |
| 104 | "Name: 1\n" |
| 105 | "DebugLoc: { File: 3, Line: 3, Column: 4 }\n" |
| 106 | "Function: 2\n" |
| 107 | "Hotness: 5\n" |
| 108 | "Args:\n" |
| 109 | " - key: 4\n" |
| 110 | " - keydebug: 5\n" |
| 111 | " DebugLoc: { File: 6, Line: 6, Column: 7 }\n" |
| 112 | "...\n", |
Francis Visoiu Mistrih | 2d8fdca | 2019-07-26 01:33:30 +0000 | [diff] [blame^] | 113 | StringRef("REMARKS\0" |
| 114 | "\0\0\0\0\0\0\0\0" |
| 115 | "\x2d\0\0\0\0\0\0\0" |
| 116 | "pass\0name\0func\0path\0value\0valuedebug\0argpath\0" |
| 117 | EXTERNALFILETESTPATH"\0", |
| 118 | 83), |
| 119 | /*UseStrTab=*/true); |
Francis Visoiu Mistrih | 78c92d2 | 2019-07-23 18:09:12 +0000 | [diff] [blame] | 120 | } |
Francis Visoiu Mistrih | c5cc9ef | 2019-07-24 16:36:35 +0000 | [diff] [blame] | 121 | |
| 122 | TEST(YAMLRemarks, SerializerRemarkParsedStrTab) { |
| 123 | StringRef StrTab("pass\0name\0func\0path\0value\0valuedebug\0argpath\0", 45); |
| 124 | remarks::Remark R; |
| 125 | R.RemarkType = remarks::Type::Missed; |
| 126 | R.PassName = "pass"; |
| 127 | R.RemarkName = "name"; |
| 128 | R.FunctionName = "func"; |
| 129 | R.Loc = remarks::RemarkLocation{"path", 3, 4}; |
| 130 | R.Hotness = 5; |
| 131 | R.Args.emplace_back(); |
| 132 | R.Args.back().Key = "key"; |
| 133 | R.Args.back().Val = "value"; |
| 134 | R.Args.emplace_back(); |
| 135 | R.Args.back().Key = "keydebug"; |
| 136 | R.Args.back().Val = "valuedebug"; |
| 137 | R.Args.back().Loc = remarks::RemarkLocation{"argpath", 6, 7}; |
| 138 | check(R, |
| 139 | "--- !Missed\n" |
| 140 | "Pass: 0\n" |
| 141 | "Name: 1\n" |
| 142 | "DebugLoc: { File: 3, Line: 3, Column: 4 }\n" |
| 143 | "Function: 2\n" |
| 144 | "Hotness: 5\n" |
| 145 | "Args:\n" |
| 146 | " - key: 4\n" |
| 147 | " - keydebug: 5\n" |
| 148 | " DebugLoc: { File: 6, Line: 6, Column: 7 }\n" |
| 149 | "...\n", |
Francis Visoiu Mistrih | 2d8fdca | 2019-07-26 01:33:30 +0000 | [diff] [blame^] | 150 | StringRef("REMARKS\0" |
| 151 | "\0\0\0\0\0\0\0\0" |
| 152 | "\x2d\0\0\0\0\0\0\0" |
| 153 | "pass\0name\0func\0path\0value\0valuedebug\0argpath\0" |
| 154 | EXTERNALFILETESTPATH"\0", |
| 155 | 83), |
| 156 | /*UseStrTab=*/true, |
| 157 | remarks::StringTable(remarks::ParsedStringTable(StrTab))); |
Francis Visoiu Mistrih | c5cc9ef | 2019-07-24 16:36:35 +0000 | [diff] [blame] | 158 | } |