Debian Codegraph @

clang-tools-extra/clangd/unittests/ReplayPeambleTests.cpp

raw
1 //===-- ReplayPreambleTests.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 // These tests cover clangd's logic to replay PP events from preamble to
10 // clang-tidy checks.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "../../clang-tidy/ClangTidyCheck.h"
15 #include "../../clang-tidy/ClangTidyModule.h"
16 #include "AST.h"
17 #include "Config.h"
18 #include "Diagnostics.h"
19 #include "ParsedAST.h"
20 #include "SourceCode.h"
21 #include "TestTU.h"
22 #include "TidyProvider.h"
23 #include "support/Context.h"
24 #include "clang/AST/DeclTemplate.h"
25 #include "clang/Basic/FileEntry.h"
26 #include "clang/Basic/LLVM.h"
27 #include "clang/Basic/SourceLocation.h"
28 #include "clang/Basic/SourceManager.h"
29 #include "clang/Basic/TokenKinds.h"
30 #include "clang/Lex/PPCallbacks.h"
31 #include "clang/Lex/Token.h"
32 #include "clang/Tooling/Syntax/Tokens.h"
33 #include "llvm/ADT/StringRef.h"
34 #include "llvm/Support/Registry.h"
35 #include "llvm/Testing/Annotations/Annotations.h"
36 #include "gmock/gmock-matchers.h"
37 #include "gmock/gmock.h"
38 #include "gtest/gtest.h"
39 #include <cstddef>
40 #include <memory>
41 #include <vector>
42
43 namespace clang {
44
45 class Module;
46
47 namespace clangd {
48 namespace {
49 struct Inclusion {
50 Inclusion(const SourceManager &SM, SourceLocation HashLoc,
51 const Token &IncludeTok, llvm::StringRef FileName, bool IsAngled,
52 CharSourceRange FilenameRange)
53 : HashOffset(SM.getDecomposedLoc(HashLoc).second), IncTok(IncludeTok),
54 IncDirective(IncludeTok.getIdentifierInfo()->getName()),
55 FileNameOffset(SM.getDecomposedLoc(FilenameRange.getBegin()).second),
56 FileName(FileName), IsAngled(IsAngled) {
57 EXPECT_EQ(
58 toSourceCode(SM, FilenameRange.getAsRange()).drop_back().drop_front(),
59 FileName);
60 }
61 size_t HashOffset;
62 syntax::Token IncTok;
63 llvm::StringRef IncDirective;
64 size_t FileNameOffset;
65 llvm::StringRef FileName;
66 bool IsAngled;
67 };
68 static std::vector<Inclusion> Includes;
69 static std::vector<syntax::Token> SkippedFiles;
70 struct ReplayPreamblePPCallback : public PPCallbacks {
71 const SourceManager &SM;
72 explicit ReplayPreamblePPCallback(const SourceManager &SM) : SM(SM) {}
73
74 void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok,
75 StringRef FileName, bool IsAngled,
76 CharSourceRange FilenameRange, OptionalFileEntryRef,
77 StringRef, StringRef, const clang::Module *, bool,
78 SrcMgr::CharacteristicKind) override {
79 Includes.emplace_back(SM, HashLoc, IncludeTok, FileName, IsAngled,
80 FilenameRange);
81 }
82
83 void FileSkipped(const FileEntryRef &, const Token &FilenameTok,
84 SrcMgr::CharacteristicKind) override {
85 SkippedFiles.emplace_back(FilenameTok);
86 }
87 };
88 struct ReplayPreambleCheck : public tidy::ClangTidyCheck {
89 ReplayPreambleCheck(StringRef Name, tidy::ClangTidyContext *Context)
90 : ClangTidyCheck(Name, Context) {}
91 void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP,
92 Preprocessor *ModuleExpanderPP) override {
93 PP->addPPCallbacks(::std::make_unique<ReplayPreamblePPCallback>(SM));
94 }
95 };
96 llvm::StringLiteral CheckName = "replay-preamble-check";
97 struct ReplayPreambleModule : public tidy::ClangTidyModule {
98 void
99 addCheckFactories(tidy::ClangTidyCheckFactories &CheckFactories) override {
100 CheckFactories.registerCheck<ReplayPreambleCheck>(CheckName);
101 }
102 };
103 static tidy::ClangTidyModuleRegistry::Add<ReplayPreambleModule>
104 X("replay-preamble-module", "");
105
106 MATCHER_P(rangeIs, R, "") {
107 return arg.beginOffset() == R.Begin && arg.endOffset() == R.End;
108 }
109
110 TEST(ReplayPreambleTest, IncludesAndSkippedFiles) {
111 TestTU TU;
112 // This check records inclusion directives replayed by clangd.
113 TU.ClangTidyProvider = addTidyChecks(CheckName);
114 llvm::Annotations Test(R"cpp(
115 $hash^#$include[[import]] $filebegin^"$filerange[[bar.h]]"
116 $hash^#$include[[include_next]] $filebegin^"$filerange[[baz.h]]"
117 $hash^#$include[[include]] $filebegin^<$filerange[[a.h]]>)cpp");
118 llvm::StringRef Code = Test.code();
119 TU.Code = Code.str();
120 TU.AdditionalFiles["bar.h"] = "";
121 TU.AdditionalFiles["baz.h"] = "";
122 TU.AdditionalFiles["a.h"] = "";
123 // Since we are also testing #import directives, and they don't make much
124 // sense in c++ (also they actually break on windows), just set language to
125 // obj-c.
126 TU.ExtraArgs = {"-isystem.", "-xobjective-c"};
127
128 // Allow the check to run even though not marked as fast.
129 Config Cfg;
130 Cfg.Diagnostics.ClangTidy.FastCheckFilter = Config::FastCheckPolicy::Loose;
131 WithContextValue WithCfg(Config::Key, std::move(Cfg));
132
133 const auto &AST = TU.build();
134 const auto &SM = AST.getSourceManager();
135
136 auto HashLocs = Test.points("hash");
137 ASSERT_EQ(HashLocs.size(), Includes.size());
138 auto IncludeRanges = Test.ranges("include");
139 ASSERT_EQ(IncludeRanges.size(), Includes.size());
140 auto FileBeginLocs = Test.points("filebegin");
141 ASSERT_EQ(FileBeginLocs.size(), Includes.size());
142 auto FileRanges = Test.ranges("filerange");
143 ASSERT_EQ(FileRanges.size(), Includes.size());
144
145 ASSERT_EQ(SkippedFiles.size(), Includes.size());
146 for (size_t I = 0; I < Includes.size(); ++I) {
147 const auto &Inc = Includes[I];
148
149 EXPECT_EQ(Inc.HashOffset, HashLocs[I]);
150
151 auto IncRange = IncludeRanges[I];
152 EXPECT_THAT(Inc.IncTok.range(SM), rangeIs(IncRange));
153 EXPECT_EQ(Inc.IncTok.kind(), tok::identifier);
154 EXPECT_EQ(Inc.IncDirective,
155 Code.substr(IncRange.Begin, IncRange.End - IncRange.Begin));
156
157 EXPECT_EQ(Inc.FileNameOffset, FileBeginLocs[I]);
158 EXPECT_EQ(Inc.IsAngled, Code[FileBeginLocs[I]] == '<');
159
160 auto FileRange = FileRanges[I];
161 EXPECT_EQ(Inc.FileName,
162 Code.substr(FileRange.Begin, FileRange.End - FileRange.Begin));
163
164 EXPECT_EQ(SM.getDecomposedLoc(SkippedFiles[I].location()).second,
165 Inc.FileNameOffset);
166 // This also contains quotes/angles so increment the range by one from both
167 // sides.
168 EXPECT_EQ(
169 SkippedFiles[I].text(SM),
170 Code.substr(FileRange.Begin - 1, FileRange.End - FileRange.Begin + 2));
171 EXPECT_EQ(SkippedFiles[I].kind(), tok::header_name);
172 }
173 }
174 } // namespace
175 } // namespace clangd
176 } // namespace clang