2018-08-15 00:03:32 +08:00
|
|
|
//===--- Compiler.h ----------------------------------------------*- C++-*-===//
|
2017-12-04 21:49: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-12-04 21:49:59 +08:00
|
|
|
//
|
2018-08-15 00:03:32 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2017-12-04 21:49:59 +08:00
|
|
|
//
|
|
|
|
// Shared utilities for invoking the clang compiler.
|
2019-09-04 15:35:00 +08:00
|
|
|
// Most callers will use this through Preamble/ParsedAST, but some features like
|
|
|
|
// CodeComplete run their own compile actions that share these low-level pieces.
|
2017-12-04 21:49:59 +08:00
|
|
|
//
|
2018-08-15 00:03:32 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-12-04 21:49:59 +08:00
|
|
|
#ifndef LLVM_CLANG_TOOLS_EXTRA_CLANGD_COMPILER_H
|
|
|
|
#define LLVM_CLANG_TOOLS_EXTRA_CLANGD_COMPILER_H
|
2017-12-15 05:22:03 +08:00
|
|
|
|
2019-01-22 17:58:53 +08:00
|
|
|
#include "../clang-tidy/ClangTidyOptions.h"
|
2019-04-15 20:32:28 +08:00
|
|
|
#include "GlobalCompilationDatabase.h"
|
2019-01-28 22:01:55 +08:00
|
|
|
#include "index/Index.h"
|
2017-12-04 21:49:59 +08:00
|
|
|
#include "clang/Frontend/CompilerInstance.h"
|
|
|
|
#include "clang/Frontend/PrecompiledPreamble.h"
|
2019-01-22 17:58:53 +08:00
|
|
|
#include "clang/Tooling/CompilationDatabase.h"
|
2017-12-04 21:49:59 +08:00
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
namespace clangd {
|
|
|
|
|
|
|
|
class IgnoreDiagnostics : public DiagnosticConsumer {
|
|
|
|
public:
|
2018-02-12 20:48:51 +08:00
|
|
|
static void log(DiagnosticsEngine::Level DiagLevel,
|
|
|
|
const clang::Diagnostic &Info);
|
|
|
|
|
2017-12-04 21:49:59 +08:00
|
|
|
void HandleDiagnostic(DiagnosticsEngine::Level DiagLevel,
|
2018-02-12 20:48:51 +08:00
|
|
|
const clang::Diagnostic &Info) override;
|
2017-12-04 21:49:59 +08:00
|
|
|
};
|
|
|
|
|
2019-01-28 22:01:55 +08:00
|
|
|
// Options to run clang e.g. when parsing AST.
|
|
|
|
struct ParseOptions {
|
|
|
|
tidy::ClangTidyOptions ClangTidyOpts;
|
|
|
|
bool SuggestMissingIncludes = false;
|
|
|
|
};
|
|
|
|
|
2019-01-22 17:58:53 +08:00
|
|
|
/// Information required to run clang, e.g. to parse AST or do code completion.
|
|
|
|
struct ParseInputs {
|
|
|
|
tooling::CompileCommand CompileCommand;
|
|
|
|
IntrusiveRefCntPtr<llvm::vfs::FileSystem> FS;
|
|
|
|
std::string Contents;
|
2019-01-28 22:01:55 +08:00
|
|
|
// Used to recover from diagnostics (e.g. find missing includes for symbol).
|
|
|
|
const SymbolIndex *Index = nullptr;
|
|
|
|
ParseOptions Opts;
|
2019-01-22 17:58:53 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Builds compiler invocation that could be used to build AST or preamble.
|
|
|
|
std::unique_ptr<CompilerInvocation>
|
[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
|
|
|
buildCompilerInvocation(const ParseInputs &Inputs,
|
|
|
|
clang::DiagnosticConsumer &D);
|
2019-01-22 17:58:53 +08:00
|
|
|
|
2018-01-18 23:17:00 +08:00
|
|
|
/// Creates a compiler instance, configured so that:
|
|
|
|
/// - Contents of the parsed file are remapped to \p MainFile.
|
|
|
|
/// - Preamble is overriden to use PCH passed to this function. It means the
|
|
|
|
/// changes to the preamble headers or files included in the preamble are
|
|
|
|
/// not visible to this compiler instance.
|
2018-10-10 21:27:25 +08:00
|
|
|
/// - llvm::vfs::FileSystem is used for all underlying file accesses. The
|
|
|
|
/// actual vfs used by the compiler may be an overlay over the passed vfs.
|
2018-01-18 23:17:00 +08:00
|
|
|
/// Returns null on errors. When non-null value is returned, it is expected to
|
|
|
|
/// be consumed by FrontendAction::BeginSourceFile to properly destroy \p
|
|
|
|
/// MainFile.
|
2017-12-04 21:49:59 +08:00
|
|
|
std::unique_ptr<CompilerInstance> prepareCompilerInstance(
|
|
|
|
std::unique_ptr<clang::CompilerInvocation>, const PrecompiledPreamble *,
|
|
|
|
std::unique_ptr<llvm::MemoryBuffer> MainFile,
|
2018-10-10 21:27:25 +08:00
|
|
|
IntrusiveRefCntPtr<llvm::vfs::FileSystem>, DiagnosticConsumer &);
|
2017-12-04 21:49:59 +08:00
|
|
|
|
|
|
|
} // namespace clangd
|
|
|
|
} // namespace clang
|
|
|
|
|
2018-08-15 00:03:32 +08:00
|
|
|
#endif // LLVM_CLANG_TOOLS_EXTRA_CLANGD_COMPILER_H
|