Files
carbon-lang/toolchain/check/cpp/context.h
T
Richard SmithandJon Ross-Perkins a8eca2ece6 Delay finishing the C++ translation unit until we reach the real EOF. (#6489)
Instead of parsing a complete C++ translation unit and then interacting
with the translation unit further after the fact, delay finishing the
translation unit until we finish the Carbon check phase. This fixes some
issues where we would produce duplicated or incorrect diagnostics at the
end of the C++ translation unit, particularly for unused declarations.
Now we're in control of how we parse the translation unit, also disable
parsing of C++20 modules if the syntax appears within `import Cpp
inline` code.

Keep the same clang parser alive throughout check, and use it instead of
building a new one when parsing macros. This resolves issues where the
translation unit scope was destroyed too early, resulting in unqualified
lookup within macros being unable to find global scope entities.

---------

Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
2025-12-13 00:56:30 +00:00

69 lines
2.0 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
#ifndef CARBON_TOOLCHAIN_CHECK_CPP_CONTEXT_H_
#define CARBON_TOOLCHAIN_CHECK_CPP_CONTEXT_H_
#include <memory>
#include "clang/Basic/SourceLocation.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendAction.h"
#include "clang/Parse/Parser.h"
#include "common/check.h"
#include "llvm/ADT/SmallVector.h"
namespace Carbon::Check {
// Context for C++ code during check.
//
// This stores state for a Clang AST and Sema, as well as any additional
// information needed to perform mapping between Carbon and C++ types,
// declarations, and similar values.
class CppContext {
public:
explicit CppContext(std::unique_ptr<clang::FrontendAction> action);
~CppContext();
auto action() -> clang::FrontendAction& { return *action_; }
auto ast_context() -> clang::ASTContext& {
return action_->getCompilerInstance().getASTContext();
}
auto sema() -> clang::Sema& {
return action_->getCompilerInstance().getSema();
}
auto parser() -> clang::Parser& { return *parser_; }
auto set_parser(std::unique_ptr<clang::Parser> parser) {
CARBON_CHECK(!parser_);
parser_ = std::move(parser);
}
auto clang_mangle_context() -> clang::MangleContext&;
auto carbon_file_locations() -> llvm::SmallVector<clang::SourceLocation>& {
return carbon_file_locations_;
}
private:
// The clang action that is generating the C++ AST.
std::unique_ptr<clang::FrontendAction> action_;
// Per-Carbon-file start locations for corresponding Clang source buffers.
// Owned and managed by code in location.cpp.
llvm::SmallVector<clang::SourceLocation> carbon_file_locations_;
// The Clang mangle context for the target in the ASTContext.
std::unique_ptr<clang::MangleContext> clang_mangle_context_;
// The Clang parser.
std::unique_ptr<clang::Parser> parser_;
};
} // namespace Carbon::Check
#endif // CARBON_TOOLCHAIN_CHECK_CPP_CONTEXT_H_