mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-18 00:20:28 +00:00
Stop using the clang tooling library to build an ASTUnit; that library is set up to process clang frontend arguments, assuming that something has already built frontend arguments from the compiler arguments. It is also too encapsulated and doesn't let us inspect and modify the compiler invocation before it's executed. Instead, build the AST unit directly in two phases: * FIrst, take a list of clang driver arguments and convert them into a list of compiler arguments, using `clang::createInvocation`. Internally, this uses the clang driver to build a frontend invocation, including building system-specific include paths as needed. * Then, directly build an ASTUnit from this compiler invocation. I've factored this so that we can split out the `createInvocation` step, with the intention that we may want to move it out of check and into the carbon driver with the rest of the driver-level argument handling, and we may want to customize some of the clang options before we invoke the clang frontend with that set of options. In order to make the invocation reusable, it no longer depends on the name of the carbon file importing the C++ code. In place of synthesizing a header file name as `<foo.carbon>.generated.cpp_imports.h`, we now insert line marker directives into the generated header so that errors in that header cause Clang to point a diagnostic back at the Carbon source file itself. This results in a minor improvement in the diagnostic output: we no longer refer to a nonexistent generated file. But the snippet still contains text that doesn't match the source code, so it remains imperfect. --------- Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
79 lines
2.9 KiB
C++
79 lines
2.9 KiB
C++
// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
|
|
// Exceptions. See /LICENSE for license information.
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include "absl/flags/flag.h"
|
|
#include "toolchain/diagnostics/diagnostic_kind.h"
|
|
#include "toolchain/testing/coverage_helper.h"
|
|
|
|
ABSL_FLAG(std::string, testdata_manifest, "",
|
|
"A path to a file containing repo-relative names of test files.");
|
|
|
|
namespace Carbon::Diagnostics {
|
|
namespace {
|
|
|
|
constexpr Kind Kinds[] = {
|
|
#define CARBON_DIAGNOSTIC_KIND(Name) Kind::Name,
|
|
#include "toolchain/diagnostics/diagnostic_kind.def"
|
|
};
|
|
|
|
constexpr Kind UntestedKinds[] = {
|
|
// These exist only for unit tests.
|
|
Kind::TestDiagnostic,
|
|
Kind::TestDiagnosticNote,
|
|
|
|
// Diagnosing erroneous install conditions, but test environments are
|
|
// typically correct.
|
|
Kind::CompilePreludeManifestError,
|
|
Kind::DriverInstallInvalid,
|
|
|
|
// These diagnose filesystem issues that are hard to unit test.
|
|
Kind::ErrorReadingFile,
|
|
Kind::ErrorStattingFile,
|
|
Kind::FileTooLarge,
|
|
|
|
// These aren't feasible to test with a normal testcase, but are tested in
|
|
// lex/tokenized_buffer_test.cpp.
|
|
Kind::TooManyTokens,
|
|
Kind::UnsupportedCrLineEnding,
|
|
Kind::UnsupportedLfCrLineEnding,
|
|
|
|
// This is a little long but is tested in lex/numeric_literal_test.cpp.
|
|
Kind::TooManyDigits,
|
|
|
|
// TODO: This can only fire if the first message in a diagnostic is rooted
|
|
// in a file other than the file being compiled. The language server
|
|
// currently only supports compiling one file at a time. Do one of:
|
|
// - When imports are supported, find a diagnostic whose first message isn't
|
|
// in the current file.
|
|
// - Require all diagnostics produced by compiling have their first location
|
|
// be in the file being compiled, never an import.
|
|
Kind::LanguageServerDiagnosticInWrongFile,
|
|
|
|
// TODO: This can only fire if we attempt to convert a non-reference
|
|
// expression to a durable reference binding. At the moment, the only time
|
|
// we attempt reference binding is within a `var` pattern, where the
|
|
// conversion cannot fail. This should be covered once we support `ref`
|
|
// binding syntax.
|
|
Kind::ConversionFailureNonRefToRef,
|
|
|
|
// TODO: These are temporarily unreachable because we don't pass invalid
|
|
// driver options to Clang, but will become reachable once we support
|
|
// passing custom Clang arguments.
|
|
Kind::CppInteropDriverError,
|
|
Kind::CppInteropDriverWarning,
|
|
};
|
|
|
|
// Looks for diagnostic kinds that aren't covered by a file_test.
|
|
TEST(Coverage, Kind) {
|
|
Testing::TestKindCoverage(absl::GetFlag(FLAGS_testdata_manifest),
|
|
R"(^ *// CHECK:STDERR: .* \[(\w+)\]$)",
|
|
llvm::ArrayRef(Kinds),
|
|
llvm::ArrayRef(UntestedKinds));
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace Carbon::Diagnostics
|