| 1 |
//===-- ParsedASTTests.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 build a TU, which generally uses the APIs |
| 10 |
// in ParsedAST and Preamble, via the TestTU helper. |
| 11 |
// |
| 12 |
//===----------------------------------------------------------------------===// |
| 13 |
|
| 14 |
#include "../../clang-tidy/ClangTidyCheck.h" |
| 15 |
#include "../../clang-tidy/ClangTidyModule.h" |
| 16 |
#include "../../clang-tidy/ClangTidyModuleRegistry.h" |
| 17 |
#include "AST.h" |
| 18 |
#include "Annotations.h" |
| 19 |
#include "Compiler.h" |
| 20 |
#include "Diagnostics.h" |
| 21 |
#include "Headers.h" |
| 22 |
#include "ParsedAST.h" |
| 23 |
#include "Preamble.h" |
| 24 |
#include "SourceCode.h" |
| 25 |
#include "TestFS.h" |
| 26 |
#include "TestTU.h" |
| 27 |
#include "TidyProvider.h" |
| 28 |
#include "clang/AST/DeclTemplate.h" |
| 29 |
#include "clang/Basic/SourceLocation.h" |
| 30 |
#include "clang/Basic/SourceManager.h" |
| 31 |
#include "clang/Basic/TokenKinds.h" |
| 32 |
#include "clang/Lex/PPCallbacks.h" |
| 33 |
#include "clang/Lex/Token.h" |
| 34 |
#include "clang/Tooling/Syntax/Tokens.h" |
| 35 |
#include "llvm/ADT/StringRef.h" |
| 36 |
#include "llvm/Testing/Support/Error.h" |
| 37 |
#include "gmock/gmock-matchers.h" |
| 38 |
#include "gmock/gmock.h" |
| 39 |
#include "gtest/gtest.h" |
| 40 |
|
| 41 |
namespace clang { |
| 42 |
namespace clangd { |
| 43 |
namespace { |
| 44 |
|
| 45 |
using ::testing::AllOf; |
| 46 |
using ::testing::Contains; |
| 47 |
using ::testing::ElementsAre; |
| 48 |
using ::testing::ElementsAreArray; |
| 49 |
using ::testing::IsEmpty; |
| 50 |
using ::testing::UnorderedElementsAreArray; |
| 51 |
|
| 52 |
MATCHER_P(declNamed, Name, "") { |
| 53 |
if (NamedDecl *ND = dyn_cast<NamedDecl>(arg)) |
| 54 |
if (ND->getName() == Name) |
| 55 |
return true; |
| 56 |
if (auto *Stream = result_listener->stream()) { |
| 57 |
llvm::raw_os_ostream OS(*Stream); |
| 58 |
arg->dump(OS); |
| 59 |
} |
| 60 |
return false; |
| 61 |
} |
| 62 |
|
| 63 |
MATCHER_P(declKind, Kind, "") { |
| 64 |
if (NamedDecl *ND = dyn_cast<NamedDecl>(arg)) |
| 65 |
if (ND->getDeclKindName() == llvm::StringRef(Kind)) |
| 66 |
return true; |
| 67 |
if (auto *Stream = result_listener->stream()) { |
| 68 |
llvm::raw_os_ostream OS(*Stream); |
| 69 |
arg->dump(OS); |
| 70 |
} |
| 71 |
return false; |
| 72 |
} |
| 73 |
|
| 74 |
// Matches if the Decl has template args equal to ArgName. If the decl is a |
| 75 |
// NamedDecl and ArgName is an empty string it also matches. |
| 76 |
MATCHER_P(withTemplateArgs, ArgName, "") { |
| 77 |
if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(arg)) { |
| 78 |
if (const auto *Args = FD->getTemplateSpecializationArgs()) { |
| 79 |
std::string SpecializationArgs; |
| 80 |
// Without the PrintingPolicy "bool" will be printed as "_Bool". |
| 81 |
LangOptions LO; |
| 82 |
PrintingPolicy Policy(LO); |
| 83 |
Policy.adjustForCPlusPlus(); |
| 84 |
for (const auto &Arg : Args->asArray()) { |
| 85 |
if (SpecializationArgs.size() > 0) |
| 86 |
SpecializationArgs += ","; |
| 87 |
SpecializationArgs += Arg.getAsType().getAsString(Policy); |
| 88 |
} |
| 89 |
if (Args->size() == 0) |
| 90 |
return ArgName == SpecializationArgs; |
| 91 |
return ArgName == "<" + SpecializationArgs + ">"; |
| 92 |
} |
| 93 |
} |
| 94 |
if (const NamedDecl *ND = dyn_cast<NamedDecl>(arg)) |
| 95 |
return printTemplateSpecializationArgs(*ND) == ArgName; |
| 96 |
return false; |
| 97 |
} |
| 98 |
|
| 99 |
MATCHER_P(rangeIs, R, "") { |
| 100 |
return arg.beginOffset() == R.Begin && arg.endOffset() == R.End; |
| 101 |
} |
| 102 |
|
| 103 |
MATCHER_P(pragmaTrivia, P, "") { return arg.Trivia == P; } |
| 104 |
|
| 105 |
MATCHER(eqInc, "") { |
| 106 |
Inclusion Actual = testing::get<0>(arg); |
| 107 |
Inclusion Expected = testing::get<1>(arg); |
| 108 |
return std::tie(Actual.HashLine, Actual.Written) == |
| 109 |
std::tie(Expected.HashLine, Expected.Written); |
| 110 |
} |
| 111 |
|
| 112 |
TEST(ParsedASTTest, TopLevelDecls) { |
| 113 |
TestTU TU; |
| 114 |
TU.HeaderCode = R"( |
| 115 |
int header1(); |
| 116 |
int header2; |
| 117 |
)"; |
| 118 |
TU.Code = R"cpp( |
| 119 |
int main(); |
| 120 |
template <typename> bool X = true; |
| 121 |
)cpp"; |
| 122 |
auto AST = TU.build(); |
| 123 |
EXPECT_THAT(AST.getLocalTopLevelDecls(), |
| 124 |
testing::UnorderedElementsAreArray( |
| 125 |
{AllOf(declNamed("main"), declKind("Function")), |
| 126 |
AllOf(declNamed("X"), declKind("VarTemplate"))})); |
| 127 |
} |
| 128 |
|
| 129 |
TEST(ParsedASTTest, DoesNotGetIncludedTopDecls) { |
| 130 |
TestTU TU; |
| 131 |
TU.HeaderCode = R"cpp( |
| 132 |
#define LL void foo(){} |
| 133 |
template<class T> |
| 134 |
struct H { |
| 135 |
H() {} |
| 136 |
LL |
| 137 |
}; |
| 138 |
)cpp"; |
| 139 |
TU.Code = R"cpp( |
| 140 |
int main() { |
| 141 |
H<int> h; |
| 142 |
h.foo(); |
| 143 |
} |
| 144 |
)cpp"; |
| 145 |
auto AST = TU.build(); |
| 146 |
EXPECT_THAT(AST.getLocalTopLevelDecls(), ElementsAre(declNamed("main"))); |
| 147 |
} |
| 148 |
|
| 149 |
TEST(ParsedASTTest, DoesNotGetImplicitTemplateTopDecls) { |
| 150 |
TestTU TU; |
| 151 |
TU.Code = R"cpp( |
| 152 |
template<typename T> |
| 153 |
void f(T) {} |
| 154 |
void s() { |
| 155 |
f(10UL); |
| 156 |
} |
| 157 |
)cpp"; |
| 158 |
|
| 159 |
auto AST = TU.build(); |
| 160 |
EXPECT_THAT(AST.getLocalTopLevelDecls(), |
| 161 |
ElementsAre(declNamed("f"), declNamed("s"))); |
| 162 |
} |
| 163 |
|
| 164 |
TEST(ParsedASTTest, |
| 165 |
GetsExplicitInstantiationAndSpecializationTemplateTopDecls) { |
| 166 |
TestTU TU; |
| 167 |
TU.Code = R"cpp( |
| 168 |
template <typename T> |
| 169 |
void f(T) {} |
| 170 |
template<> |
| 171 |
void f(bool); |
| 172 |
template void f(double); |
| 173 |
|
| 174 |
template <class T> |
| 175 |
struct V {}; |
| 176 |
template<class T> |
| 177 |
struct V<T*> {}; |
| 178 |
template <> |
| 179 |
struct V<bool> {}; |
| 180 |
|
| 181 |
template<class T> |
| 182 |
T foo = T(10); |
| 183 |
int i = foo<int>; |
| 184 |
double d = foo<double>; |
| 185 |
|
| 186 |
template <class T> |
| 187 |
int foo<T*> = 0; |
| 188 |
template <> |
| 189 |
int foo<bool> = 0; |
| 190 |
)cpp"; |
| 191 |
|
| 192 |
auto AST = TU.build(); |
| 193 |
EXPECT_THAT( |
| 194 |
AST.getLocalTopLevelDecls(), |
| 195 |
ElementsAreArray({AllOf(declNamed("f"), withTemplateArgs("")), |
| 196 |
AllOf(declNamed("f"), withTemplateArgs("<bool>")), |
| 197 |
AllOf(declNamed("f"), withTemplateArgs("<double>")), |
| 198 |
AllOf(declNamed("V"), withTemplateArgs("")), |
| 199 |
AllOf(declNamed("V"), withTemplateArgs("<T *>")), |
| 200 |
AllOf(declNamed("V"), withTemplateArgs("<bool>")), |
| 201 |
AllOf(declNamed("foo"), withTemplateArgs("")), |
| 202 |
AllOf(declNamed("i"), withTemplateArgs("")), |
| 203 |
AllOf(declNamed("d"), withTemplateArgs("")), |
| 204 |
AllOf(declNamed("foo"), withTemplateArgs("<T *>")), |
| 205 |
AllOf(declNamed("foo"), withTemplateArgs("<bool>"))})); |
| 206 |
} |
| 207 |
|
| 208 |
TEST(ParsedASTTest, IgnoresDelayedTemplateParsing) { |
| 209 |
auto TU = TestTU::withCode(R"cpp( |
| 210 |
template <typename T> void xxx() { |
| 211 |
int yyy = 0; |
| 212 |
} |
| 213 |
)cpp"); |
| 214 |
TU.ExtraArgs.push_back("-fdelayed-template-parsing"); |
| 215 |
auto AST = TU.build(); |
| 216 |
EXPECT_EQ(Decl::Var, findUnqualifiedDecl(AST, "yyy").getKind()); |
| 217 |
} |
| 218 |
|
| 219 |
TEST(ParsedASTTest, TokensAfterPreamble) { |
| 220 |
TestTU TU; |
| 221 |
TU.AdditionalFiles["foo.h"] = R"( |
| 222 |
int foo(); |
| 223 |
)"; |
| 224 |
TU.Code = R"cpp( |
| 225 |
#include "foo.h" |
| 226 |
first_token; |
| 227 |
void test() { |
| 228 |
// error-ok: invalid syntax, just examining token stream |
| 229 |
} |
| 230 |
last_token |
| 231 |
)cpp"; |
| 232 |
auto AST = TU.build(); |
| 233 |
const syntax::TokenBuffer &T = AST.getTokens(); |
| 234 |
const auto &SM = AST.getSourceManager(); |
| 235 |
|
| 236 |
ASSERT_GT(T.expandedTokens().size(), 2u); |
| 237 |
// Check first token after the preamble. |
| 238 |
EXPECT_EQ(T.expandedTokens().front().text(SM), "first_token"); |
| 239 |
// Last token is always 'eof'. |
| 240 |
EXPECT_EQ(T.expandedTokens().back().kind(), tok::eof); |
| 241 |
// Check the token before 'eof'. |
| 242 |
EXPECT_EQ(T.expandedTokens().drop_back().back().text(SM), "last_token"); |
| 243 |
|
| 244 |
// The spelled tokens for the main file should have everything. |
| 245 |
auto Spelled = T.spelledTokens(SM.getMainFileID()); |
| 246 |
ASSERT_FALSE(Spelled.empty()); |
| 247 |
EXPECT_EQ(Spelled.front().kind(), tok::hash); |
| 248 |
EXPECT_EQ(Spelled.back().text(SM), "last_token"); |
| 249 |
} |
| 250 |
|
| 251 |
TEST(ParsedASTTest, NoCrashOnTokensWithTidyCheck) { |
| 252 |
TestTU TU; |
| 253 |
// this check runs the preprocessor, we need to make sure it does not break |
| 254 |
// our recording logic. |
| 255 |
TU.ClangTidyProvider = addTidyChecks("modernize-use-trailing-return-type"); |
| 256 |
TU.Code = "inline int foo() {}"; |
| 257 |
|
| 258 |
auto AST = TU.build(); |
| 259 |
const syntax::TokenBuffer &T = AST.getTokens(); |
| 260 |
const auto &SM = AST.getSourceManager(); |
| 261 |
|
| 262 |
ASSERT_GT(T.expandedTokens().size(), 7u); |
| 263 |
// Check first token after the preamble. |
| 264 |
EXPECT_EQ(T.expandedTokens().front().text(SM), "inline"); |
| 265 |
// Last token is always 'eof'. |
| 266 |
EXPECT_EQ(T.expandedTokens().back().kind(), tok::eof); |
| 267 |
// Check the token before 'eof'. |
| 268 |
EXPECT_EQ(T.expandedTokens().drop_back().back().text(SM), "}"); |
| 269 |
} |
| 270 |
|
| 271 |
TEST(ParsedASTTest, CanBuildInvocationWithUnknownArgs) { |
| 272 |
MockFS FS; |
| 273 |
FS.Files = {{testPath("foo.cpp"), "void test() {}"}}; |
| 274 |
// Unknown flags should not prevent a build of compiler invocation. |
| 275 |
ParseInputs Inputs; |
| 276 |
Inputs.TFS = &FS; |
| 277 |
Inputs.CompileCommand.CommandLine = {"clang", "-fsome-unknown-flag", |
| 278 |
testPath("foo.cpp")}; |
| 279 |
IgnoreDiagnostics IgnoreDiags; |
| 280 |
EXPECT_NE(buildCompilerInvocation(Inputs, IgnoreDiags), nullptr); |
| 281 |
|
| 282 |
// Unknown forwarded to -cc1 should not a failure either. |
| 283 |
Inputs.CompileCommand.CommandLine = { |
| 284 |
"clang", "-Xclang", "-fsome-unknown-flag", testPath("foo.cpp")}; |
| 285 |
EXPECT_NE(buildCompilerInvocation(Inputs, IgnoreDiags), nullptr); |
| 286 |
} |
| 287 |
|
| 288 |
TEST(ParsedASTTest, CollectsMainFileMacroExpansions) { |
| 289 |
Annotations TestCase(R"cpp( |
| 290 |
#define ^MACRO_ARGS(X, Y) X Y |
| 291 |
// - preamble ends |
| 292 |
^ID(int A); |
| 293 |
// Macro arguments included. |
| 294 |
^MACRO_ARGS(^MACRO_ARGS(^MACRO_EXP(int), E), ^ID(= 2)); |
| 295 |
|
| 296 |
// Macro names inside other macros not included. |
| 297 |
#define ^MACRO_ARGS2(X, Y) X Y |
| 298 |
#define ^FOO BAR |
| 299 |
#define ^BAR 1 |
| 300 |
int F = ^FOO; |
| 301 |
|
| 302 |
// Macros from token concatenations not included. |
| 303 |
#define ^CONCAT(X) X##A() |
| 304 |
#define ^PREPEND(X) MACRO##X() |
| 305 |
#define ^MACROA() 123 |
| 306 |
int G = ^CONCAT(MACRO); |
| 307 |
int H = ^PREPEND(A); |
| 308 |
|
| 309 |
// Macros included not from preamble not included. |
| 310 |
#include "foo.inc" |
| 311 |
|
| 312 |
int printf(const char*, ...); |
| 313 |
void exit(int); |
| 314 |
#define ^assert(COND) if (!(COND)) { printf("%s", #COND); exit(0); } |
| 315 |
|
| 316 |
void test() { |
| 317 |
// Includes macro expansions in arguments that are expressions |
| 318 |
^assert(0 <= ^BAR); |
| 319 |
} |
| 320 |
|
| 321 |
#ifdef ^UNDEFINED |
| 322 |
#endif |
| 323 |
|
| 324 |
#define ^MULTIPLE_DEFINITION 1 |
| 325 |
#undef ^MULTIPLE_DEFINITION |
| 326 |
|
| 327 |
#define ^MULTIPLE_DEFINITION 2 |
| 328 |
#undef ^MULTIPLE_DEFINITION |
| 329 |
)cpp"); |
| 330 |
auto TU = TestTU::withCode(TestCase.code()); |
| 331 |
TU.HeaderCode = R"cpp( |
| 332 |
#define ID(X) X |
| 333 |
#define MACRO_EXP(X) ID(X) |
| 334 |
MACRO_EXP(int B); |
| 335 |
)cpp"; |
| 336 |
TU.AdditionalFiles["foo.inc"] = R"cpp( |
| 337 |
int C = ID(1); |
| 338 |
#define DEF 1 |
| 339 |
int D = DEF; |
| 340 |
)cpp"; |
| 341 |
ParsedAST AST = TU.build(); |
| 342 |
std::vector<Position> MacroExpansionPositions; |
| 343 |
for (const auto &SIDToRefs : AST.getMacros().MacroRefs) { |
| 344 |
for (const auto &R : SIDToRefs.second) |
| 345 |
MacroExpansionPositions.push_back(R.Rng.start); |
| 346 |
} |
| 347 |
for (const auto &R : AST.getMacros().UnknownMacros) |
| 348 |
MacroExpansionPositions.push_back(R.Rng.start); |
| 349 |
EXPECT_THAT(MacroExpansionPositions, |
| 350 |
testing::UnorderedElementsAreArray(TestCase.points())); |
| 351 |
} |
| 352 |
|
| 353 |
MATCHER_P(withFileName, Inc, "") { return arg.FileName == Inc; } |
| 354 |
|
| 355 |
TEST(ParsedASTTest, ReplayPreambleForTidyCheckers) { |
| 356 |
struct Inclusion { |
| 357 |
Inclusion(const SourceManager &SM, SourceLocation HashLoc, |
| 358 |
const Token &IncludeTok, llvm::StringRef FileName, bool IsAngled, |
| 359 |
CharSourceRange FilenameRange) |
| 360 |
: HashOffset(SM.getDecomposedLoc(HashLoc).second), IncTok(IncludeTok), |
| 361 |
IncDirective(IncludeTok.getIdentifierInfo()->getName()), |
| 362 |
FileNameOffset(SM.getDecomposedLoc(FilenameRange.getBegin()).second), |
| 363 |
FileName(FileName), IsAngled(IsAngled) { |
| 364 |
EXPECT_EQ( |
| 365 |
toSourceCode(SM, FilenameRange.getAsRange()).drop_back().drop_front(), |
| 366 |
FileName); |
| 367 |
} |
| 368 |
size_t HashOffset; |
| 369 |
syntax::Token IncTok; |
| 370 |
llvm::StringRef IncDirective; |
| 371 |
size_t FileNameOffset; |
| 372 |
llvm::StringRef FileName; |
| 373 |
bool IsAngled; |
| 374 |
}; |
| 375 |
static std::vector<Inclusion> Includes; |
| 376 |
static std::vector<syntax::Token> SkippedFiles; |
| 377 |
struct ReplayPreamblePPCallback : public PPCallbacks { |
| 378 |
const SourceManager &SM; |
| 379 |
explicit ReplayPreamblePPCallback(const SourceManager &SM) : SM(SM) {} |
| 380 |
|
| 381 |
void InclusionDirective(SourceLocation HashLoc, const Token &IncludeTok, |
| 382 |
StringRef FileName, bool IsAngled, |
| 383 |
CharSourceRange FilenameRange, OptionalFileEntryRef, |
| 384 |
StringRef, StringRef, const clang::Module *, |
| 385 |
SrcMgr::CharacteristicKind) override { |
| 386 |
Includes.emplace_back(SM, HashLoc, IncludeTok, FileName, IsAngled, |
| 387 |
FilenameRange); |
| 388 |
} |
| 389 |
|
| 390 |
void FileSkipped(const FileEntryRef &, const Token &FilenameTok, |
| 391 |
SrcMgr::CharacteristicKind) override { |
| 392 |
SkippedFiles.emplace_back(FilenameTok); |
| 393 |
} |
| 394 |
}; |
| 395 |
struct ReplayPreambleCheck : public tidy::ClangTidyCheck { |
| 396 |
ReplayPreambleCheck(StringRef Name, tidy::ClangTidyContext *Context) |
| 397 |
: ClangTidyCheck(Name, Context) {} |
| 398 |
void registerPPCallbacks(const SourceManager &SM, Preprocessor *PP, |
| 399 |
Preprocessor *ModuleExpanderPP) override { |
| 400 |
PP->addPPCallbacks(::std::make_unique<ReplayPreamblePPCallback>(SM)); |
| 401 |
} |
| 402 |
}; |
| 403 |
struct ReplayPreambleModule : public tidy::ClangTidyModule { |
| 404 |
void |
| 405 |
addCheckFactories(tidy::ClangTidyCheckFactories &CheckFactories) override { |
| 406 |
CheckFactories.registerCheck<ReplayPreambleCheck>( |
| 407 |
"replay-preamble-check"); |
| 408 |
} |
| 409 |
}; |
| 410 |
|
| 411 |
static tidy::ClangTidyModuleRegistry::Add<ReplayPreambleModule> X( |
| 412 |
"replay-preamble-module", ""); |
| 413 |
TestTU TU; |
| 414 |
// This check records inclusion directives replayed by clangd. |
| 415 |
TU.ClangTidyProvider = addTidyChecks("replay-preamble-check"); |
| 416 |
llvm::Annotations Test(R"cpp( |
| 417 |
$hash^#$include[[import]] $filebegin^"$filerange[[bar.h]]" |
| 418 |
$hash^#$include[[include_next]] $filebegin^"$filerange[[baz.h]]" |
| 419 |
$hash^#$include[[include]] $filebegin^<$filerange[[a.h]]>)cpp"); |
| 420 |
llvm::StringRef Code = Test.code(); |
| 421 |
TU.Code = Code.str(); |
| 422 |
TU.AdditionalFiles["bar.h"] = ""; |
| 423 |
TU.AdditionalFiles["baz.h"] = ""; |
| 424 |
TU.AdditionalFiles["a.h"] = ""; |
| 425 |
// Since we are also testing #import directives, and they don't make much |
| 426 |
// sense in c++ (also they actually break on windows), just set language to |
| 427 |
// obj-c. |
| 428 |
TU.ExtraArgs = {"-isystem.", "-xobjective-c"}; |
| 429 |
|
| 430 |
const auto &AST = TU.build(); |
| 431 |
const auto &SM = AST.getSourceManager(); |
| 432 |
|
| 433 |
auto HashLocs = Test.points("hash"); |
| 434 |
ASSERT_EQ(HashLocs.size(), Includes.size()); |
| 435 |
auto IncludeRanges = Test.ranges("include"); |
| 436 |
ASSERT_EQ(IncludeRanges.size(), Includes.size()); |
| 437 |
auto FileBeginLocs = Test.points("filebegin"); |
| 438 |
ASSERT_EQ(FileBeginLocs.size(), Includes.size()); |
| 439 |
auto FileRanges = Test.ranges("filerange"); |
| 440 |
ASSERT_EQ(FileRanges.size(), Includes.size()); |
| 441 |
|
| 442 |
ASSERT_EQ(SkippedFiles.size(), Includes.size()); |
| 443 |
for (size_t I = 0; I < Includes.size(); ++I) { |
| 444 |
const auto &Inc = Includes[I]; |
| 445 |
|
| 446 |
EXPECT_EQ(Inc.HashOffset, HashLocs[I]); |
| 447 |
|
| 448 |
auto IncRange = IncludeRanges[I]; |
| 449 |
EXPECT_THAT(Inc.IncTok.range(SM), rangeIs(IncRange)); |
| 450 |
EXPECT_EQ(Inc.IncTok.kind(), tok::identifier); |
| 451 |
EXPECT_EQ(Inc.IncDirective, |
| 452 |
Code.substr(IncRange.Begin, IncRange.End - IncRange.Begin)); |
| 453 |
|
| 454 |
EXPECT_EQ(Inc.FileNameOffset, FileBeginLocs[I]); |
| 455 |
EXPECT_EQ(Inc.IsAngled, Code[FileBeginLocs[I]] == '<'); |
| 456 |
|
| 457 |
auto FileRange = FileRanges[I]; |
| 458 |
EXPECT_EQ(Inc.FileName, |
| 459 |
Code.substr(FileRange.Begin, FileRange.End - FileRange.Begin)); |
| 460 |
|
| 461 |
EXPECT_EQ(SM.getDecomposedLoc(SkippedFiles[I].location()).second, |
| 462 |
Inc.FileNameOffset); |
| 463 |
// This also contains quotes/angles so increment the range by one from both |
| 464 |
// sides. |
| 465 |
EXPECT_EQ( |
| 466 |
SkippedFiles[I].text(SM), |
| 467 |
Code.substr(FileRange.Begin - 1, FileRange.End - FileRange.Begin + 2)); |
| 468 |
EXPECT_EQ(SkippedFiles[I].kind(), tok::header_name); |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
TEST(ParsedASTTest, PatchesAdditionalIncludes) { |
| 473 |
llvm::StringLiteral ModifiedContents = R"cpp( |
| 474 |
#include "baz.h" |
| 475 |
#include "foo.h" |
| 476 |
#include "sub/aux.h" |
| 477 |
void bar() { |
| 478 |
foo(); |
| 479 |
baz(); |
| 480 |
aux(); |
| 481 |
})cpp"; |
| 482 |
// Build expected ast with symbols coming from headers. |
| 483 |
TestTU TU; |
| 484 |
TU.Filename = "foo.cpp"; |
| 485 |
TU.AdditionalFiles["foo.h"] = "void foo();"; |
| 486 |
TU.AdditionalFiles["sub/baz.h"] = "void baz();"; |
| 487 |
TU.AdditionalFiles["sub/aux.h"] = "void aux();"; |
| 488 |
TU.ExtraArgs = {"-I" + testPath("sub")}; |
| 489 |
TU.Code = ModifiedContents.str(); |
| 490 |
auto ExpectedAST = TU.build(); |
| 491 |
|
| 492 |
// Build preamble with no includes. |
| 493 |
TU.Code = ""; |
| 494 |
StoreDiags Diags; |
| 495 |
MockFS FS; |
| 496 |
auto Inputs = TU.inputs(FS); |
| 497 |
auto CI = buildCompilerInvocation(Inputs, Diags); |
| 498 |
auto EmptyPreamble = |
| 499 |
buildPreamble(testPath("foo.cpp"), *CI, Inputs, true, nullptr); |
| 500 |
ASSERT_TRUE(EmptyPreamble); |
| 501 |
EXPECT_THAT(EmptyPreamble->Includes.MainFileIncludes, IsEmpty()); |
| 502 |
|
| 503 |
// Now build an AST using empty preamble and ensure patched includes worked. |
| 504 |
TU.Code = ModifiedContents.str(); |
| 505 |
Inputs = TU.inputs(FS); |
| 506 |
auto PatchedAST = ParsedAST::build(testPath("foo.cpp"), Inputs, std::move(CI), |
| 507 |
{}, EmptyPreamble); |
| 508 |
ASSERT_TRUE(PatchedAST); |
| 509 |
ASSERT_FALSE(PatchedAST->getDiagnostics()); |
| 510 |
|
| 511 |
// Ensure source location information is correct, including resolved paths. |
| 512 |
EXPECT_THAT(PatchedAST->getIncludeStructure().MainFileIncludes, |
| 513 |
testing::Pointwise( |
| 514 |
eqInc(), ExpectedAST.getIncludeStructure().MainFileIncludes)); |
| 515 |
// Ensure file proximity signals are correct. |
| 516 |
auto &SM = PatchedAST->getSourceManager(); |
| 517 |
auto &FM = SM.getFileManager(); |
| 518 |
// Copy so that we can use operator[] to get the children. |
| 519 |
IncludeStructure Includes = PatchedAST->getIncludeStructure(); |
| 520 |
auto MainFE = FM.getFile(testPath("foo.cpp")); |
| 521 |
ASSERT_TRUE(MainFE); |
| 522 |
auto MainID = Includes.getID(*MainFE); |
| 523 |
auto AuxFE = FM.getFile(testPath("sub/aux.h")); |
| 524 |
ASSERT_TRUE(AuxFE); |
| 525 |
auto AuxID = Includes.getID(*AuxFE); |
| 526 |
EXPECT_THAT(Includes.IncludeChildren[*MainID], Contains(*AuxID)); |
| 527 |
} |
| 528 |
|
| 529 |
TEST(ParsedASTTest, PatchesDeletedIncludes) { |
| 530 |
TestTU TU; |
| 531 |
TU.Filename = "foo.cpp"; |
| 532 |
TU.Code = ""; |
| 533 |
auto ExpectedAST = TU.build(); |
| 534 |
|
| 535 |
// Build preamble with no includes. |
| 536 |
TU.Code = R"cpp(#include <foo.h>)cpp"; |
| 537 |
StoreDiags Diags; |
| 538 |
MockFS FS; |
| 539 |
auto Inputs = TU.inputs(FS); |
| 540 |
auto CI = buildCompilerInvocation(Inputs, Diags); |
| 541 |
auto BaselinePreamble = |
| 542 |
buildPreamble(testPath("foo.cpp"), *CI, Inputs, true, nullptr); |
| 543 |
ASSERT_TRUE(BaselinePreamble); |
| 544 |
EXPECT_THAT(BaselinePreamble->Includes.MainFileIncludes, |
| 545 |
ElementsAre(testing::Field(&Inclusion::Written, "<foo.h>"))); |
| 546 |
|
| 547 |
// Now build an AST using additional includes and check that locations are |
| 548 |
// correctly parsed. |
| 549 |
TU.Code = ""; |
| 550 |
Inputs = TU.inputs(FS); |
| 551 |
auto PatchedAST = ParsedAST::build(testPath("foo.cpp"), Inputs, std::move(CI), |
| 552 |
{}, BaselinePreamble); |
| 553 |
ASSERT_TRUE(PatchedAST); |
| 554 |
|
| 555 |
// Ensure source location information is correct. |
| 556 |
EXPECT_THAT(PatchedAST->getIncludeStructure().MainFileIncludes, |
| 557 |
testing::Pointwise( |
| 558 |
eqInc(), ExpectedAST.getIncludeStructure().MainFileIncludes)); |
| 559 |
// Ensure file proximity signals are correct. |
| 560 |
auto &SM = ExpectedAST.getSourceManager(); |
| 561 |
auto &FM = SM.getFileManager(); |
| 562 |
// Copy so that we can getOrCreateID(). |
| 563 |
IncludeStructure Includes = ExpectedAST.getIncludeStructure(); |
| 564 |
auto MainFE = FM.getFileRef(testPath("foo.cpp")); |
| 565 |
ASSERT_THAT_EXPECTED(MainFE, llvm::Succeeded()); |
| 566 |
auto MainID = Includes.getOrCreateID(*MainFE); |
| 567 |
auto &PatchedFM = PatchedAST->getSourceManager().getFileManager(); |
| 568 |
IncludeStructure PatchedIncludes = PatchedAST->getIncludeStructure(); |
| 569 |
auto PatchedMainFE = PatchedFM.getFileRef(testPath("foo.cpp")); |
| 570 |
ASSERT_THAT_EXPECTED(PatchedMainFE, llvm::Succeeded()); |
| 571 |
auto PatchedMainID = PatchedIncludes.getOrCreateID(*PatchedMainFE); |
| 572 |
EXPECT_EQ(Includes.includeDepth(MainID)[MainID], |
| 573 |
PatchedIncludes.includeDepth(PatchedMainID)[PatchedMainID]); |
| 574 |
} |
| 575 |
|
| 576 |
// Returns Code guarded by #ifndef guards |
| 577 |
std::string guard(llvm::StringRef Code) { |
| 578 |
static int GuardID = 0; |
| 579 |
std::string GuardName = ("GUARD_" + llvm::Twine(++GuardID)).str(); |
| 580 |
return llvm::formatv("#ifndef {0}\n#define {0}\n{1}\n#endif\n", GuardName, |
| 581 |
Code); |
| 582 |
} |
| 583 |
|
| 584 |
std::string once(llvm::StringRef Code) { |
| 585 |
return llvm::formatv("#pragma once\n{0}\n", Code); |
| 586 |
} |
| 587 |
|
| 588 |
bool mainIsGuarded(const ParsedAST &AST) { |
| 589 |
const auto &SM = AST.getSourceManager(); |
| 590 |
const FileEntry *MainFE = SM.getFileEntryForID(SM.getMainFileID()); |
| 591 |
return AST.getPreprocessor() |
| 592 |
.getHeaderSearchInfo() |
| 593 |
.isFileMultipleIncludeGuarded(MainFE); |
| 594 |
} |
| 595 |
|
| 596 |
MATCHER_P(diag, Desc, "") { |
| 597 |
return llvm::StringRef(arg.Message).contains(Desc); |
| 598 |
} |
| 599 |
|
| 600 |
// Check our understanding of whether the main file is header guarded or not. |
| 601 |
TEST(ParsedASTTest, HeaderGuards) { |
| 602 |
TestTU TU; |
| 603 |
TU.ImplicitHeaderGuard = false; |
| 604 |
|
| 605 |
TU.Code = ";"; |
| 606 |
EXPECT_FALSE(mainIsGuarded(TU.build())); |
| 607 |
|
| 608 |
TU.Code = guard(";"); |
| 609 |
EXPECT_TRUE(mainIsGuarded(TU.build())); |
| 610 |
|
| 611 |
TU.Code = once(";"); |
| 612 |
EXPECT_TRUE(mainIsGuarded(TU.build())); |
| 613 |
|
| 614 |
TU.Code = R"cpp( |
| 615 |
; |
| 616 |
#pragma once |
| 617 |
)cpp"; |
| 618 |
EXPECT_FALSE(mainIsGuarded(TU.build())); // FIXME: true |
| 619 |
|
| 620 |
TU.Code = R"cpp( |
| 621 |
; |
| 622 |
#ifndef GUARD |
| 623 |
#define GUARD |
| 624 |
; |
| 625 |
#endif |
| 626 |
)cpp"; |
| 627 |
EXPECT_FALSE(mainIsGuarded(TU.build())); |
| 628 |
} |
| 629 |
|
| 630 |
// Check our handling of files that include themselves. |
| 631 |
// Ideally we allow this if the file has header guards. |
| 632 |
// |
| 633 |
// Note: the semicolons (empty statements) are significant! |
| 634 |
// - they force the preamble to end and the body to begin. Directives can have |
| 635 |
// different effects in the preamble vs main file (which we try to hide). |
| 636 |
// - if the preamble would otherwise cover the whole file, a trailing semicolon |
| 637 |
// forces their sizes to be different. This is significant because the file |
| 638 |
// size is part of the lookup key for HeaderFileInfo, and we don't want to |
| 639 |
// rely on the preamble's HFI being looked up when parsing the main file. |
| 640 |
TEST(ParsedASTTest, HeaderGuardsSelfInclude) { |
| 641 |
TestTU TU; |
| 642 |
TU.ImplicitHeaderGuard = false; |
| 643 |
TU.Filename = "self.h"; |
| 644 |
|
| 645 |
TU.Code = R"cpp( |
| 646 |
#include "self.h" // error-ok |
| 647 |
; |
| 648 |
)cpp"; |
| 649 |
auto AST = TU.build(); |
| 650 |
EXPECT_THAT(*AST.getDiagnostics(), |
| 651 |
ElementsAre(diag("recursively when building a preamble"))); |
| 652 |
EXPECT_FALSE(mainIsGuarded(AST)); |
| 653 |
|
| 654 |
TU.Code = R"cpp( |
| 655 |
; |
| 656 |
#include "self.h" // error-ok |
| 657 |
)cpp"; |
| 658 |
AST = TU.build(); |
| 659 |
EXPECT_THAT(*AST.getDiagnostics(), ElementsAre(diag("nested too deeply"))); |
| 660 |
EXPECT_FALSE(mainIsGuarded(AST)); |
| 661 |
|
| 662 |
TU.Code = R"cpp( |
| 663 |
#pragma once |
| 664 |
#include "self.h" |
| 665 |
; |
| 666 |
)cpp"; |
| 667 |
AST = TU.build(); |
| 668 |
EXPECT_THAT(*AST.getDiagnostics(), IsEmpty()); |
| 669 |
EXPECT_TRUE(mainIsGuarded(AST)); |
| 670 |
|
| 671 |
TU.Code = R"cpp( |
| 672 |
#pragma once |
| 673 |
; |
| 674 |
#include "self.h" |
| 675 |
)cpp"; |
| 676 |
AST = TU.build(); |
| 677 |
EXPECT_THAT(*AST.getDiagnostics(), IsEmpty()); |
| 678 |
EXPECT_TRUE(mainIsGuarded(AST)); |
| 679 |
|
| 680 |
TU.Code = R"cpp( |
| 681 |
; |
| 682 |
#pragma once |
| 683 |
#include "self.h" |
| 684 |
)cpp"; |
| 685 |
AST = TU.build(); |
| 686 |
EXPECT_THAT(*AST.getDiagnostics(), IsEmpty()); |
| 687 |
EXPECT_TRUE(mainIsGuarded(AST)); |
| 688 |
|
| 689 |
TU.Code = R"cpp( |
| 690 |
#ifndef GUARD |
| 691 |
#define GUARD |
| 692 |
#include "self.h" // error-ok: FIXME, this would be nice to support |
| 693 |
#endif |
| 694 |
; |
| 695 |
)cpp"; |
| 696 |
AST = TU.build(); |
| 697 |
EXPECT_THAT(*AST.getDiagnostics(), |
| 698 |
ElementsAre(diag("recursively when building a preamble"))); |
| 699 |
EXPECT_TRUE(mainIsGuarded(AST)); |
| 700 |
|
| 701 |
TU.Code = R"cpp( |
| 702 |
#ifndef GUARD |
| 703 |
#define GUARD |
| 704 |
; |
| 705 |
#include "self.h" |
| 706 |
#endif |
| 707 |
)cpp"; |
| 708 |
AST = TU.build(); |
| 709 |
EXPECT_THAT(*AST.getDiagnostics(), IsEmpty()); |
| 710 |
EXPECT_TRUE(mainIsGuarded(AST)); |
| 711 |
|
| 712 |
// Guarded too late... |
| 713 |
TU.Code = R"cpp( |
| 714 |
#include "self.h" // error-ok |
| 715 |
#ifndef GUARD |
| 716 |
#define GUARD |
| 717 |
; |
| 718 |
#endif |
| 719 |
)cpp"; |
| 720 |
AST = TU.build(); |
| 721 |
EXPECT_THAT(*AST.getDiagnostics(), |
| 722 |
ElementsAre(diag("recursively when building a preamble"))); |
| 723 |
EXPECT_FALSE(mainIsGuarded(AST)); |
| 724 |
|
| 725 |
TU.Code = R"cpp( |
| 726 |
#include "self.h" // error-ok |
| 727 |
; |
| 728 |
#ifndef GUARD |
| 729 |
#define GUARD |
| 730 |
#endif |
| 731 |
)cpp"; |
| 732 |
AST = TU.build(); |
| 733 |
EXPECT_THAT(*AST.getDiagnostics(), |
| 734 |
ElementsAre(diag("recursively when building a preamble"))); |
| 735 |
EXPECT_FALSE(mainIsGuarded(AST)); |
| 736 |
|
| 737 |
TU.Code = R"cpp( |
| 738 |
; |
| 739 |
#ifndef GUARD |
| 740 |
#define GUARD |
| 741 |
#include "self.h" |
| 742 |
#endif |
| 743 |
)cpp"; |
| 744 |
AST = TU.build(); |
| 745 |
EXPECT_THAT(*AST.getDiagnostics(), IsEmpty()); |
| 746 |
EXPECT_FALSE(mainIsGuarded(AST)); |
| 747 |
|
| 748 |
TU.Code = R"cpp( |
| 749 |
#include "self.h" // error-ok |
| 750 |
#pragma once |
| 751 |
; |
| 752 |
)cpp"; |
| 753 |
AST = TU.build(); |
| 754 |
EXPECT_THAT(*AST.getDiagnostics(), |
| 755 |
ElementsAre(diag("recursively when building a preamble"))); |
| 756 |
EXPECT_TRUE(mainIsGuarded(AST)); |
| 757 |
|
| 758 |
TU.Code = R"cpp( |
| 759 |
#include "self.h" // error-ok |
| 760 |
; |
| 761 |
#pragma once |
| 762 |
)cpp"; |
| 763 |
AST = TU.build(); |
| 764 |
EXPECT_THAT(*AST.getDiagnostics(), |
| 765 |
ElementsAre(diag("recursively when building a preamble"))); |
| 766 |
EXPECT_TRUE(mainIsGuarded(AST)); |
| 767 |
} |
| 768 |
|
| 769 |
// Tests how we handle common idioms for splitting a header-only library |
| 770 |
// into interface and implementation files (e.g. *.h vs *.inl). |
| 771 |
// These files mutually include each other, and need careful handling of include |
| 772 |
// guards (which interact with preambles). |
| 773 |
TEST(ParsedASTTest, HeaderGuardsImplIface) { |
| 774 |
std::string Interface = R"cpp( |
| 775 |
// error-ok: we assert on diagnostics explicitly |
| 776 |
template <class T> struct Traits { |
| 777 |
unsigned size(); |
| 778 |
}; |
| 779 |
#include "impl.h" |
| 780 |
)cpp"; |
| 781 |
std::string Implementation = R"cpp( |
| 782 |
// error-ok: we assert on diagnostics explicitly |
| 783 |
#include "iface.h" |
| 784 |
template <class T> unsigned Traits<T>::size() { |
| 785 |
return sizeof(T); |
| 786 |
} |
| 787 |
)cpp"; |
| 788 |
|
| 789 |
TestTU TU; |
| 790 |
TU.ImplicitHeaderGuard = false; // We're testing include guard handling! |
| 791 |
TU.ExtraArgs.push_back("-xc++-header"); |
| 792 |
|
| 793 |
// Editing the interface file, which is include guarded (easy case). |
| 794 |
// We mostly get this right via PP if we don't recognize the include guard. |
| 795 |
TU.Filename = "iface.h"; |
| 796 |
TU.Code = guard(Interface); |
| 797 |
TU.AdditionalFiles = {{"impl.h", Implementation}}; |
| 798 |
auto AST = TU.build(); |
| 799 |
EXPECT_THAT(*AST.getDiagnostics(), IsEmpty()); |
| 800 |
EXPECT_TRUE(mainIsGuarded(AST)); |
| 801 |
// Slightly harder: the `#pragma once` is part of the preamble, and we |
| 802 |
// need to transfer it to the main file's HeaderFileInfo. |
| 803 |
TU.Code = once(Interface); |
| 804 |
AST = TU.build(); |
| 805 |
EXPECT_THAT(*AST.getDiagnostics(), IsEmpty()); |
| 806 |
EXPECT_TRUE(mainIsGuarded(AST)); |
| 807 |
|
| 808 |
// Editing the implementation file, which is not include guarded. |
| 809 |
TU.Filename = "impl.h"; |
| 810 |
TU.Code = Implementation; |
| 811 |
TU.AdditionalFiles = {{"iface.h", guard(Interface)}}; |
| 812 |
AST = TU.build(); |
| 813 |
// The diagnostic is unfortunate in this case, but correct per our model. |
| 814 |
// Ultimately the include is skipped and the code is parsed correctly though. |
| 815 |
EXPECT_THAT(*AST.getDiagnostics(), |
| 816 |
ElementsAre(diag("in included file: main file cannot be included " |
| 817 |
"recursively when building a preamble"))); |
| 818 |
EXPECT_FALSE(mainIsGuarded(AST)); |
| 819 |
// Interface is pragma once guarded, same thing. |
| 820 |
TU.AdditionalFiles = {{"iface.h", once(Interface)}}; |
| 821 |
AST = TU.build(); |
| 822 |
EXPECT_THAT(*AST.getDiagnostics(), |
| 823 |
ElementsAre(diag("in included file: main file cannot be included " |
| 824 |
"recursively when building a preamble"))); |
| 825 |
EXPECT_FALSE(mainIsGuarded(AST)); |
| 826 |
} |
| 827 |
|
| 828 |
TEST(ParsedASTTest, DiscoversPragmaMarks) { |
| 829 |
TestTU TU; |
| 830 |
TU.AdditionalFiles["Header.h"] = R"( |
| 831 |
#pragma mark - Something API |
| 832 |
int something(); |
| 833 |
#pragma mark Something else |
| 834 |
)"; |
| 835 |
TU.Code = R"cpp( |
| 836 |
#include "Header.h" |
| 837 |
#pragma mark In Preamble |
| 838 |
#pragma mark - Something Impl |
| 839 |
int something() { return 1; } |
| 840 |
#pragma mark End |
| 841 |
)cpp"; |
| 842 |
auto AST = TU.build(); |
| 843 |
|
| 844 |
EXPECT_THAT(AST.getMarks(), ElementsAre(pragmaTrivia(" In Preamble"), |
| 845 |
pragmaTrivia(" - Something Impl"), |
| 846 |
pragmaTrivia(" End"))); |
| 847 |
} |
| 848 |
|
| 849 |
} // namespace |
| 850 |
} // namespace clangd |
| 851 |
} // namespace clang |