1 //===- unittest/Tooling/HeaderAnalysisTest.cpp ----------------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "clang/Tooling/Inclusions/HeaderAnalysis.h"
10 #include "clang/Lex/Preprocessor.h"
11 #include "clang/Testing/TestAST.h"
12 #include "gmock/gmock.h"
13 #include "gtest/gtest.h"
20 TEST(HeaderAnalysisTest
, IsSelfContained
) {
23 #include "headerguard
.h
"
24 #include "pragmaonce
.h
"
28 #include "unguarded
.h
"
31 Inputs
.ExtraFiles
["headerguard.h"] = R
"cpp(
37 Inputs
.ExtraFiles
["pragmaonce.h"] = R
"cpp(
40 Inputs
.ExtraFiles
["imported.h"] = "";
42 Inputs
.ExtraFiles
["unguarded.h"] = "";
43 Inputs
.ExtraFiles
["bad.h"] = R
"cpp(
47 #error "Only
... can be included directly
"
52 const auto &SM
= AST
.sourceManager();
53 auto &FM
= SM
.getFileManager();
54 auto &HI
= AST
.preprocessor().getHeaderSearchInfo();
56 isSelfContainedHeader(*FM
.getOptionalFileRef("headerguard.h"), SM
, HI
));
58 isSelfContainedHeader(*FM
.getOptionalFileRef("pragmaonce.h"), SM
, HI
));
60 isSelfContainedHeader(*FM
.getOptionalFileRef("imported.h"), SM
, HI
));
61 EXPECT_TRUE(isSelfContainedHeader(
62 *SM
.getFileEntryRefForID(SM
.getMainFileID()), SM
, HI
));
65 isSelfContainedHeader(*FM
.getOptionalFileRef("unguarded.h"), SM
, HI
));
66 EXPECT_FALSE(isSelfContainedHeader(*FM
.getOptionalFileRef("bad.h"), SM
, HI
));
69 TEST(HeaderAnalysisTest
, CodeContainsImports
) {
70 EXPECT_TRUE(codeContainsImports(R
"cpp(
79 EXPECT_TRUE(codeContainsImports(R
"cpp(
89 EXPECT_FALSE(codeContainsImports(R
"cpp(
98 TEST(HeaderAnalysisTest
, ParseIWYUPragma
) {
99 EXPECT_THAT(parseIWYUPragma("// IWYU pragma: keep"), Eq("keep"));
100 EXPECT_THAT(parseIWYUPragma("// IWYU pragma: keep me\netc"),
102 EXPECT_THAT(parseIWYUPragma("/* IWYU pragma: keep */"), Eq("keep"));
103 EXPECT_EQ(parseIWYUPragma("// IWYU pragma: keep"), std::nullopt
)
104 << "Prefix is sensitive to whitespace";
105 EXPECT_EQ(parseIWYUPragma("// IWYU pragma:keep"), std::nullopt
)
106 << "Prefix is sensitive to whitespace";
107 EXPECT_EQ(parseIWYUPragma("/\n* IWYU pragma: keep */"), std::nullopt
)
108 << "Must start with /* or //";
112 } // namespace tooling