Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 1 | //===---- IncludeInserterTest.cpp - clang-tidy ----------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
Alexander Kornienko | 5f252ca | 2015-08-14 14:31:31 +0000 | [diff] [blame] | 10 | #include "../clang-tidy/utils/IncludeInserter.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 11 | #include "clang/Lex/Preprocessor.h" |
| 12 | #include "clang/Frontend/CompilerInstance.h" |
| 13 | #include "ClangTidyTest.h" |
| 14 | #include "gtest/gtest.h" |
| 15 | |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 16 | // FIXME: Canonicalize paths correctly on windows. |
| 17 | // Currently, adding virtual files will canonicalize the paths before |
| 18 | // storing the virtual entries. |
| 19 | // When resolving virtual entries in the FileManager, the paths (for |
| 20 | // example coming from a #include directive) are not canonicalized |
| 21 | // to native paths; thus, the virtual file is not found. |
| 22 | // This needs to be fixed in the FileManager before we can make |
| 23 | // clang-tidy tests work. |
| 24 | #if !defined(_WIN32) |
| 25 | |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 26 | namespace clang { |
| 27 | namespace tidy { |
| 28 | namespace { |
| 29 | |
| 30 | class IncludeInserterCheckBase : public ClangTidyCheck { |
| 31 | public: |
Manuel Klimek | 46e82c3 | 2015-08-11 12:59:22 +0000 | [diff] [blame] | 32 | IncludeInserterCheckBase(StringRef CheckName, ClangTidyContext *Context) |
| 33 | : ClangTidyCheck(CheckName, Context) {} |
Daniel Jasper | d30dc3f | 2015-08-19 21:02:27 +0000 | [diff] [blame] | 34 | |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 35 | void registerPPCallbacks(CompilerInstance &Compiler) override { |
Etienne Bergeron | 2a4c00f | 2016-05-03 02:54:05 +0000 | [diff] [blame] | 36 | Inserter.reset(new utils::IncludeInserter( |
| 37 | Compiler.getSourceManager(), |
| 38 | Compiler.getLangOpts(), |
| 39 | utils::IncludeSorter::IS_Google)); |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 40 | Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks()); |
| 41 | } |
| 42 | |
| 43 | void registerMatchers(ast_matchers::MatchFinder *Finder) override { |
| 44 | Finder->addMatcher(ast_matchers::declStmt().bind("stmt"), this); |
| 45 | } |
| 46 | |
| 47 | void check(const ast_matchers::MatchFinder::MatchResult &Result) override { |
Alexander Kornienko | 9f58fe0 | 2016-12-13 16:19:19 +0000 | [diff] [blame] | 48 | auto Diag = diag(Result.Nodes.getNodeAs<DeclStmt>("stmt")->getLocStart(), |
Daniel Jasper | d30dc3f | 2015-08-19 21:02:27 +0000 | [diff] [blame] | 49 | "foo, bar"); |
| 50 | for (StringRef header : HeadersToInclude()) { |
| 51 | auto Fixit = Inserter->CreateIncludeInsertion( |
| 52 | Result.SourceManager->getMainFileID(), header, IsAngledInclude()); |
| 53 | if (Fixit) { |
| 54 | Diag << *Fixit; |
| 55 | } |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 56 | } |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 57 | } |
| 58 | |
Daniel Jasper | d30dc3f | 2015-08-19 21:02:27 +0000 | [diff] [blame] | 59 | virtual std::vector<StringRef> HeadersToInclude() const = 0; |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 60 | virtual bool IsAngledInclude() const = 0; |
| 61 | |
Etienne Bergeron | 2a4c00f | 2016-05-03 02:54:05 +0000 | [diff] [blame] | 62 | std::unique_ptr<utils::IncludeInserter> Inserter; |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | class NonSystemHeaderInserterCheck : public IncludeInserterCheckBase { |
| 66 | public: |
Manuel Klimek | 46e82c3 | 2015-08-11 12:59:22 +0000 | [diff] [blame] | 67 | NonSystemHeaderInserterCheck(StringRef CheckName, ClangTidyContext *Context) |
| 68 | : IncludeInserterCheckBase(CheckName, Context) {} |
Daniel Jasper | d30dc3f | 2015-08-19 21:02:27 +0000 | [diff] [blame] | 69 | |
| 70 | std::vector<StringRef> HeadersToInclude() const override { |
| 71 | return {"path/to/header.h"}; |
| 72 | } |
| 73 | bool IsAngledInclude() const override { return false; } |
| 74 | }; |
| 75 | |
Alexander Kornienko | e3d91a5 | 2017-01-12 15:31:50 +0000 | [diff] [blame] | 76 | class EarlyInAlphabetHeaderInserterCheck : public IncludeInserterCheckBase { |
| 77 | public: |
| 78 | EarlyInAlphabetHeaderInserterCheck(StringRef CheckName, ClangTidyContext *Context) |
| 79 | : IncludeInserterCheckBase(CheckName, Context) {} |
| 80 | |
| 81 | std::vector<StringRef> HeadersToInclude() const override { |
| 82 | return {"a/header.h"}; |
| 83 | } |
| 84 | bool IsAngledInclude() const override { return false; } |
| 85 | }; |
| 86 | |
Daniel Jasper | d30dc3f | 2015-08-19 21:02:27 +0000 | [diff] [blame] | 87 | class MultipleHeaderInserterCheck : public IncludeInserterCheckBase { |
| 88 | public: |
| 89 | MultipleHeaderInserterCheck(StringRef CheckName, ClangTidyContext *Context) |
| 90 | : IncludeInserterCheckBase(CheckName, Context) {} |
Daniel Jasper | d30dc3f | 2015-08-19 21:02:27 +0000 | [diff] [blame] | 91 | |
| 92 | std::vector<StringRef> HeadersToInclude() const override { |
| 93 | return {"path/to/header.h", "path/to/header2.h", "path/to/header.h"}; |
| 94 | } |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 95 | bool IsAngledInclude() const override { return false; } |
| 96 | }; |
| 97 | |
Alexander Kornienko | 8cc024e | 2015-08-14 12:33:25 +0000 | [diff] [blame] | 98 | class CSystemIncludeInserterCheck : public IncludeInserterCheckBase { |
| 99 | public: |
Alexander Kornienko | 078ab13 | 2015-08-14 13:23:55 +0000 | [diff] [blame] | 100 | CSystemIncludeInserterCheck(StringRef CheckName, ClangTidyContext *Context) |
| 101 | : IncludeInserterCheckBase(CheckName, Context) {} |
Daniel Jasper | d30dc3f | 2015-08-19 21:02:27 +0000 | [diff] [blame] | 102 | |
| 103 | std::vector<StringRef> HeadersToInclude() const override { |
| 104 | return {"stdlib.h"}; |
| 105 | } |
Alexander Kornienko | 8cc024e | 2015-08-14 12:33:25 +0000 | [diff] [blame] | 106 | bool IsAngledInclude() const override { return true; } |
| 107 | }; |
| 108 | |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 109 | class CXXSystemIncludeInserterCheck : public IncludeInserterCheckBase { |
| 110 | public: |
Manuel Klimek | 46e82c3 | 2015-08-11 12:59:22 +0000 | [diff] [blame] | 111 | CXXSystemIncludeInserterCheck(StringRef CheckName, ClangTidyContext *Context) |
| 112 | : IncludeInserterCheckBase(CheckName, Context) {} |
Daniel Jasper | d30dc3f | 2015-08-19 21:02:27 +0000 | [diff] [blame] | 113 | |
| 114 | std::vector<StringRef> HeadersToInclude() const override { return {"set"}; } |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 115 | bool IsAngledInclude() const override { return true; } |
| 116 | }; |
| 117 | |
| 118 | template <typename Check> |
Daniel Jasper | d30dc3f | 2015-08-19 21:02:27 +0000 | [diff] [blame] | 119 | std::string runCheckOnCode(StringRef Code, StringRef Filename) { |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 120 | std::vector<ClangTidyError> Errors; |
| 121 | return test::runCheckOnCode<Check>(Code, &Errors, Filename, None, |
| 122 | ClangTidyOptions(), |
| 123 | {// Main file include |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 124 | {"clang_tidy/tests/" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 125 | "insert_includes_test_header.h", |
| 126 | "\n"}, |
| 127 | // Non system headers |
Alexander Kornienko | e3d91a5 | 2017-01-12 15:31:50 +0000 | [diff] [blame] | 128 | {"a/header.h", "\n"}, |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 129 | {"path/to/a/header.h", "\n"}, |
| 130 | {"path/to/z/header.h", "\n"}, |
| 131 | {"path/to/header.h", "\n"}, |
Daniel Jasper | d30dc3f | 2015-08-19 21:02:27 +0000 | [diff] [blame] | 132 | {"path/to/header2.h", "\n"}, |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 133 | // Fake system headers. |
| 134 | {"stdlib.h", "\n"}, |
| 135 | {"unistd.h", "\n"}, |
| 136 | {"list", "\n"}, |
| 137 | {"map", "\n"}, |
| 138 | {"set", "\n"}, |
| 139 | {"vector", "\n"}}); |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | TEST(IncludeInserterTest, InsertAfterLastNonSystemInclude) { |
| 143 | const char *PreCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 144 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 145 | |
| 146 | #include <list> |
| 147 | #include <map> |
| 148 | |
| 149 | #include "path/to/a/header.h" |
| 150 | |
| 151 | void foo() { |
| 152 | int a = 0; |
| 153 | })"; |
| 154 | const char *PostCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 155 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 156 | |
| 157 | #include <list> |
| 158 | #include <map> |
| 159 | |
| 160 | #include "path/to/a/header.h" |
| 161 | #include "path/to/header.h" |
| 162 | |
| 163 | void foo() { |
| 164 | int a = 0; |
| 165 | })"; |
| 166 | |
| 167 | EXPECT_EQ(PostCode, runCheckOnCode<NonSystemHeaderInserterCheck>( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 168 | PreCode, "clang_tidy/tests/" |
Daniel Jasper | d30dc3f | 2015-08-19 21:02:27 +0000 | [diff] [blame] | 169 | "insert_includes_test_input2.cc")); |
| 170 | } |
| 171 | |
| 172 | TEST(IncludeInserterTest, InsertMultipleIncludesAndDeduplicate) { |
| 173 | const char *PreCode = R"( |
| 174 | #include "clang_tidy/tests/insert_includes_test_header.h" |
| 175 | |
| 176 | #include <list> |
| 177 | #include <map> |
| 178 | |
| 179 | #include "path/to/a/header.h" |
| 180 | |
| 181 | void foo() { |
| 182 | int a = 0; |
| 183 | })"; |
| 184 | const char *PostCode = R"( |
| 185 | #include "clang_tidy/tests/insert_includes_test_header.h" |
| 186 | |
| 187 | #include <list> |
| 188 | #include <map> |
| 189 | |
| 190 | #include "path/to/a/header.h" |
| 191 | #include "path/to/header.h" |
| 192 | #include "path/to/header2.h" |
| 193 | |
| 194 | void foo() { |
| 195 | int a = 0; |
| 196 | })"; |
| 197 | |
| 198 | EXPECT_EQ(PostCode, runCheckOnCode<MultipleHeaderInserterCheck>( |
| 199 | PreCode, "clang_tidy/tests/" |
| 200 | "insert_includes_test_input2.cc")); |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | TEST(IncludeInserterTest, InsertBeforeFirstNonSystemInclude) { |
| 204 | const char *PreCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 205 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 206 | |
| 207 | #include <list> |
| 208 | #include <map> |
| 209 | |
| 210 | #include "path/to/z/header.h" |
| 211 | |
| 212 | void foo() { |
| 213 | int a = 0; |
| 214 | })"; |
| 215 | const char *PostCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 216 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 217 | |
| 218 | #include <list> |
| 219 | #include <map> |
| 220 | |
| 221 | #include "path/to/header.h" |
| 222 | #include "path/to/z/header.h" |
| 223 | |
| 224 | void foo() { |
| 225 | int a = 0; |
| 226 | })"; |
| 227 | |
| 228 | EXPECT_EQ(PostCode, runCheckOnCode<NonSystemHeaderInserterCheck>( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 229 | PreCode, "clang_tidy/tests/" |
Daniel Jasper | d30dc3f | 2015-08-19 21:02:27 +0000 | [diff] [blame] | 230 | "insert_includes_test_input2.cc")); |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | TEST(IncludeInserterTest, InsertBetweenNonSystemIncludes) { |
| 234 | const char *PreCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 235 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 236 | |
| 237 | #include <list> |
| 238 | #include <map> |
| 239 | |
| 240 | #include "path/to/a/header.h" |
| 241 | #include "path/to/z/header.h" |
| 242 | |
| 243 | void foo() { |
| 244 | int a = 0; |
| 245 | })"; |
| 246 | const char *PostCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 247 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 248 | |
| 249 | #include <list> |
| 250 | #include <map> |
| 251 | |
| 252 | #include "path/to/a/header.h" |
| 253 | #include "path/to/header.h" |
| 254 | #include "path/to/z/header.h" |
| 255 | |
| 256 | void foo() { |
| 257 | int a = 0; |
| 258 | })"; |
| 259 | |
| 260 | EXPECT_EQ(PostCode, runCheckOnCode<NonSystemHeaderInserterCheck>( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 261 | PreCode, "clang_tidy/tests/" |
Daniel Jasper | d30dc3f | 2015-08-19 21:02:27 +0000 | [diff] [blame] | 262 | "insert_includes_test_input2.cc")); |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | TEST(IncludeInserterTest, NonSystemIncludeAlreadyIncluded) { |
| 266 | const char *PreCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 267 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 268 | |
| 269 | #include <list> |
| 270 | #include <map> |
| 271 | |
| 272 | #include "path/to/a/header.h" |
| 273 | #include "path/to/header.h" |
| 274 | #include "path/to/z/header.h" |
| 275 | |
| 276 | void foo() { |
| 277 | int a = 0; |
| 278 | })"; |
| 279 | EXPECT_EQ(PreCode, runCheckOnCode<NonSystemHeaderInserterCheck>( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 280 | PreCode, "clang_tidy/tests/" |
Daniel Jasper | d30dc3f | 2015-08-19 21:02:27 +0000 | [diff] [blame] | 281 | "insert_includes_test_input2.cc")); |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | TEST(IncludeInserterTest, InsertNonSystemIncludeAfterLastCXXSystemInclude) { |
| 285 | const char *PreCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 286 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 287 | |
| 288 | #include <list> |
| 289 | #include <map> |
| 290 | |
| 291 | void foo() { |
| 292 | int a = 0; |
| 293 | })"; |
| 294 | const char *PostCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 295 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 296 | |
| 297 | #include <list> |
| 298 | #include <map> |
| 299 | |
| 300 | #include "path/to/header.h" |
| 301 | |
| 302 | void foo() { |
| 303 | int a = 0; |
| 304 | })"; |
| 305 | |
| 306 | EXPECT_EQ(PostCode, runCheckOnCode<NonSystemHeaderInserterCheck>( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 307 | PreCode, "clang_tidy/tests/" |
Daniel Jasper | d30dc3f | 2015-08-19 21:02:27 +0000 | [diff] [blame] | 308 | "insert_includes_test_header.cc")); |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | TEST(IncludeInserterTest, InsertNonSystemIncludeAfterMainFileInclude) { |
| 312 | const char *PreCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 313 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 314 | |
| 315 | void foo() { |
| 316 | int a = 0; |
| 317 | })"; |
| 318 | const char *PostCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 319 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 320 | |
| 321 | #include "path/to/header.h" |
| 322 | |
| 323 | void foo() { |
| 324 | int a = 0; |
| 325 | })"; |
| 326 | |
| 327 | EXPECT_EQ(PostCode, runCheckOnCode<NonSystemHeaderInserterCheck>( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 328 | PreCode, "clang_tidy/tests/" |
Daniel Jasper | d30dc3f | 2015-08-19 21:02:27 +0000 | [diff] [blame] | 329 | "insert_includes_test_header.cc")); |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 330 | } |
| 331 | |
| 332 | TEST(IncludeInserterTest, InsertCXXSystemIncludeAfterLastCXXSystemInclude) { |
| 333 | const char *PreCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 334 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 335 | |
| 336 | #include <list> |
| 337 | #include <map> |
| 338 | |
| 339 | #include "path/to/a/header.h" |
| 340 | |
| 341 | void foo() { |
| 342 | int a = 0; |
| 343 | })"; |
| 344 | const char *PostCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 345 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 346 | |
| 347 | #include <list> |
| 348 | #include <map> |
| 349 | #include <set> |
| 350 | |
| 351 | #include "path/to/a/header.h" |
| 352 | |
| 353 | void foo() { |
| 354 | int a = 0; |
| 355 | })"; |
| 356 | |
| 357 | EXPECT_EQ(PostCode, runCheckOnCode<CXXSystemIncludeInserterCheck>( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 358 | PreCode, "clang_tidy/tests/" |
Daniel Jasper | d30dc3f | 2015-08-19 21:02:27 +0000 | [diff] [blame] | 359 | "insert_includes_test_header.cc")); |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | TEST(IncludeInserterTest, InsertCXXSystemIncludeBeforeFirstCXXSystemInclude) { |
| 363 | const char *PreCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 364 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 365 | |
| 366 | #include <vector> |
| 367 | |
| 368 | #include "path/to/a/header.h" |
| 369 | |
| 370 | void foo() { |
| 371 | int a = 0; |
| 372 | })"; |
| 373 | const char *PostCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 374 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 375 | |
| 376 | #include <set> |
| 377 | #include <vector> |
| 378 | |
| 379 | #include "path/to/a/header.h" |
| 380 | |
| 381 | void foo() { |
| 382 | int a = 0; |
| 383 | })"; |
| 384 | |
| 385 | EXPECT_EQ(PostCode, runCheckOnCode<CXXSystemIncludeInserterCheck>( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 386 | PreCode, "clang_tidy/tests/" |
Daniel Jasper | d30dc3f | 2015-08-19 21:02:27 +0000 | [diff] [blame] | 387 | "insert_includes_test_header.cc")); |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | TEST(IncludeInserterTest, InsertCXXSystemIncludeBetweenCXXSystemIncludes) { |
| 391 | const char *PreCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 392 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 393 | |
| 394 | #include <map> |
| 395 | #include <vector> |
| 396 | |
| 397 | #include "path/to/a/header.h" |
| 398 | |
| 399 | void foo() { |
| 400 | int a = 0; |
| 401 | })"; |
| 402 | const char *PostCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 403 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 404 | |
| 405 | #include <map> |
| 406 | #include <set> |
| 407 | #include <vector> |
| 408 | |
| 409 | #include "path/to/a/header.h" |
| 410 | |
| 411 | void foo() { |
| 412 | int a = 0; |
| 413 | })"; |
| 414 | |
| 415 | EXPECT_EQ(PostCode, runCheckOnCode<CXXSystemIncludeInserterCheck>( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 416 | PreCode, "clang_tidy/tests/" |
Daniel Jasper | d30dc3f | 2015-08-19 21:02:27 +0000 | [diff] [blame] | 417 | "insert_includes_test_header.cc")); |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | TEST(IncludeInserterTest, InsertCXXSystemIncludeAfterMainFileInclude) { |
| 421 | const char *PreCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 422 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 423 | |
| 424 | #include "path/to/a/header.h" |
| 425 | |
| 426 | void foo() { |
| 427 | int a = 0; |
| 428 | })"; |
| 429 | const char *PostCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 430 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 431 | |
| 432 | #include <set> |
| 433 | |
| 434 | #include "path/to/a/header.h" |
| 435 | |
| 436 | void foo() { |
| 437 | int a = 0; |
| 438 | })"; |
| 439 | |
| 440 | EXPECT_EQ(PostCode, runCheckOnCode<CXXSystemIncludeInserterCheck>( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 441 | PreCode, "clang_tidy/tests/" |
Daniel Jasper | d30dc3f | 2015-08-19 21:02:27 +0000 | [diff] [blame] | 442 | "insert_includes_test_header.cc")); |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | TEST(IncludeInserterTest, InsertCXXSystemIncludeAfterCSystemInclude) { |
| 446 | const char *PreCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 447 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 448 | |
| 449 | #include <stdlib.h> |
| 450 | |
| 451 | #include "path/to/a/header.h" |
| 452 | |
| 453 | void foo() { |
| 454 | int a = 0; |
| 455 | })"; |
| 456 | const char *PostCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 457 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 458 | |
| 459 | #include <stdlib.h> |
| 460 | |
| 461 | #include <set> |
| 462 | |
| 463 | #include "path/to/a/header.h" |
| 464 | |
| 465 | void foo() { |
| 466 | int a = 0; |
| 467 | })"; |
| 468 | |
| 469 | EXPECT_EQ(PostCode, runCheckOnCode<CXXSystemIncludeInserterCheck>( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 470 | PreCode, "clang_tidy/tests/" |
Daniel Jasper | d30dc3f | 2015-08-19 21:02:27 +0000 | [diff] [blame] | 471 | "insert_includes_test_header.cc")); |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Alexander Kornienko | 8cc024e | 2015-08-14 12:33:25 +0000 | [diff] [blame] | 474 | TEST(IncludeInserterTest, InsertCXXSystemIncludeBeforeNonSystemInclude) { |
| 475 | const char *PreCode = R"( |
| 476 | #include "path/to/a/header.h" |
| 477 | |
| 478 | void foo() { |
| 479 | int a = 0; |
| 480 | })"; |
| 481 | const char *PostCode = R"( |
| 482 | #include <set> |
| 483 | |
| 484 | #include "path/to/a/header.h" |
| 485 | |
| 486 | void foo() { |
| 487 | int a = 0; |
| 488 | })"; |
| 489 | |
| 490 | EXPECT_EQ(PostCode, runCheckOnCode<CXXSystemIncludeInserterCheck>( |
| 491 | PreCode, "devtools/cymbal/clang_tidy/tests/" |
Daniel Jasper | d30dc3f | 2015-08-19 21:02:27 +0000 | [diff] [blame] | 492 | "insert_includes_test_header.cc")); |
Alexander Kornienko | 8cc024e | 2015-08-14 12:33:25 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | TEST(IncludeInserterTest, InsertCSystemIncludeBeforeCXXSystemInclude) { |
| 496 | const char *PreCode = R"( |
| 497 | #include <set> |
| 498 | |
| 499 | #include "path/to/a/header.h" |
| 500 | |
| 501 | void foo() { |
| 502 | int a = 0; |
| 503 | })"; |
| 504 | const char *PostCode = R"( |
| 505 | #include <stdlib.h> |
| 506 | |
| 507 | #include <set> |
| 508 | |
| 509 | #include "path/to/a/header.h" |
| 510 | |
| 511 | void foo() { |
| 512 | int a = 0; |
| 513 | })"; |
| 514 | |
| 515 | EXPECT_EQ(PostCode, runCheckOnCode<CSystemIncludeInserterCheck>( |
| 516 | PreCode, "devtools/cymbal/clang_tidy/tests/" |
Daniel Jasper | d30dc3f | 2015-08-19 21:02:27 +0000 | [diff] [blame] | 517 | "insert_includes_test_header.cc")); |
Alexander Kornienko | 8cc024e | 2015-08-14 12:33:25 +0000 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | TEST(IncludeInserterTest, InsertIncludeIfThereWasNoneBefore) { |
| 521 | const char *PreCode = R"( |
| 522 | void foo() { |
| 523 | int a = 0; |
| 524 | })"; |
| 525 | const char *PostCode = R"(#include <set> |
| 526 | |
| 527 | |
| 528 | void foo() { |
| 529 | int a = 0; |
| 530 | })"; |
| 531 | |
| 532 | EXPECT_EQ(PostCode, runCheckOnCode<CXXSystemIncludeInserterCheck>( |
| 533 | PreCode, "devtools/cymbal/clang_tidy/tests/" |
Daniel Jasper | d30dc3f | 2015-08-19 21:02:27 +0000 | [diff] [blame] | 534 | "insert_includes_test_header.cc")); |
Alexander Kornienko | 8cc024e | 2015-08-14 12:33:25 +0000 | [diff] [blame] | 535 | } |
| 536 | |
Alexander Kornienko | e3d91a5 | 2017-01-12 15:31:50 +0000 | [diff] [blame] | 537 | TEST(IncludeInserterTest, DontInsertDuplicateIncludeEvenIfMiscategorized) { |
| 538 | const char *PreCode = R"( |
| 539 | #include "clang_tidy/tests/insert_includes_test_header.h" |
| 540 | |
| 541 | #include <map> |
| 542 | #include <set> |
| 543 | #include <vector> |
| 544 | |
| 545 | #include "a/header.h" |
| 546 | #include "path/to/a/header.h" |
| 547 | #include "path/to/header.h" |
| 548 | |
| 549 | void foo() { |
| 550 | int a = 0; |
| 551 | })"; |
| 552 | |
| 553 | const char *PostCode = R"( |
| 554 | #include "clang_tidy/tests/insert_includes_test_header.h" |
| 555 | |
| 556 | #include <map> |
| 557 | #include <set> |
| 558 | #include <vector> |
| 559 | |
| 560 | #include "a/header.h" |
| 561 | #include "path/to/a/header.h" |
| 562 | #include "path/to/header.h" |
| 563 | |
| 564 | void foo() { |
| 565 | int a = 0; |
| 566 | })"; |
| 567 | |
| 568 | EXPECT_EQ(PostCode, runCheckOnCode<EarlyInAlphabetHeaderInserterCheck>( |
| 569 | PreCode, "workspace_folder/clang_tidy/tests/" |
| 570 | "insert_includes_test_header.cc")); |
| 571 | } |
| 572 | |
| 573 | TEST(IncludeInserterTest, HandleOrderInSubdirectory) { |
| 574 | const char *PreCode = R"( |
| 575 | #include "clang_tidy/tests/insert_includes_test_header.h" |
| 576 | |
| 577 | #include <map> |
| 578 | #include <set> |
| 579 | #include <vector> |
| 580 | |
| 581 | #include "path/to/a/header.h" |
| 582 | #include "path/to/header.h" |
| 583 | |
| 584 | void foo() { |
| 585 | int a = 0; |
| 586 | })"; |
| 587 | |
| 588 | const char *PostCode = R"( |
| 589 | #include "clang_tidy/tests/insert_includes_test_header.h" |
| 590 | |
| 591 | #include <map> |
| 592 | #include <set> |
| 593 | #include <vector> |
| 594 | |
| 595 | #include "a/header.h" |
| 596 | #include "path/to/a/header.h" |
| 597 | #include "path/to/header.h" |
| 598 | |
| 599 | void foo() { |
| 600 | int a = 0; |
| 601 | })"; |
| 602 | |
| 603 | EXPECT_EQ(PostCode, runCheckOnCode<EarlyInAlphabetHeaderInserterCheck>( |
| 604 | PreCode, "workspace_folder/clang_tidy/tests/" |
| 605 | "insert_includes_test_header.cc")); |
| 606 | } |
| 607 | |
Eugene Zelenko | 7da47b8 | 2016-01-26 22:32:24 +0000 | [diff] [blame] | 608 | } // anonymous namespace |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 609 | } // namespace tidy |
| 610 | } // namespace clang |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 611 | |
Alexander Kornienko | 078ab13 | 2015-08-14 13:23:55 +0000 | [diff] [blame] | 612 | #endif |