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