Skip to content

Conversation

@ivanaivanovska
Copy link
Contributor

@ivanaivanovska ivanaivanovska commented Nov 5, 2025

Adds support for object-like macros with a single replacement numeric-literal kind token. Only macros that evaluate to an integer constant are supported for now. When detected at name lookup, they are imported as a constant integer value in Carbon.

Demo:

// --- macros.h

#define CONFIG_VALUE 2
// main.carbon
library "Main";

import Cpp library "macros.h";
import Core library "io";

fn Run() {
    let a: i32 = Cpp.CONFIG_VALUE;
    Core.Print(a);
}
$ bazel-bin/toolchain/carbon compile main.carbon
$ bazel-bin/toolchain/carbon link main.o \--output=demo_carbon
$ ./demo_carbon
2

Part of #6303

@ivanaivanovska ivanaivanovska requested a review from a team as a code owner November 5, 2025 14:58
@ivanaivanovska ivanaivanovska requested review from zygoloid and removed request for a team November 5, 2025 14:58
// CHECK:STDERR: let b: f32 = Cpp.n;
// CHECK:STDERR: ^~~~~
// CHECK:STDERR:
let b: f32 = Cpp.n;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should eventually allow an implicit conversion from i32 to f32, at which point I think this test won't test that the macro is being selected. How about something like:

Suggested change
let b: f32 = Cpp.n;
var b: array(f32, Cpp.n) = (1.0,);

... to test that it's being treated as an integer constant with value 1.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sg, thanks for the suggestion.

return macro_info->second;

clang::Preprocessor& preprocessor = context.clang_sema().getPreprocessor();
clang::IdentifierInfo* identifier_info =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be nice to do this identifier lookup only once, rather than once here and once in ClangLookupName. I'd be happy with a TODO if you'd like to get this landed to unblock further macro work.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a TODO.

@ivanaivanovska ivanaivanovska added this pull request to the merge queue Nov 11, 2025
Merged via the queue into carbon-language:trunk with commit 3b0dad9 Nov 11, 2025
8 checks passed
@ivanaivanovska ivanaivanovska deleted the macros_first_impl branch November 11, 2025 12:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants