| 1 |
//===- unittest/Tooling/HeaderAnalysisTest.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 "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" |
| 14 |
|
| 15 |
namespace clang { |
| 16 |
namespace tooling { |
| 17 |
namespace { |
| 18 |
using testing::Eq; |
| 19 |
|
| 20 |
TEST(HeaderAnalysisTest, IsSelfContained) { |
| 21 |
TestInputs Inputs; |
| 22 |
Inputs.Code = R"cpp( |
| 23 |
#include "headerguard.h" |
| 24 |
#include "pragmaonce.h" |
| 25 |
#import "imported.h" |
| 26 |
|
| 27 |
#include "bad.h" |
| 28 |
#include "unguarded.h" |
| 29 |
)cpp"; |
| 30 |
|
| 31 |
Inputs.ExtraFiles["headerguard.h"] = R"cpp( |
| 32 |
#ifndef HEADER_H |
| 33 |
#define HEADER_H |
| 34 |
|
| 35 |
#endif HEADER_H |
| 36 |
)cpp"; |
| 37 |
Inputs.ExtraFiles["pragmaonce.h"] = R"cpp( |
| 38 |
#pragma once |
| 39 |
)cpp"; |
| 40 |
Inputs.ExtraFiles["imported.h"] = ""; |
| 41 |
|
| 42 |
Inputs.ExtraFiles["unguarded.h"] = ""; |
| 43 |
Inputs.ExtraFiles["bad.h"] = R"cpp( |
| 44 |
#pragma once |
| 45 |
|
| 46 |
#if defined(INSIDE_H) |
| 47 |
#error "Only ... can be included directly" |
| 48 |
#endif |
| 49 |
)cpp"; |
| 50 |
|
| 51 |
TestAST AST(Inputs); |
| 52 |
const auto &SM = AST.sourceManager(); |
| 53 |
auto &FM = SM.getFileManager(); |
| 54 |
auto &HI = AST.preprocessor().getHeaderSearchInfo(); |
| 55 |
EXPECT_TRUE(isSelfContainedHeader(FM.getFile("headerguard.h").get(), SM, HI)); |
| 56 |
EXPECT_TRUE(isSelfContainedHeader(FM.getFile("pragmaonce.h").get(), SM, HI)); |
| 57 |
EXPECT_TRUE(isSelfContainedHeader(FM.getFile("imported.h").get(), SM, HI)); |
| 58 |
EXPECT_TRUE( |
| 59 |
isSelfContainedHeader(SM.getFileEntryForID(SM.getMainFileID()), SM, HI)); |
| 60 |
|
| 61 |
EXPECT_FALSE(isSelfContainedHeader(FM.getFile("unguarded.h").get(), SM, HI)); |
| 62 |
EXPECT_FALSE(isSelfContainedHeader(FM.getFile("bad.h").get(), SM, HI)); |
| 63 |
} |
| 64 |
|
| 65 |
TEST(HeaderAnalysisTest, CodeContainsImports) { |
| 66 |
EXPECT_TRUE(codeContainsImports(R"cpp( |
| 67 |
#include "foo.h" |
| 68 |
#import "NSFoo.h" |
| 69 |
|
| 70 |
int main() { |
| 71 |
foo(); |
| 72 |
} |
| 73 |
)cpp")); |
| 74 |
|
| 75 |
EXPECT_TRUE(codeContainsImports(R"cpp( |
| 76 |
#include "foo.h" |
| 77 |
|
| 78 |
int main() { |
| 79 |
foo(); |
| 80 |
} |
| 81 |
|
| 82 |
#import "NSFoo.h" |
| 83 |
)cpp")); |
| 84 |
|
| 85 |
EXPECT_FALSE(codeContainsImports(R"cpp( |
| 86 |
#include "foo.h" |
| 87 |
|
| 88 |
int main() { |
| 89 |
foo(); |
| 90 |
} |
| 91 |
)cpp")); |
| 92 |
} |
| 93 |
|
| 94 |
TEST(HeaderAnalysisTest, ParseIWYUPragma) { |
| 95 |
EXPECT_THAT(parseIWYUPragma("// IWYU pragma: keep"), Eq("keep")); |
| 96 |
EXPECT_THAT(parseIWYUPragma("// IWYU pragma: keep me\netc"), |
| 97 |
Eq("keep me")); |
| 98 |
EXPECT_THAT(parseIWYUPragma("/* IWYU pragma: keep */"), Eq("keep")); |
| 99 |
EXPECT_EQ(parseIWYUPragma("// IWYU pragma: keep"), std::nullopt) |
| 100 |
<< "Prefix is sensitive to whitespace"; |
| 101 |
EXPECT_EQ(parseIWYUPragma("// IWYU pragma:keep"), std::nullopt) |
| 102 |
<< "Prefix is sensitive to whitespace"; |
| 103 |
EXPECT_EQ(parseIWYUPragma("/\n* IWYU pragma: keep */"), std::nullopt) |
| 104 |
<< "Must start with /* or //"; |
| 105 |
} |
| 106 |
|
| 107 |
} // namespace |
| 108 |
} // namespace tooling |
| 109 |
} // namespace clang |