| 1 | //===--- FindHeadersTest.cpp ----------------------------------------------===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "AnalysisInternal.h" |
| 10 | #include "TypesInternal.h" |
| 11 | #include "clang-include-cleaner/Record.h" |
| 12 | #include "clang-include-cleaner/Types.h" |
| 13 | #include "clang/AST/RecursiveASTVisitor.h" |
| 14 | #include "clang/Basic/FileEntry.h" |
| 15 | #include "clang/Basic/FileManager.h" |
| 16 | #include "clang/Frontend/FrontendActions.h" |
| 17 | #include "clang/Testing/TestAST.h" |
| 18 | #include "clang/Tooling/Inclusions/StandardLibrary.h" |
| 19 | #include "llvm/ADT/SmallVector.h" |
| 20 | #include "gmock/gmock.h" |
| 21 | #include "gtest/gtest.h" |
| 22 | #include <memory> |
| 23 | |
| 24 | namespace clang::include_cleaner { |
| 25 | namespace { |
| 26 | using testing::ElementsAre; |
| 27 | using testing::UnorderedElementsAre; |
| 28 | |
| 29 | std::string guard(llvm::StringRef Code) { |
| 30 | return "#pragma once\n" + Code.str(); |
| 31 | } |
| 32 | |
| 33 | class FindHeadersTest : public testing::Test { |
| 34 | protected: |
| 35 | TestInputs Inputs; |
| 36 | PragmaIncludes PI; |
| 37 | std::unique_ptr<TestAST> AST; |
| 38 | FindHeadersTest() { |
| 39 | Inputs.MakeAction = [this] { |
| 40 | struct Hook : public SyntaxOnlyAction { |
| 41 | public: |
| 42 | Hook(PragmaIncludes *Out) : Out(Out) {} |
| 43 | bool BeginSourceFileAction(clang::CompilerInstance &CI) override { |
| 44 | Out->record(CI); |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | PragmaIncludes *Out; |
| 49 | }; |
| 50 | return std::make_unique<Hook>(&PI); |
| 51 | }; |
| 52 | } |
| 53 | void buildAST() { AST = std::make_unique<TestAST>(Inputs); } |
| 54 | |
| 55 | llvm::SmallVector<Hinted<Header>> findHeaders(llvm::StringRef FileName) { |
| 56 | return include_cleaner::findHeaders( |
| 57 | AST->sourceManager().translateFileLineCol( |
| 58 | AST->fileManager().getFile(FileName).get(), |
| 59 | /*Line=*/1, /*Col=*/1), |
| 60 | AST->sourceManager(), &PI); |
| 61 | } |
| 62 | const FileEntry *physicalHeader(llvm::StringRef FileName) { |
| 63 | return AST->fileManager().getFile(FileName).get(); |
| 64 | }; |
| 65 | }; |
| 66 | |
| 67 | TEST_F(FindHeadersTest, IWYUPrivateToPublic) { |
| 68 | Inputs.Code = R"cpp( |
| 69 | #include "private.h" |
| 70 | )cpp"; |
| 71 | Inputs.ExtraFiles["private.h"] = guard(R"cpp( |
| 72 | // IWYU pragma: private, include "path/public.h" |
| 73 | )cpp"); |
| 74 | buildAST(); |
| 75 | EXPECT_THAT(findHeaders("private.h"), |
| 76 | UnorderedElementsAre(physicalHeader("private.h"), |
| 77 | Header("\"path/public.h\""))); |
| 78 | } |
| 79 | |
| 80 | TEST_F(FindHeadersTest, IWYUExport) { |
| 81 | Inputs.Code = R"cpp( |
| 82 | #include "exporter.h" |
| 83 | )cpp"; |
| 84 | Inputs.ExtraFiles["exporter.h"] = guard(R"cpp( |
| 85 | #include "exported1.h" // IWYU pragma: export |
| 86 | |
| 87 | // IWYU pragma: begin_exports |
| 88 | #include "exported2.h" |
| 89 | // IWYU pragma: end_exports |
| 90 | |
| 91 | #include "normal.h" |
| 92 | )cpp"); |
| 93 | Inputs.ExtraFiles["exported1.h"] = guard(""); |
| 94 | Inputs.ExtraFiles["exported2.h"] = guard(""); |
| 95 | Inputs.ExtraFiles["normal.h"] = guard(""); |
| 96 | |
| 97 | buildAST(); |
| 98 | EXPECT_THAT(findHeaders("exported1.h"), |
| 99 | UnorderedElementsAre(physicalHeader("exported1.h"), |
| 100 | physicalHeader("exporter.h"))); |
| 101 | EXPECT_THAT(findHeaders("exported2.h"), |
| 102 | UnorderedElementsAre(physicalHeader("exported2.h"), |
| 103 | physicalHeader("exporter.h"))); |
| 104 | EXPECT_THAT(findHeaders("normal.h"), |
| 105 | UnorderedElementsAre(physicalHeader("normal.h"))); |
| 106 | EXPECT_THAT(findHeaders("exporter.h"), |
| 107 | UnorderedElementsAre(physicalHeader("exporter.h"))); |
| 108 | } |
| 109 | |
| 110 | TEST_F(FindHeadersTest, IWYUExportForStandardHeaders) { |
| 111 | Inputs.Code = R"cpp( |
| 112 | #include "exporter.h" |
| 113 | )cpp"; |
| 114 | Inputs.ExtraFiles["exporter.h"] = guard(R"cpp( |
| 115 | #include <string> // IWYU pragma: export |
| 116 | )cpp"); |
| 117 | Inputs.ExtraFiles["string"] = guard(""); |
| 118 | Inputs.ExtraArgs.push_back("-isystem."); |
| 119 | buildAST(); |
| 120 | tooling::stdlib::Symbol StdString = |
| 121 | *tooling::stdlib::Symbol::named("std::", "string"); |
| 122 | EXPECT_THAT( |
| 123 | include_cleaner::findHeaders(StdString, AST->sourceManager(), &PI), |
| 124 | UnorderedElementsAre(physicalHeader("exporter.h"), StdString.header())); |
| 125 | } |
| 126 | |
| 127 | TEST_F(FindHeadersTest, SelfContained) { |
| 128 | Inputs.Code = R"cpp( |
| 129 | #include "header.h" |
| 130 | )cpp"; |
| 131 | Inputs.ExtraFiles["header.h"] = guard(R"cpp( |
| 132 | #include "fragment.inc" |
| 133 | )cpp"); |
| 134 | Inputs.ExtraFiles["fragment.inc"] = ""; |
| 135 | buildAST(); |
| 136 | EXPECT_THAT(findHeaders("fragment.inc"), |
| 137 | UnorderedElementsAre(physicalHeader("fragment.inc"), |
| 138 | physicalHeader("header.h"))); |
| 139 | } |
| 140 | |
| 141 | TEST_F(FindHeadersTest, NonSelfContainedTraversePrivate) { |
| 142 | Inputs.Code = R"cpp( |
| 143 | #include "header.h" |
| 144 | )cpp"; |
| 145 | Inputs.ExtraFiles["header.h"] = guard(R"cpp( |
| 146 | #include "fragment.inc" |
| 147 | )cpp"); |
| 148 | Inputs.ExtraFiles["fragment.inc"] = R"cpp( |
| 149 | // IWYU pragma: private, include "public.h" |
| 150 | )cpp"; |
| 151 | |
| 152 | buildAST(); |
| 153 | // There is a IWYU private mapping in the non self-contained header, verify |
| 154 | // that we don't emit its includer. |
| 155 | EXPECT_THAT(findHeaders("fragment.inc"), |
| 156 | UnorderedElementsAre(physicalHeader("fragment.inc"), |
| 157 | Header("\"public.h\""))); |
| 158 | } |
| 159 | |
| 160 | TEST_F(FindHeadersTest, NonSelfContainedTraverseExporter) { |
| 161 | Inputs.Code = R"cpp( |
| 162 | #include "exporter.h" |
| 163 | )cpp"; |
| 164 | Inputs.ExtraFiles["exporter.h"] = guard(R"cpp( |
| 165 | #include "exported.h" // IWYU pragma: export |
| 166 | )cpp"); |
| 167 | Inputs.ExtraFiles["exported.h"] = guard(R"cpp( |
| 168 | #include "fragment.inc" |
| 169 | )cpp"); |
| 170 | Inputs.ExtraFiles["fragment.inc"] = ""; |
| 171 | buildAST(); |
| 172 | // Verify that we emit exporters for each header on the path. |
| 173 | EXPECT_THAT(findHeaders("fragment.inc"), |
| 174 | UnorderedElementsAre(physicalHeader("fragment.inc"), |
| 175 | physicalHeader("exported.h"), |
| 176 | physicalHeader("exporter.h"))); |
| 177 | } |
| 178 | |
| 179 | TEST_F(FindHeadersTest, TargetIsExpandedFromMacroInHeader) { |
| 180 | struct CustomVisitor : RecursiveASTVisitor<CustomVisitor> { |
| 181 | const Decl *Out = nullptr; |
| 182 | bool VisitNamedDecl(const NamedDecl *ND) { |
| 183 | if (ND->getName() == "FLAG_foo" || ND->getName() == "Foo") { |
| 184 | EXPECT_TRUE(Out == nullptr); |
| 185 | Out = ND; |
| 186 | } |
| 187 | return true; |
| 188 | } |
| 189 | }; |
| 190 | |
| 191 | struct { |
| 192 | llvm::StringRef MacroHeader; |
| 193 | llvm::StringRef DeclareHeader; |
| 194 | } TestCases[] = { |
| 195 | {/*MacroHeader=*/R"cpp( |
| 196 | #define DEFINE_CLASS(name) class name {}; |
| 197 | )cpp", |
| 198 | /*DeclareHeader=*/R"cpp( |
| 199 | #include "macro.h" |
| 200 | DEFINE_CLASS(Foo) |
| 201 | )cpp"}, |
| 202 | {/*MacroHeader=*/R"cpp( |
| 203 | #define DEFINE_Foo class Foo {}; |
| 204 | )cpp", |
| 205 | /*DeclareHeader=*/R"cpp( |
| 206 | #include "macro.h" |
| 207 | DEFINE_Foo |
| 208 | )cpp"}, |
| 209 | {/*MacroHeader=*/R"cpp( |
| 210 | #define DECLARE_FLAGS(name) extern int FLAG_##name |
| 211 | )cpp", |
| 212 | /*DeclareHeader=*/R"cpp( |
| 213 | #include "macro.h" |
| 214 | DECLARE_FLAGS(foo); |
| 215 | )cpp"}, |
| 216 | }; |
| 217 | |
| 218 | for (const auto &T : TestCases) { |
| 219 | Inputs.Code = R"cpp(#include "declare.h")cpp"; |
| 220 | Inputs.ExtraFiles["declare.h"] = guard(T.DeclareHeader); |
| 221 | Inputs.ExtraFiles["macro.h"] = guard(T.MacroHeader); |
| 222 | buildAST(); |
| 223 | |
| 224 | CustomVisitor Visitor; |
| 225 | Visitor.TraverseDecl(AST->context().getTranslationUnitDecl()); |
| 226 | |
| 227 | auto Headers = clang::include_cleaner::findHeaders( |
| 228 | Visitor.Out->getLocation(), AST->sourceManager(), |
| 229 | /*PragmaIncludes=*/nullptr); |
| 230 | EXPECT_THAT(Headers, UnorderedElementsAre(physicalHeader("declare.h"))); |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | MATCHER_P2(HintedHeader, Header, Hint, "") { |
| 235 | return std::tie(arg.Hint, arg) == std::tie(Hint, Header); |
| 236 | } |
| 237 | |
| 238 | TEST_F(FindHeadersTest, PublicHeaderHint) { |
| 239 | Inputs.Code = R"cpp( |
| 240 | #include "public.h" |
| 241 | )cpp"; |
| 242 | Inputs.ExtraFiles["public.h"] = guard(R"cpp( |
| 243 | #include "private.h" |
| 244 | #include "private.inc" |
| 245 | )cpp"); |
| 246 | Inputs.ExtraFiles["private.h"] = guard(R"cpp( |
| 247 | // IWYU pragma: private |
| 248 | )cpp"); |
| 249 | Inputs.ExtraFiles["private.inc"] = ""; |
| 250 | buildAST(); |
| 251 | // Non self-contained files and headers marked with IWYU private pragma |
| 252 | // shouldn't have PublicHeader hint. |
| 253 | EXPECT_THAT( |
| 254 | findHeaders("private.inc"), |
| 255 | UnorderedElementsAre( |
| 256 | HintedHeader(physicalHeader("private.inc"), Hints::None), |
| 257 | HintedHeader(physicalHeader("public.h"), Hints::PublicHeader))); |
| 258 | EXPECT_THAT(findHeaders("private.h"), |
| 259 | UnorderedElementsAre( |
| 260 | HintedHeader(physicalHeader("private.h"), Hints::None))); |
| 261 | } |
| 262 | |
| 263 | TEST_F(FindHeadersTest, PreferredHeaderHint) { |
| 264 | Inputs.Code = R"cpp( |
| 265 | #include "private.h" |
| 266 | )cpp"; |
| 267 | Inputs.ExtraFiles["private.h"] = guard(R"cpp( |
| 268 | // IWYU pragma: private, include "public.h" |
| 269 | )cpp"); |
| 270 | buildAST(); |
| 271 | // Headers explicitly marked should've preferred signal. |
| 272 | EXPECT_THAT(findHeaders("private.h"), |
| 273 | UnorderedElementsAre( |
| 274 | HintedHeader(physicalHeader("private.h"), Hints::None), |
| 275 | HintedHeader(Header("\"public.h\""), |
| 276 | Hints::PreferredHeader | Hints::PublicHeader))); |
| 277 | } |
| 278 | |
| 279 | class HeadersForSymbolTest : public FindHeadersTest { |
| 280 | protected: |
| 281 | llvm::SmallVector<Header> headersForFoo() { |
| 282 | struct Visitor : public RecursiveASTVisitor<Visitor> { |
| 283 | const NamedDecl *Out = nullptr; |
| 284 | bool VisitNamedDecl(const NamedDecl *ND) { |
| 285 | if (ND->getName() == "foo") { |
| 286 | EXPECT_TRUE(Out == nullptr || Out == ND->getCanonicalDecl()) |
| 287 | << "Found multiple matches for foo."; |
| 288 | Out = cast<NamedDecl>(ND->getCanonicalDecl()); |
| 289 | } |
| 290 | return true; |
| 291 | } |
| 292 | }; |
| 293 | Visitor V; |
| 294 | V.TraverseDecl(AST->context().getTranslationUnitDecl()); |
| 295 | if (!V.Out) |
| 296 | ADD_FAILURE() << "Couldn't find any decls named foo."; |
| 297 | assert(V.Out); |
| 298 | return headersForSymbol(*V.Out, AST->sourceManager(), &PI); |
| 299 | } |
| 300 | }; |
| 301 | |
| 302 | TEST_F(HeadersForSymbolTest, Deduplicates) { |
| 303 | Inputs.Code = R"cpp( |
| 304 | #include "foo.h" |
| 305 | )cpp"; |
| 306 | Inputs.ExtraFiles["foo.h"] = guard(R"cpp( |
| 307 | // IWYU pragma: private, include "foo.h" |
| 308 | void foo(); |
| 309 | void foo(); |
| 310 | )cpp"); |
| 311 | buildAST(); |
| 312 | EXPECT_THAT( |
| 313 | headersForFoo(), |
| 314 | UnorderedElementsAre(physicalHeader("foo.h"), |
| 315 | // FIXME: de-duplicate across different kinds. |
| 316 | Header("\"foo.h\""))); |
| 317 | } |
| 318 | |
| 319 | TEST_F(HeadersForSymbolTest, RankByName) { |
| 320 | Inputs.Code = R"cpp( |
| 321 | #include "fox.h" |
| 322 | #include "bar.h" |
| 323 | )cpp"; |
| 324 | Inputs.ExtraFiles["fox.h"] = guard(R"cpp( |
| 325 | void foo(); |
| 326 | )cpp"); |
| 327 | Inputs.ExtraFiles["bar.h"] = guard(R"cpp( |
| 328 | void foo(); |
| 329 | )cpp"); |
| 330 | buildAST(); |
| 331 | EXPECT_THAT(headersForFoo(), |
| 332 | ElementsAre(physicalHeader("bar.h"), physicalHeader("fox.h"))); |
| 333 | } |
| 334 | |
| 335 | TEST_F(HeadersForSymbolTest, Ranking) { |
| 336 | // Sorting is done over (canonical, public, complete) triplet. |
| 337 | Inputs.Code = R"cpp( |
| 338 | #include "private.h" |
| 339 | #include "public.h" |
| 340 | #include "public_complete.h" |
| 341 | )cpp"; |
| 342 | Inputs.ExtraFiles["public.h"] = guard(R"cpp( |
| 343 | struct foo; |
| 344 | )cpp"); |
| 345 | Inputs.ExtraFiles["private.h"] = guard(R"cpp( |
| 346 | // IWYU pragma: private, include "canonical.h" |
| 347 | struct foo; |
| 348 | )cpp"); |
| 349 | Inputs.ExtraFiles["public_complete.h"] = guard("struct foo {};"); |
| 350 | buildAST(); |
| 351 | EXPECT_THAT(headersForFoo(), ElementsAre(Header("\"canonical.h\""), |
| 352 | physicalHeader("public_complete.h"), |
| 353 | physicalHeader("public.h"), |
| 354 | physicalHeader("private.h"))); |
| 355 | } |
| 356 | |
| 357 | TEST_F(HeadersForSymbolTest, PreferPublicOverComplete) { |
| 358 | Inputs.Code = R"cpp( |
| 359 | #include "complete_private.h" |
| 360 | #include "public.h" |
| 361 | )cpp"; |
| 362 | Inputs.ExtraFiles["complete_private.h"] = guard(R"cpp( |
| 363 | // IWYU pragma: private |
| 364 | struct foo {}; |
| 365 | )cpp"); |
| 366 | Inputs.ExtraFiles["public.h"] = guard("struct foo;"); |
| 367 | buildAST(); |
| 368 | EXPECT_THAT(headersForFoo(), |
| 369 | ElementsAre(physicalHeader("public.h"), |
| 370 | physicalHeader("complete_private.h"))); |
| 371 | } |
| 372 | |
| 373 | TEST_F(HeadersForSymbolTest, PreferNameMatch) { |
| 374 | Inputs.Code = R"cpp( |
| 375 | #include "public_complete.h" |
| 376 | #include "test/foo.fwd.h" |
| 377 | )cpp"; |
| 378 | Inputs.ExtraFiles["public_complete.h"] = guard(R"cpp( |
| 379 | struct foo {}; |
| 380 | )cpp"); |
| 381 | Inputs.ExtraFiles["test/foo.fwd.h"] = guard("struct foo;"); |
| 382 | buildAST(); |
| 383 | EXPECT_THAT(headersForFoo(), |
| 384 | ElementsAre(physicalHeader("test/foo.fwd.h"), |
| 385 | physicalHeader("public_complete.h"))); |
| 386 | } |
| 387 | |
| 388 | TEST_F(HeadersForSymbolTest, MainFile) { |
| 389 | Inputs.Code = R"cpp( |
| 390 | #include "public_complete.h" |
| 391 | struct foo; |
| 392 | )cpp"; |
| 393 | Inputs.ExtraFiles["public_complete.h"] = guard(R"cpp( |
| 394 | struct foo {}; |
| 395 | )cpp"); |
| 396 | buildAST(); |
| 397 | auto &SM = AST->sourceManager(); |
| 398 | // FIXME: Symbols provided by main file should be treated specially. |
| 399 | EXPECT_THAT(headersForFoo(), |
| 400 | ElementsAre(physicalHeader("public_complete.h"), |
| 401 | Header(SM.getFileEntryForID(SM.getMainFileID())))); |
| 402 | } |
| 403 | |
| 404 | TEST_F(HeadersForSymbolTest, PreferExporterOfPrivate) { |
| 405 | Inputs.Code = R"cpp( |
| 406 | #include "private.h" |
| 407 | #include "exporter.h" |
| 408 | )cpp"; |
| 409 | Inputs.ExtraFiles["private.h"] = guard(R"cpp( |
| 410 | // IWYU pragma: private |
| 411 | struct foo {}; |
| 412 | )cpp"); |
| 413 | Inputs.ExtraFiles["exporter.h"] = guard(R"cpp( |
| 414 | #include "private.h" // IWYU pragma: export |
| 415 | )cpp"); |
| 416 | buildAST(); |
| 417 | EXPECT_THAT(headersForFoo(), ElementsAre(physicalHeader("exporter.h"), |
| 418 | physicalHeader("private.h"))); |
| 419 | } |
| 420 | |
| 421 | TEST_F(HeadersForSymbolTest, PreferPublicOverNameMatchOnPrivate) { |
| 422 | Inputs.Code = R"cpp( |
| 423 | #include "foo.h" |
| 424 | )cpp"; |
| 425 | Inputs.ExtraFiles["foo.h"] = guard(R"cpp( |
| 426 | // IWYU pragma: private, include "public.h" |
| 427 | struct foo {}; |
| 428 | )cpp"); |
| 429 | buildAST(); |
| 430 | EXPECT_THAT(headersForFoo(), ElementsAre(Header(StringRef("\"public.h\"")), |
| 431 | physicalHeader("foo.h"))); |
| 432 | } |
| 433 | } // namespace |
| 434 | } // namespace clang::include_cleaner |