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