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) {} |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 34 | void registerPPCallbacks(CompilerInstance &Compiler) override { |
| 35 | Inserter.reset(new IncludeInserter(Compiler.getSourceManager(), |
| 36 | Compiler.getLangOpts(), |
| 37 | IncludeSorter::IS_Google)); |
| 38 | Compiler.getPreprocessor().addPPCallbacks(Inserter->CreatePPCallbacks()); |
| 39 | } |
| 40 | |
| 41 | void registerMatchers(ast_matchers::MatchFinder *Finder) override { |
| 42 | Finder->addMatcher(ast_matchers::declStmt().bind("stmt"), this); |
| 43 | } |
| 44 | |
| 45 | void check(const ast_matchers::MatchFinder::MatchResult &Result) override { |
| 46 | auto Fixit = |
| 47 | Inserter->CreateIncludeInsertion(Result.SourceManager->getMainFileID(), |
| 48 | HeaderToInclude(), IsAngledInclude()); |
| 49 | if (Fixit) { |
| 50 | diag(Result.Nodes.getStmtAs<DeclStmt>("stmt")->getLocStart(), "foo, bar") |
| 51 | << *Fixit; |
| 52 | } |
| 53 | // Second include should yield no Fixit. |
| 54 | Fixit = |
| 55 | Inserter->CreateIncludeInsertion(Result.SourceManager->getMainFileID(), |
| 56 | HeaderToInclude(), IsAngledInclude()); |
| 57 | EXPECT_FALSE(Fixit); |
| 58 | } |
| 59 | |
| 60 | virtual StringRef HeaderToInclude() const = 0; |
| 61 | virtual bool IsAngledInclude() const = 0; |
| 62 | |
| 63 | std::unique_ptr<IncludeInserter> Inserter; |
| 64 | }; |
| 65 | |
| 66 | class NonSystemHeaderInserterCheck : public IncludeInserterCheckBase { |
| 67 | public: |
Manuel Klimek | 46e82c3 | 2015-08-11 12:59:22 +0000 | [diff] [blame] | 68 | NonSystemHeaderInserterCheck(StringRef CheckName, ClangTidyContext *Context) |
| 69 | : IncludeInserterCheckBase(CheckName, Context) {} |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 70 | StringRef HeaderToInclude() const override { return "path/to/header.h"; } |
| 71 | bool IsAngledInclude() const override { return false; } |
| 72 | }; |
| 73 | |
Alexander Kornienko | 8cc024e | 2015-08-14 12:33:25 +0000 | [diff] [blame] | 74 | class CSystemIncludeInserterCheck : public IncludeInserterCheckBase { |
| 75 | public: |
Alexander Kornienko | 078ab13 | 2015-08-14 13:23:55 +0000 | [diff] [blame] | 76 | CSystemIncludeInserterCheck(StringRef CheckName, ClangTidyContext *Context) |
| 77 | : IncludeInserterCheckBase(CheckName, Context) {} |
Alexander Kornienko | 8cc024e | 2015-08-14 12:33:25 +0000 | [diff] [blame] | 78 | StringRef HeaderToInclude() const override { return "stdlib.h"; } |
| 79 | bool IsAngledInclude() const override { return true; } |
| 80 | }; |
| 81 | |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 82 | class CXXSystemIncludeInserterCheck : public IncludeInserterCheckBase { |
| 83 | public: |
Manuel Klimek | 46e82c3 | 2015-08-11 12:59:22 +0000 | [diff] [blame] | 84 | CXXSystemIncludeInserterCheck(StringRef CheckName, ClangTidyContext *Context) |
| 85 | : IncludeInserterCheckBase(CheckName, Context) {} |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 86 | StringRef HeaderToInclude() const override { return "set"; } |
| 87 | bool IsAngledInclude() const override { return true; } |
| 88 | }; |
| 89 | |
| 90 | template <typename Check> |
| 91 | std::string runCheckOnCode(StringRef Code, StringRef Filename, |
| 92 | size_t NumWarningsExpected) { |
| 93 | std::vector<ClangTidyError> Errors; |
| 94 | return test::runCheckOnCode<Check>(Code, &Errors, Filename, None, |
| 95 | ClangTidyOptions(), |
| 96 | {// Main file include |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 97 | {"clang_tidy/tests/" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 98 | "insert_includes_test_header.h", |
| 99 | "\n"}, |
| 100 | // Non system headers |
| 101 | {"path/to/a/header.h", "\n"}, |
| 102 | {"path/to/z/header.h", "\n"}, |
| 103 | {"path/to/header.h", "\n"}, |
| 104 | // Fake system headers. |
| 105 | {"stdlib.h", "\n"}, |
| 106 | {"unistd.h", "\n"}, |
| 107 | {"list", "\n"}, |
| 108 | {"map", "\n"}, |
| 109 | {"set", "\n"}, |
| 110 | {"vector", "\n"}}); |
| 111 | EXPECT_EQ(NumWarningsExpected, Errors.size()); |
| 112 | } |
| 113 | |
| 114 | TEST(IncludeInserterTest, InsertAfterLastNonSystemInclude) { |
| 115 | const char *PreCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 116 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 117 | |
| 118 | #include <list> |
| 119 | #include <map> |
| 120 | |
| 121 | #include "path/to/a/header.h" |
| 122 | |
| 123 | void foo() { |
| 124 | int a = 0; |
| 125 | })"; |
| 126 | const char *PostCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 127 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 128 | |
| 129 | #include <list> |
| 130 | #include <map> |
| 131 | |
| 132 | #include "path/to/a/header.h" |
| 133 | #include "path/to/header.h" |
| 134 | |
| 135 | void foo() { |
| 136 | int a = 0; |
| 137 | })"; |
| 138 | |
| 139 | EXPECT_EQ(PostCode, runCheckOnCode<NonSystemHeaderInserterCheck>( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 140 | PreCode, "clang_tidy/tests/" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 141 | "insert_includes_test_input2.cc", |
| 142 | 1)); |
| 143 | } |
| 144 | |
| 145 | TEST(IncludeInserterTest, InsertBeforeFirstNonSystemInclude) { |
| 146 | const char *PreCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 147 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 148 | |
| 149 | #include <list> |
| 150 | #include <map> |
| 151 | |
| 152 | #include "path/to/z/header.h" |
| 153 | |
| 154 | void foo() { |
| 155 | int a = 0; |
| 156 | })"; |
| 157 | const char *PostCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 158 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 159 | |
| 160 | #include <list> |
| 161 | #include <map> |
| 162 | |
| 163 | #include "path/to/header.h" |
| 164 | #include "path/to/z/header.h" |
| 165 | |
| 166 | void foo() { |
| 167 | int a = 0; |
| 168 | })"; |
| 169 | |
| 170 | EXPECT_EQ(PostCode, runCheckOnCode<NonSystemHeaderInserterCheck>( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 171 | PreCode, "clang_tidy/tests/" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 172 | "insert_includes_test_input2.cc", |
| 173 | 1)); |
| 174 | } |
| 175 | |
| 176 | TEST(IncludeInserterTest, InsertBetweenNonSystemIncludes) { |
| 177 | const char *PreCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 178 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 179 | |
| 180 | #include <list> |
| 181 | #include <map> |
| 182 | |
| 183 | #include "path/to/a/header.h" |
| 184 | #include "path/to/z/header.h" |
| 185 | |
| 186 | void foo() { |
| 187 | int a = 0; |
| 188 | })"; |
| 189 | const char *PostCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 190 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 191 | |
| 192 | #include <list> |
| 193 | #include <map> |
| 194 | |
| 195 | #include "path/to/a/header.h" |
| 196 | #include "path/to/header.h" |
| 197 | #include "path/to/z/header.h" |
| 198 | |
| 199 | void foo() { |
| 200 | int a = 0; |
| 201 | })"; |
| 202 | |
| 203 | EXPECT_EQ(PostCode, runCheckOnCode<NonSystemHeaderInserterCheck>( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 204 | PreCode, "clang_tidy/tests/" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 205 | "insert_includes_test_input2.cc", |
| 206 | 1)); |
| 207 | } |
| 208 | |
| 209 | TEST(IncludeInserterTest, NonSystemIncludeAlreadyIncluded) { |
| 210 | const char *PreCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 211 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 212 | |
| 213 | #include <list> |
| 214 | #include <map> |
| 215 | |
| 216 | #include "path/to/a/header.h" |
| 217 | #include "path/to/header.h" |
| 218 | #include "path/to/z/header.h" |
| 219 | |
| 220 | void foo() { |
| 221 | int a = 0; |
| 222 | })"; |
| 223 | EXPECT_EQ(PreCode, runCheckOnCode<NonSystemHeaderInserterCheck>( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 224 | PreCode, "clang_tidy/tests/" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 225 | "insert_includes_test_input2.cc", |
| 226 | 0)); |
| 227 | } |
| 228 | |
| 229 | TEST(IncludeInserterTest, InsertNonSystemIncludeAfterLastCXXSystemInclude) { |
| 230 | const char *PreCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 231 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 232 | |
| 233 | #include <list> |
| 234 | #include <map> |
| 235 | |
| 236 | void foo() { |
| 237 | int a = 0; |
| 238 | })"; |
| 239 | const char *PostCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 240 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 241 | |
| 242 | #include <list> |
| 243 | #include <map> |
| 244 | |
| 245 | #include "path/to/header.h" |
| 246 | |
| 247 | void foo() { |
| 248 | int a = 0; |
| 249 | })"; |
| 250 | |
| 251 | EXPECT_EQ(PostCode, runCheckOnCode<NonSystemHeaderInserterCheck>( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 252 | PreCode, "clang_tidy/tests/" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 253 | "insert_includes_test_header.cc", |
| 254 | 1)); |
| 255 | } |
| 256 | |
| 257 | TEST(IncludeInserterTest, InsertNonSystemIncludeAfterMainFileInclude) { |
| 258 | const char *PreCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 259 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 260 | |
| 261 | void foo() { |
| 262 | int a = 0; |
| 263 | })"; |
| 264 | const char *PostCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 265 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 266 | |
| 267 | #include "path/to/header.h" |
| 268 | |
| 269 | void foo() { |
| 270 | int a = 0; |
| 271 | })"; |
| 272 | |
| 273 | EXPECT_EQ(PostCode, runCheckOnCode<NonSystemHeaderInserterCheck>( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 274 | PreCode, "clang_tidy/tests/" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 275 | "insert_includes_test_header.cc", |
| 276 | 1)); |
| 277 | } |
| 278 | |
| 279 | TEST(IncludeInserterTest, InsertCXXSystemIncludeAfterLastCXXSystemInclude) { |
| 280 | const char *PreCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 281 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 282 | |
| 283 | #include <list> |
| 284 | #include <map> |
| 285 | |
| 286 | #include "path/to/a/header.h" |
| 287 | |
| 288 | void foo() { |
| 289 | int a = 0; |
| 290 | })"; |
| 291 | const char *PostCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 292 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 293 | |
| 294 | #include <list> |
| 295 | #include <map> |
| 296 | #include <set> |
| 297 | |
| 298 | #include "path/to/a/header.h" |
| 299 | |
| 300 | void foo() { |
| 301 | int a = 0; |
| 302 | })"; |
| 303 | |
| 304 | EXPECT_EQ(PostCode, runCheckOnCode<CXXSystemIncludeInserterCheck>( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 305 | PreCode, "clang_tidy/tests/" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 306 | "insert_includes_test_header.cc", |
| 307 | 1)); |
| 308 | } |
| 309 | |
| 310 | TEST(IncludeInserterTest, InsertCXXSystemIncludeBeforeFirstCXXSystemInclude) { |
| 311 | const char *PreCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 312 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 313 | |
| 314 | #include <vector> |
| 315 | |
| 316 | #include "path/to/a/header.h" |
| 317 | |
| 318 | void foo() { |
| 319 | int a = 0; |
| 320 | })"; |
| 321 | const char *PostCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 322 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 323 | |
| 324 | #include <set> |
| 325 | #include <vector> |
| 326 | |
| 327 | #include "path/to/a/header.h" |
| 328 | |
| 329 | void foo() { |
| 330 | int a = 0; |
| 331 | })"; |
| 332 | |
| 333 | EXPECT_EQ(PostCode, runCheckOnCode<CXXSystemIncludeInserterCheck>( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 334 | PreCode, "clang_tidy/tests/" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 335 | "insert_includes_test_header.cc", |
| 336 | 1)); |
| 337 | } |
| 338 | |
| 339 | TEST(IncludeInserterTest, InsertCXXSystemIncludeBetweenCXXSystemIncludes) { |
| 340 | const char *PreCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 341 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 342 | |
| 343 | #include <map> |
| 344 | #include <vector> |
| 345 | |
| 346 | #include "path/to/a/header.h" |
| 347 | |
| 348 | void foo() { |
| 349 | int a = 0; |
| 350 | })"; |
| 351 | const char *PostCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 352 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 353 | |
| 354 | #include <map> |
| 355 | #include <set> |
| 356 | #include <vector> |
| 357 | |
| 358 | #include "path/to/a/header.h" |
| 359 | |
| 360 | void foo() { |
| 361 | int a = 0; |
| 362 | })"; |
| 363 | |
| 364 | EXPECT_EQ(PostCode, runCheckOnCode<CXXSystemIncludeInserterCheck>( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 365 | PreCode, "clang_tidy/tests/" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 366 | "insert_includes_test_header.cc", |
| 367 | 1)); |
| 368 | } |
| 369 | |
| 370 | TEST(IncludeInserterTest, InsertCXXSystemIncludeAfterMainFileInclude) { |
| 371 | const char *PreCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 372 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 373 | |
| 374 | #include "path/to/a/header.h" |
| 375 | |
| 376 | void foo() { |
| 377 | int a = 0; |
| 378 | })"; |
| 379 | const char *PostCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 380 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 381 | |
| 382 | #include <set> |
| 383 | |
| 384 | #include "path/to/a/header.h" |
| 385 | |
| 386 | void foo() { |
| 387 | int a = 0; |
| 388 | })"; |
| 389 | |
| 390 | EXPECT_EQ(PostCode, runCheckOnCode<CXXSystemIncludeInserterCheck>( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 391 | PreCode, "clang_tidy/tests/" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 392 | "insert_includes_test_header.cc", |
| 393 | 1)); |
| 394 | } |
| 395 | |
| 396 | TEST(IncludeInserterTest, InsertCXXSystemIncludeAfterCSystemInclude) { |
| 397 | const char *PreCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 398 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 399 | |
| 400 | #include <stdlib.h> |
| 401 | |
| 402 | #include "path/to/a/header.h" |
| 403 | |
| 404 | void foo() { |
| 405 | int a = 0; |
| 406 | })"; |
| 407 | const char *PostCode = R"( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 408 | #include "clang_tidy/tests/insert_includes_test_header.h" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 409 | |
| 410 | #include <stdlib.h> |
| 411 | |
| 412 | #include <set> |
| 413 | |
| 414 | #include "path/to/a/header.h" |
| 415 | |
| 416 | void foo() { |
| 417 | int a = 0; |
| 418 | })"; |
| 419 | |
| 420 | EXPECT_EQ(PostCode, runCheckOnCode<CXXSystemIncludeInserterCheck>( |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 421 | PreCode, "clang_tidy/tests/" |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 422 | "insert_includes_test_header.cc", |
| 423 | 1)); |
| 424 | } |
| 425 | |
Alexander Kornienko | 8cc024e | 2015-08-14 12:33:25 +0000 | [diff] [blame] | 426 | TEST(IncludeInserterTest, InsertCXXSystemIncludeBeforeNonSystemInclude) { |
| 427 | const char *PreCode = R"( |
| 428 | #include "path/to/a/header.h" |
| 429 | |
| 430 | void foo() { |
| 431 | int a = 0; |
| 432 | })"; |
| 433 | const char *PostCode = R"( |
| 434 | #include <set> |
| 435 | |
| 436 | #include "path/to/a/header.h" |
| 437 | |
| 438 | void foo() { |
| 439 | int a = 0; |
| 440 | })"; |
| 441 | |
| 442 | EXPECT_EQ(PostCode, runCheckOnCode<CXXSystemIncludeInserterCheck>( |
| 443 | PreCode, "devtools/cymbal/clang_tidy/tests/" |
| 444 | "insert_includes_test_header.cc", |
| 445 | 1)); |
| 446 | } |
| 447 | |
| 448 | TEST(IncludeInserterTest, InsertCSystemIncludeBeforeCXXSystemInclude) { |
| 449 | const char *PreCode = R"( |
| 450 | #include <set> |
| 451 | |
| 452 | #include "path/to/a/header.h" |
| 453 | |
| 454 | void foo() { |
| 455 | int a = 0; |
| 456 | })"; |
| 457 | const char *PostCode = R"( |
| 458 | #include <stdlib.h> |
| 459 | |
| 460 | #include <set> |
| 461 | |
| 462 | #include "path/to/a/header.h" |
| 463 | |
| 464 | void foo() { |
| 465 | int a = 0; |
| 466 | })"; |
| 467 | |
| 468 | EXPECT_EQ(PostCode, runCheckOnCode<CSystemIncludeInserterCheck>( |
| 469 | PreCode, "devtools/cymbal/clang_tidy/tests/" |
| 470 | "insert_includes_test_header.cc", |
| 471 | 1)); |
| 472 | } |
| 473 | |
| 474 | TEST(IncludeInserterTest, InsertIncludeIfThereWasNoneBefore) { |
| 475 | const char *PreCode = R"( |
| 476 | void foo() { |
| 477 | int a = 0; |
| 478 | })"; |
| 479 | const char *PostCode = R"(#include <set> |
| 480 | |
| 481 | |
| 482 | void foo() { |
| 483 | int a = 0; |
| 484 | })"; |
| 485 | |
| 486 | EXPECT_EQ(PostCode, runCheckOnCode<CXXSystemIncludeInserterCheck>( |
| 487 | PreCode, "devtools/cymbal/clang_tidy/tests/" |
| 488 | "insert_includes_test_header.cc", |
| 489 | 1)); |
| 490 | } |
| 491 | |
Manuel Klimek | d00d6f1 | 2015-08-11 11:37:48 +0000 | [diff] [blame] | 492 | } // namespace |
| 493 | } // namespace tidy |
| 494 | } // namespace clang |
Manuel Klimek | e8c8d88 | 2015-08-11 14:21:26 +0000 | [diff] [blame] | 495 | |
Alexander Kornienko | 078ab13 | 2015-08-14 13:23:55 +0000 | [diff] [blame] | 496 | #endif |