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