llvm-project/clang-tools-extra/clangd/unittests/TestTU.h

86 lines
2.8 KiB
C
Raw Normal View History

//===--- TestTU.h - Scratch source files for testing -------------*- C++-*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// Many tests for indexing, code completion etc are most naturally expressed
// using code examples.
// TestTU lets test define these examples in a common way without dealing with
// the mechanics of VFS and compiler interactions, and then easily grab the
// AST, particular symbols, etc.
//
//===---------------------------------------------------------------------===//
#ifndef LLVM_CLANG_TOOLS_EXTRA_UNITTESTS_CLANGD_TESTTU_H
#define LLVM_CLANG_TOOLS_EXTRA_UNITTESTS_CLANGD_TESTTU_H
#include "ClangdUnit.h"
#include "Path.h"
#include "index/Index.h"
#include "llvm/ADT/StringMap.h"
#include "gtest/gtest.h"
#include <string>
#include <utility>
#include <vector>
namespace clang {
namespace clangd {
struct TestTU {
static TestTU withCode(llvm::StringRef Code) {
TestTU TU;
TU.Code = Code;
return TU;
}
static TestTU withHeaderCode(llvm::StringRef HeaderCode) {
TestTU TU;
TU.HeaderCode = HeaderCode;
return TU;
}
// The code to be compiled.
std::string Code;
std::string Filename = "TestTU.cpp";
// Define contents of a header which will be implicitly included by Code.
std::string HeaderCode;
std::string HeaderFilename = "TestTU.h";
// Name and contents of each file.
llvm::StringMap<std::string> AdditionalFiles;
// Extra arguments for the compiler invocation.
std::vector<const char *> ExtraArgs;
llvm::Optional<std::string> ClangTidyChecks;
llvm::Optional<std::string> ClangTidyWarningsAsErrors;
// Index to use when building AST.
const SymbolIndex *ExternalIndex = nullptr;
[clangd] Include insertion: require header guards, drop other heuristics, treat .def like .inc. Summary: We do have some reports of include insertion behaving badly in some codebases. Requiring header guards both makes sense in principle, and is likely to disable this "nice-to-have" feature in codebases where headers don't follow the expected pattern. With this we can drop some other heuristics, such as looking at file extensions to detect known non-headers - implementation files have no guards. One wrinkle here is #import - objc headers may not have guards because they're intended to be used via #import. If the header is the main file or is #included, we won't collect locations - merge should take care of this if we see the file #imported somewhere. Seems likely to be OK. Headers which have a canonicalization (stdlib, IWYU) are exempt from this check. *.inc files continue to be handled by looking up to the including file. This patch also adds *.def here - tablegen wants this pattern too. In terms of code structure, the division between SymbolCollector and CanonicalIncludes has shifted: SymbolCollector is responsible for more. This is because SymbolCollector has all the SourceManager/HeaderSearch access needed for checking for guards, and we interleave these checks with the *.def checks in a loop (potentially). We could hand all the info into CanonicalIncludes and put the logic there if that's preferable. Reviewers: ioeric Subscribers: ilya-biryukov, MaskRay, jkorous, arphaman, kadircet, cfe-commits Tags: #clang Differential Revision: https://reviews.llvm.org/D60316 llvm-svn: 358571
2019-04-17 18:36:02 +08:00
// Simulate a header guard of the header (using an #import directive).
bool ImplicitHeaderGuard = true;
ParsedAST build() const;
SymbolSlab headerSymbols() const;
std::unique_ptr<SymbolIndex> index() const;
};
// Look up an index symbol by qualified name, which must be unique.
const Symbol &findSymbol(const SymbolSlab &, llvm::StringRef QName);
// Look up an AST symbol by qualified name, which must be unique and top-level.
const NamedDecl &findDecl(ParsedAST &AST, llvm::StringRef QName);
// Look up an AST symbol that satisfies \p Filter.
const NamedDecl &findDecl(ParsedAST &AST,
std::function<bool(const NamedDecl &)> Filter);
// Look up an AST symbol by unqualified name, which must be unique.
const NamedDecl &findUnqualifiedDecl(ParsedAST &AST, llvm::StringRef Name);
} // namespace clangd
} // namespace clang
#endif // LLVM_CLANG_TOOLS_EXTRA_UNITTESTS_CLANGD_TESTTU_H