Skip to content

Conversation

@lanza
Copy link
Member

@lanza lanza commented Nov 18, 2025

Stack from ghstack (oldest at bottom):

This patch implements SourceLocExpr code generation, which enables
support for the following builtin functions:

  • __builtin_LINE
  • __builtin_FILE
  • __builtin_FUNCTION
  • __builtin_COLUMN

These builtins are evaluated at compile-time and emit constant values
or global string literals. The implementation follows the traditional
CodeGen approach of using ConstantEmitter to emit abstract constants
from evaluated APValues.

A key challenge was handling synthetic StringLiterals created during
constant evaluation (for __builtin_FILE and __builtin_FUNCTION).
These synthetic literals don't have valid source locations, which
caused assertion failures when creating global string constants.
The fix adds a check in getGlobalForStringLiteral to use
UnknownLoc when the StringLiteral has an invalid source range.

This feature unblocks:

  • std::source_location (C++20)
  • Logging and debugging macros that use source location builtins
  • Assertion/diagnostic frameworks

Test coverage includes:

  • Basic __builtin_LINE, __builtin_FILE, __builtin_FUNCTION, __builtin_COLUMN
  • Usage in global variable initializers
  • Default function arguments
  • Lambda expressions
  • Combined usage of multiple source location builtins

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
Copy link
Member

@bcardosolopes bcardosolopes left a comment

Choose a reason for hiding this comment

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

LGTM with one minor nit!

@@ -0,0 +1,73 @@
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -std=c++20 -fclangir -emit-cir %s -o %t.cir
// RUN: FileCheck --input-file=%t.cir %s
Copy link
Member

Choose a reason for hiding this comment

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

Please add LLVM and OGCG checks too!

@bcardosolopes
Copy link
Member

The windows failure seems exposed after rebase, we should probably REQUIRES linux / darwin for it right now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants