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