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"
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) {
58 toSourceCode(SM, FilenameRange.getAsRange()).drop_back().drop_front(),
63 llvm::StringRef IncDirective;
64 size_t FileNameOffset;
65 llvm::StringRef FileName;
68static std::vector<Inclusion> Includes;
69static std::vector<syntax::Token> SkippedFiles;
70static std::vector<std::string> DefinedMacros;
71struct ReplayPreamblePPCallback :
public PPCallbacks {
72 const SourceManager &SM;
73 explicit ReplayPreamblePPCallback(
const SourceManager &SM) : SM(SM) {}
75 void InclusionDirective(SourceLocation HashLoc,
const Token &IncludeTok,
76 StringRef FileName,
bool IsAngled,
77 CharSourceRange FilenameRange, OptionalFileEntryRef,
78 StringRef, StringRef,
const clang::Module *,
bool,
79 SrcMgr::CharacteristicKind)
override {
80 Includes.emplace_back(SM, HashLoc, IncludeTok, FileName, IsAngled,
84 void FileSkipped(
const FileEntryRef &,
const Token &FilenameTok,
85 SrcMgr::CharacteristicKind)
override {
86 SkippedFiles.emplace_back(FilenameTok);
89 void MacroDefined(
const Token &MacroNameTok,
90 const MacroDirective *
MD)
override {
91 DefinedMacros.push_back(MacroNameTok.getIdentifierInfo()->getName().str());
94struct ReplayPreambleCheck :
public tidy::ClangTidyCheck {
95 ReplayPreambleCheck(StringRef Name, tidy::ClangTidyContext *Context)
96 : ClangTidyCheck(Name, Context) {}
97 void registerPPCallbacks(
const SourceManager &SM, Preprocessor *PP,
98 Preprocessor *ModuleExpanderPP)
override {
99 PP->addPPCallbacks(::std::make_unique<ReplayPreamblePPCallback>(SM));
102llvm::StringLiteral CheckName =
"replay-preamble-check";
103struct ReplayPreambleModule :
public tidy::ClangTidyModule {
105 addCheckFactories(tidy::ClangTidyCheckFactories &CheckFactories)
override {
106 CheckFactories.registerCheck<ReplayPreambleCheck>(CheckName);
109static tidy::ClangTidyModuleRegistry::Add<ReplayPreambleModule>
110 X(
"replay-preamble-module",
"");
113 return arg.beginOffset() == R.Begin && arg.endOffset() == R.End;
116TEST(ReplayPreambleTest, MacroDefinitions) {
117 DefinedMacros.clear();
134 EXPECT_THAT(DefinedMacros, testing::Contains(std::string("_TEST_H")));
135 EXPECT_THAT(DefinedMacros, testing::Contains(std::string(
"_TEST_MACRO")));
138TEST(ReplayPreambleTest, MacroDefinitionsPartialPreamble) {
139 DefinedMacros.clear();
158 EXPECT_THAT(DefinedMacros, testing::Contains(std::string(
"_TEST_H")));
159 EXPECT_THAT(DefinedMacros, testing::Contains(std::string(
"_TEST_MACRO")));
162TEST(ReplayPreambleTest, IncludesAndSkippedFiles) {
166 llvm::Annotations Test(R
"cpp(
167 $hash^#$include[[import]] $filebegin^"$filerange[[bar.h]]"
168 $hash^#$include[[include_next]] $filebegin^"$filerange[[baz.h]]"
169 $hash^#$include[[include]] $filebegin^<$filerange[[a.h]]>)cpp");
170 llvm::StringRef Code = Test.code();
171 TU.Code = Code.str();
172 TU.AdditionalFiles["bar.h"] =
"";
173 TU.AdditionalFiles[
"baz.h"] =
"";
174 TU.AdditionalFiles[
"a.h"] =
"";
178 TU.ExtraArgs = {
"-isystem.",
"-xobjective-c"};
185 const auto &
AST = TU.build();
186 const auto &SM =
AST.getSourceManager();
188 auto HashLocs = Test.points(
"hash");
189 ASSERT_EQ(HashLocs.size(), Includes.size());
190 auto IncludeRanges = Test.ranges(
"include");
191 ASSERT_EQ(IncludeRanges.size(), Includes.size());
192 auto FileBeginLocs = Test.points(
"filebegin");
193 ASSERT_EQ(FileBeginLocs.size(), Includes.size());
194 auto FileRanges = Test.ranges(
"filerange");
195 ASSERT_EQ(FileRanges.size(), Includes.size());
197 ASSERT_EQ(SkippedFiles.size(), Includes.size());
198 for (
size_t I = 0; I < Includes.size(); ++I) {
199 const auto &Inc = Includes[I];
201 EXPECT_EQ(Inc.HashOffset, HashLocs[I]);
203 auto IncRange = IncludeRanges[I];
204 EXPECT_THAT(Inc.IncTok.range(SM), rangeIs(IncRange));
205 EXPECT_EQ(Inc.IncTok.kind(), tok::identifier);
206 EXPECT_EQ(Inc.IncDirective,
207 Code.substr(IncRange.Begin, IncRange.End - IncRange.Begin));
209 EXPECT_EQ(Inc.FileNameOffset, FileBeginLocs[I]);
210 EXPECT_EQ(Inc.IsAngled, Code[FileBeginLocs[I]] ==
'<');
212 auto FileRange = FileRanges[I];
213 EXPECT_EQ(Inc.FileName,
214 Code.substr(FileRange.Begin, FileRange.End - FileRange.Begin));
216 EXPECT_EQ(SM.getDecomposedLoc(SkippedFiles[I].location()).second,
221 SkippedFiles[I].text(SM),
222 Code.substr(FileRange.Begin - 1, FileRange.End - FileRange.Begin + 2));
223 EXPECT_EQ(SkippedFiles[I].kind(), tok::header_name);
static GeneratorRegistry::Add< MDGenerator > MD(MDGenerator::Format, "Generator for MD output.")
WithContextValue extends Context::current() with a single value.
FIXME: Skip testing on windows temporarily due to the different escaping code mode.
static URISchemeRegistry::Add< TestScheme > X(TestScheme::Scheme, "Test schema")
TidyProvider addTidyChecks(llvm::StringRef Checks, llvm::StringRef WarningsAsErrors)
Provider the enables a specific set of checks and warnings as errors.
TEST(BackgroundQueueTest, Priority)
llvm::StringRef toSourceCode(const SourceManager &SM, SourceRange R)
Returns the source code covered by the source range.
===– Representation.cpp - ClangDoc Representation --------—*- C++ -*-===//
Settings that express user/project preferences and control clangd behavior.
static clangd::Key< Config > Key
Context key which can be used to set the current Config.
FastCheckPolicy FastCheckFilter
struct clang::clangd::Config::@343034053122374337352226322054223376344037116252 Diagnostics
Controls warnings and errors when parsing code.
struct clang::clangd::Config::@343034053122374337352226322054223376344037116252::@107156241027253143221327255130274177352007274355 ClangTidy
Configures what clang-tidy checks to run and options to use with them.
TidyProvider ClangTidyProvider