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;
70struct ReplayPreamblePPCallback :
public PPCallbacks {
71 const SourceManager &SM;
72 explicit ReplayPreamblePPCallback(
const SourceManager &SM) : SM(SM) {}
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,
83 void FileSkipped(
const FileEntryRef &,
const Token &FilenameTok,
84 SrcMgr::CharacteristicKind)
override {
85 SkippedFiles.emplace_back(FilenameTok);
88struct 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));
96llvm::StringLiteral CheckName =
"replay-preamble-check";
97struct ReplayPreambleModule :
public tidy::ClangTidyModule {
99 addCheckFactories(tidy::ClangTidyCheckFactories &CheckFactories)
override {
100 CheckFactories.registerCheck<ReplayPreambleCheck>(CheckName);
103static tidy::ClangTidyModuleRegistry::Add<ReplayPreambleModule>
104 X(
"replay-preamble-module",
"");
107 return arg.beginOffset() == R.Begin && arg.endOffset() == R.End;
110TEST(ReplayPreambleTest, IncludesAndSkippedFiles) {
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"] =
"";
126 TU.ExtraArgs = {
"-isystem.",
"-xobjective-c"};
133 const auto &
AST = TU.build();
134 const auto &SM =
AST.getSourceManager();
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());
145 ASSERT_EQ(SkippedFiles.size(), Includes.size());
146 for (
size_t I = 0; I < Includes.size(); ++I) {
147 const auto &Inc = Includes[I];
149 EXPECT_EQ(Inc.HashOffset, HashLocs[I]);
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));
157 EXPECT_EQ(Inc.FileNameOffset, FileBeginLocs[I]);
158 EXPECT_EQ(Inc.IsAngled, Code[FileBeginLocs[I]] ==
'<');
160 auto FileRange = FileRanges[I];
161 EXPECT_EQ(Inc.FileName,
162 Code.substr(FileRange.Begin, FileRange.End - FileRange.Begin));
164 EXPECT_EQ(SM.getDecomposedLoc(SkippedFiles[I].location()).second,
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);
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