Files
carbon-lang/toolchain/check/cpp/macros.h
T
Richard Smith 6e9e871b74 Fix handling of recursive macros. (#7593)
Inject the name of a macro rather than its contents when computing its
expansion. If the macro refers to itself, it will not expand within its
own body, rather than expanding once.

Switching from `EnterTokenStream` to `EnterToken` exposed that our Clang
preprocessing environment was a little broken -- we reached the end of
the primary source file and starting tearing stuff down before we
actually finished parsing, which we were mostly getting away with before
but aren't any more. Enabled Clang's incremental processing mode to fix
this. This causes Clang to remain in the main source file when it
reaches EOF instead of popping it. This also causes the diagnostics for
invalid `module;` declarations to change, but in a way that seems not
really any worse than before.

Also slightly changes the diagnostics produced from macro expansion
failures. The new diagnostics are a bit more precise -- they now capture
the outermost level of macro expansion -- but we don't do a good job of
rendering the Clang snippet attached to the "in macro expansion" context
note yet, so the context looks a bit weird: we get two different
snippets attached to the same diagnostic.
2026-07-31 20:09:03 +00:00

27 lines
873 B
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_MACROS_H_
#define CARBON_TOOLCHAIN_CHECK_CPP_MACROS_H_
#include "toolchain/check/context.h"
namespace clang {
class IdentifierInfo;
class MacroInfo;
} // namespace clang
namespace Carbon::Check {
// Tries to evaluate the given macro. The macro will be evaluated as a
// constant if possible. Returns an `InstId` on success or
// `SemIR::ErrorInst::InstId` otherwise.
auto TryEvaluateMacro(Context& context, SemIR::LocId loc_id,
clang::IdentifierInfo* identifier_info,
clang::MacroInfo* macro_info) -> SemIR::InstId;
} // namespace Carbon::Check
#endif // CARBON_TOOLCHAIN_CHECK_CPP_MACROS_H_