2019-09-04 17:46:06 +08:00
|
|
|
//===--- ParsedAST.h - Building translation units ----------------*- C++-*-===//
|
2017-05-16 17:38:59 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// 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
|
2017-05-16 17:38:59 +08:00
|
|
|
//
|
2018-08-15 00:03:32 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-09-04 17:46:06 +08:00
|
|
|
//
|
|
|
|
// This file exposes building a file as if it were open in clangd, and defines
|
|
|
|
// the ParsedAST structure that holds the results.
|
|
|
|
//
|
|
|
|
// This is similar to a clang -fsyntax-only run that produces a clang AST, but
|
|
|
|
// we have several customizations:
|
|
|
|
// - preamble handling
|
|
|
|
// - capturing diagnostics for later access
|
2021-07-19 21:14:23 +08:00
|
|
|
// - running clang-tidy checks
|
2019-09-04 17:46:06 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2019-09-04 17:46:06 +08:00
|
|
|
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_PARSEDAST_H
|
|
|
|
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_PARSEDAST_H
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2019-09-24 19:14:06 +08:00
|
|
|
#include "CollectMacros.h"
|
2019-01-22 17:58:53 +08:00
|
|
|
#include "Compiler.h"
|
2018-03-12 23:28:22 +08:00
|
|
|
#include "Diagnostics.h"
|
2018-05-14 20:19:16 +08:00
|
|
|
#include "Headers.h"
|
2019-09-04 15:35:00 +08:00
|
|
|
#include "Preamble.h"
|
2019-02-05 00:19:57 +08:00
|
|
|
#include "index/CanonicalIncludes.h"
|
[clangd] Move non-clang base pieces into separate support/ lib. NFCI
Summary:
This enforces layering, reduces a sprawling clangd/ directory, and makes life
easier for embedders.
Reviewers: kbobyrev
Subscribers: mgorny, ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, jfb, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D79014
2020-04-28 23:49:17 +08:00
|
|
|
#include "support/Path.h"
|
2017-09-30 00:41:23 +08:00
|
|
|
#include "clang/Frontend/FrontendAction.h"
|
2017-07-21 21:29:29 +08:00
|
|
|
#include "clang/Frontend/PrecompiledPreamble.h"
|
2018-05-24 23:50:15 +08:00
|
|
|
#include "clang/Lex/Preprocessor.h"
|
2017-07-21 21:29:29 +08:00
|
|
|
#include "clang/Tooling/CompilationDatabase.h"
|
2019-06-19 22:03:19 +08:00
|
|
|
#include "clang/Tooling/Syntax/Tokens.h"
|
[clangd] Surface errors from command-line parsing
Summary:
Those errors are exposed at the first character of a file,
for a lack of a better place.
Previously, all errors were stored inside the AST and report
accordingly. However, errors in command-line argument parsing could
result in failure to produce the AST, so we need an alternative ways to
report those errors.
We take the following approach in this patch:
- buildCompilerInvocation() now requires an explicit DiagnosticConsumer.
- TUScheduler and TestTU now collect the diagnostics produced when
parsing command line arguments.
If pasing of the AST failed, diagnostics are reported via a new
ParsingCallbacks::onFailedAST method.
If parsing of the AST succeeded, any errors produced during
command-line parsing are stored alongside the AST inside the
ParsedAST instance and reported as previously by calling the
ParsingCallbacks::onMainAST method;
- The client code that uses ClangdServer's DiagnosticConsumer
does not need to change, it will receive new diagnostics in the
onDiagnosticsReady() callback
Errors produced when parsing command-line arguments are collected using
the same StoreDiags class that is used to collect all other errors. They
are recognized by their location being invalid. IIUC, the location is
invalid as there is no source manager at this point, it is created at a
later stage.
Although technically we might also get diagnostics that mention the
command-line arguments FileID with after the source manager was created
(and they have valid source locations), we choose to not handle those
and they are dropped as not coming from the main file. AFAICT, those
diagnostics should always be notes, therefore it's safe to drop them
without loosing too much information.
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, javed.absar, MaskRay, jkorous, arphaman, cfe-commits, gribozavr
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66759
llvm-svn: 370177
2019-08-28 17:24:55 +08:00
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
2020-04-08 03:18:00 +08:00
|
|
|
#include "llvm/ADT/None.h"
|
|
|
|
#include "llvm/ADT/Optional.h"
|
|
|
|
#include "llvm/ADT/StringRef.h"
|
2017-05-16 17:38:59 +08:00
|
|
|
#include <memory>
|
2018-02-09 18:17:23 +08:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2017-05-16 17:38:59 +08:00
|
|
|
|
2018-10-10 21:27:25 +08:00
|
|
|
namespace clang {
|
2017-05-16 17:38:59 +08:00
|
|
|
namespace clangd {
|
2021-04-26 08:01:32 +08:00
|
|
|
class HeuristicResolver;
|
2019-09-04 15:35:00 +08:00
|
|
|
class SymbolIndex;
|
2017-11-24 21:04:21 +08:00
|
|
|
|
2017-08-01 23:51:38 +08:00
|
|
|
/// Stores and provides access to parsed AST.
|
|
|
|
class ParsedAST {
|
2017-05-16 17:38:59 +08:00
|
|
|
public:
|
2020-04-27 06:13:56 +08:00
|
|
|
/// Attempts to run Clang and store the parsed AST.
|
|
|
|
/// If \p Preamble is non-null it is reused during parsing.
|
|
|
|
/// This function does not check if preamble is valid to reuse.
|
2017-08-01 23:51:38 +08:00
|
|
|
static llvm::Optional<ParsedAST>
|
2020-04-27 06:13:56 +08:00
|
|
|
build(llvm::StringRef Filename, const ParseInputs &Inputs,
|
|
|
|
std::unique_ptr<clang::CompilerInvocation> CI,
|
[clangd] Surface errors from command-line parsing
Summary:
Those errors are exposed at the first character of a file,
for a lack of a better place.
Previously, all errors were stored inside the AST and report
accordingly. However, errors in command-line argument parsing could
result in failure to produce the AST, so we need an alternative ways to
report those errors.
We take the following approach in this patch:
- buildCompilerInvocation() now requires an explicit DiagnosticConsumer.
- TUScheduler and TestTU now collect the diagnostics produced when
parsing command line arguments.
If pasing of the AST failed, diagnostics are reported via a new
ParsingCallbacks::onFailedAST method.
If parsing of the AST succeeded, any errors produced during
command-line parsing are stored alongside the AST inside the
ParsedAST instance and reported as previously by calling the
ParsingCallbacks::onMainAST method;
- The client code that uses ClangdServer's DiagnosticConsumer
does not need to change, it will receive new diagnostics in the
onDiagnosticsReady() callback
Errors produced when parsing command-line arguments are collected using
the same StoreDiags class that is used to collect all other errors. They
are recognized by their location being invalid. IIUC, the location is
invalid as there is no source manager at this point, it is created at a
later stage.
Although technically we might also get diagnostics that mention the
command-line arguments FileID with after the source manager was created
(and they have valid source locations), we choose to not handle those
and they are dropped as not coming from the main file. AFAICT, those
diagnostics should always be notes, therefore it's safe to drop them
without loosing too much information.
Reviewers: kadircet
Reviewed By: kadircet
Subscribers: nridge, javed.absar, MaskRay, jkorous, arphaman, cfe-commits, gribozavr
Tags: #clang
Differential Revision: https://reviews.llvm.org/D66759
llvm-svn: 370177
2019-08-28 17:24:55 +08:00
|
|
|
llvm::ArrayRef<Diag> CompilerInvocationDiags,
|
2020-04-27 06:13:56 +08:00
|
|
|
std::shared_ptr<const PreambleData> Preamble);
|
2017-08-01 23:51:38 +08:00
|
|
|
|
|
|
|
ParsedAST(ParsedAST &&Other);
|
|
|
|
ParsedAST &operator=(ParsedAST &&Other);
|
|
|
|
|
|
|
|
~ParsedAST();
|
|
|
|
|
2018-05-28 20:23:17 +08:00
|
|
|
/// Note that the returned ast will not contain decls from the preamble that
|
|
|
|
/// were not deserialized during parsing. Clients should expect only decls
|
|
|
|
/// from the main file to be in the AST.
|
2017-08-01 23:51:38 +08:00
|
|
|
ASTContext &getASTContext();
|
|
|
|
const ASTContext &getASTContext() const;
|
|
|
|
|
|
|
|
Preprocessor &getPreprocessor();
|
2018-01-10 01:32:00 +08:00
|
|
|
std::shared_ptr<Preprocessor> getPreprocessorPtr();
|
2017-08-01 23:51:38 +08:00
|
|
|
const Preprocessor &getPreprocessor() const;
|
|
|
|
|
2019-05-29 05:52:34 +08:00
|
|
|
SourceManager &getSourceManager() {
|
|
|
|
return getASTContext().getSourceManager();
|
|
|
|
}
|
|
|
|
const SourceManager &getSourceManager() const {
|
|
|
|
return getASTContext().getSourceManager();
|
|
|
|
}
|
|
|
|
|
2019-12-05 07:09:35 +08:00
|
|
|
const LangOptions &getLangOpts() const {
|
|
|
|
return getASTContext().getLangOpts();
|
|
|
|
}
|
|
|
|
|
2018-05-28 20:23:17 +08:00
|
|
|
/// This function returns top-level decls present in the main file of the AST.
|
|
|
|
/// The result does not include the decls that come from the preamble.
|
2018-06-06 00:30:25 +08:00
|
|
|
/// (These should be const, but RecursiveASTVisitor requires Decl*).
|
|
|
|
ArrayRef<Decl *> getLocalTopLevelDecls();
|
2017-08-01 23:51:38 +08:00
|
|
|
|
2021-03-15 17:18:12 +08:00
|
|
|
const llvm::Optional<std::vector<Diag>> &getDiagnostics() const {
|
|
|
|
return Diags;
|
|
|
|
}
|
2017-05-23 21:42:59 +08:00
|
|
|
|
2020-01-04 23:28:41 +08:00
|
|
|
/// Returns the estimated size of the AST and the accessory structures, in
|
2018-01-25 22:32:21 +08:00
|
|
|
/// bytes. Does not include the size of the preamble.
|
|
|
|
std::size_t getUsedBytes() const;
|
2018-07-03 16:09:29 +08:00
|
|
|
const IncludeStructure &getIncludeStructure() const;
|
2019-02-05 00:19:57 +08:00
|
|
|
const CanonicalIncludes &getCanonicalIncludes() const;
|
2018-01-25 22:32:21 +08:00
|
|
|
|
2019-09-24 19:14:06 +08:00
|
|
|
/// Gets all macro references (definition, expansions) present in the main
|
|
|
|
/// file, including those in the preamble region.
|
|
|
|
const MainFileMacros &getMacros() const;
|
2021-07-13 00:29:48 +08:00
|
|
|
/// Gets all pragma marks in the main file.
|
|
|
|
const std::vector<PragmaMark> &getMarks() const;
|
2019-06-19 22:03:19 +08:00
|
|
|
/// Tokens recorded while parsing the main file.
|
|
|
|
/// (!) does not have tokens from the preamble.
|
|
|
|
const syntax::TokenBuffer &getTokens() const { return Tokens; }
|
|
|
|
|
[clangd] Track document versions, include them with diags, enhance logs
Summary:
This ties to an LSP feature (diagnostic versioning) but really a lot
of the value is in being able to log what's happening with file versions
and queues more descriptively and clearly.
As such it's fairly invasive, for a logging patch :-\
Key decisions:
- at the LSP layer, we don't reqire the client to provide versions (LSP
makes it mandatory but we never enforced it). If not provided,
versions start at 0 and increment. DraftStore handles this.
- don't propagate magically using contexts, but rather manually:
addDocument -> ParseInputs -> (ParsedAST, Preamble, various callbacks)
Context-propagation would hide the versions from ClangdServer, which
would make producing good log messages hard
- within ClangdServer, treat versions as opaque and unordered.
std::string is a convenient type for this, and allows richer versions
for embedders. They're "mandatory" but "null" is a reasonable default.
Subscribers: ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D75582
2020-03-04 07:33:29 +08:00
|
|
|
/// Returns the version of the ParseInputs this AST was built from.
|
|
|
|
llvm::StringRef version() const { return Version; }
|
|
|
|
|
2020-04-08 03:18:00 +08:00
|
|
|
/// Returns the version of the ParseInputs used to build Preamble part of this
|
|
|
|
/// AST. Might be None if no Preamble is used.
|
|
|
|
llvm::Optional<llvm::StringRef> preambleVersion() const;
|
|
|
|
|
2021-01-18 15:58:43 +08:00
|
|
|
const HeuristicResolver *getHeuristicResolver() const {
|
|
|
|
return Resolver.get();
|
|
|
|
}
|
|
|
|
|
2017-05-16 17:38:59 +08:00
|
|
|
private:
|
[clangd] Track document versions, include them with diags, enhance logs
Summary:
This ties to an LSP feature (diagnostic versioning) but really a lot
of the value is in being able to log what's happening with file versions
and queues more descriptively and clearly.
As such it's fairly invasive, for a logging patch :-\
Key decisions:
- at the LSP layer, we don't reqire the client to provide versions (LSP
makes it mandatory but we never enforced it). If not provided,
versions start at 0 and increment. DraftStore handles this.
- don't propagate magically using contexts, but rather manually:
addDocument -> ParseInputs -> (ParsedAST, Preamble, various callbacks)
Context-propagation would hide the versions from ClangdServer, which
would make producing good log messages hard
- within ClangdServer, treat versions as opaque and unordered.
std::string is a convenient type for this, and allows richer versions
for embedders. They're "mandatory" but "null" is a reasonable default.
Subscribers: ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D75582
2020-03-04 07:33:29 +08:00
|
|
|
ParsedAST(llvm::StringRef Version,
|
|
|
|
std::shared_ptr<const PreambleData> Preamble,
|
2017-11-24 21:04:21 +08:00
|
|
|
std::unique_ptr<CompilerInstance> Clang,
|
2019-06-19 22:03:19 +08:00
|
|
|
std::unique_ptr<FrontendAction> Action, syntax::TokenBuffer Tokens,
|
2021-07-13 00:29:48 +08:00
|
|
|
MainFileMacros Macros, std::vector<PragmaMark> Marks,
|
|
|
|
std::vector<Decl *> LocalTopLevelDecls,
|
2021-03-15 17:18:12 +08:00
|
|
|
llvm::Optional<std::vector<Diag>> Diags, IncludeStructure Includes,
|
2019-09-24 19:14:06 +08:00
|
|
|
CanonicalIncludes CanonIncludes);
|
2017-08-01 23:51:38 +08:00
|
|
|
|
[clangd] Track document versions, include them with diags, enhance logs
Summary:
This ties to an LSP feature (diagnostic versioning) but really a lot
of the value is in being able to log what's happening with file versions
and queues more descriptively and clearly.
As such it's fairly invasive, for a logging patch :-\
Key decisions:
- at the LSP layer, we don't reqire the client to provide versions (LSP
makes it mandatory but we never enforced it). If not provided,
versions start at 0 and increment. DraftStore handles this.
- don't propagate magically using contexts, but rather manually:
addDocument -> ParseInputs -> (ParsedAST, Preamble, various callbacks)
Context-propagation would hide the versions from ClangdServer, which
would make producing good log messages hard
- within ClangdServer, treat versions as opaque and unordered.
std::string is a convenient type for this, and allows richer versions
for embedders. They're "mandatory" but "null" is a reasonable default.
Subscribers: ilya-biryukov, javed.absar, MaskRay, jkorous, arphaman, kadircet, usaxena95, cfe-commits
Tags: #clang
Differential Revision: https://reviews.llvm.org/D75582
2020-03-04 07:33:29 +08:00
|
|
|
std::string Version;
|
2017-11-24 21:04:21 +08:00
|
|
|
// In-memory preambles must outlive the AST, it is important that this member
|
|
|
|
// goes before Clang and Action.
|
|
|
|
std::shared_ptr<const PreambleData> Preamble;
|
2017-08-01 23:51:38 +08:00
|
|
|
// We store an "incomplete" FrontendAction (i.e. no EndSourceFile was called
|
|
|
|
// on it) and CompilerInstance used to run it. That way we don't have to do
|
|
|
|
// complex memory management of all Clang structures on our own. (They are
|
|
|
|
// stored in CompilerInstance and cleaned up by
|
|
|
|
// FrontendAction.EndSourceFile).
|
|
|
|
std::unique_ptr<CompilerInstance> Clang;
|
|
|
|
std::unique_ptr<FrontendAction> Action;
|
2019-06-19 22:03:19 +08:00
|
|
|
/// Tokens recorded after the preamble finished.
|
|
|
|
/// - Includes all spelled tokens for the main file.
|
2020-08-07 21:02:06 +08:00
|
|
|
/// - Includes expanded tokens produced **after** preamble.
|
2019-06-19 22:03:19 +08:00
|
|
|
/// - Does not have spelled or expanded tokens for files from preamble.
|
|
|
|
syntax::TokenBuffer Tokens;
|
2017-08-01 23:51:38 +08:00
|
|
|
|
2019-09-24 19:14:06 +08:00
|
|
|
/// All macro definitions and expansions in the main file.
|
|
|
|
MainFileMacros Macros;
|
2021-07-13 00:29:48 +08:00
|
|
|
// Pragma marks in the main file.
|
|
|
|
std::vector<PragmaMark> Marks;
|
2021-03-15 17:18:12 +08:00
|
|
|
// Data, stored after parsing. None if AST was built with a stale preamble.
|
|
|
|
llvm::Optional<std::vector<Diag>> Diags;
|
2018-05-28 20:23:17 +08:00
|
|
|
// Top-level decls inside the current file. Not that this does not include
|
|
|
|
// top-level decls from the preamble.
|
2018-06-06 00:30:25 +08:00
|
|
|
std::vector<Decl *> LocalTopLevelDecls;
|
2018-07-03 16:09:29 +08:00
|
|
|
IncludeStructure Includes;
|
2019-02-05 00:19:57 +08:00
|
|
|
CanonicalIncludes CanonIncludes;
|
2021-01-18 15:58:43 +08:00
|
|
|
std::unique_ptr<HeuristicResolver> Resolver;
|
2017-08-01 23:51:38 +08:00
|
|
|
};
|
2017-07-21 21:29:29 +08:00
|
|
|
|
2017-05-16 17:38:59 +08:00
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|
2018-08-15 00:03:32 +08:00
|
|
|
|
2019-09-04 17:46:06 +08:00
|
|
|
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_PARSEDAST_H
|