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