Skip to content

Comment out compiler builtins#304

Open
davispuh wants to merge 1 commit intojacob-carlborg:masterfrom
davispuh:builtin
Open

Comment out compiler builtins#304
davispuh wants to merge 1 commit intojacob-carlborg:masterfrom
davispuh:builtin

Conversation

@davispuh
Copy link
Copy Markdown

Consider C header like:

#define va_copy __builtin_va_copy
#define ALIGNOF(TYPE)  _Alignof (TYPE)
#define __unused  __attribute__((__unused__))
#define __noreturn  __attribute__((__noreturn__))
#define STATIC_ASSERT  _Static_assert
#define CONST  const
#define STATIC  static
#define __volatile  volatile

Currently dsep would produce:

enum va_copy = __builtin_va_copy;

Which fails to compile:

Error: undefined identifier `__builtin_va_copy`
enum va_copy = __builtin_va_copy;

When this PR is applied together with #303
Then result will be:

// FIXME: enum va_copy = __builtin_va_copy;
// FIXME: alias ALIGNOF = _Alignof;
// FIXME: enum __unused = __attribute__(__unused__);
// FIXME: enum __noreturn = __attribute__(__noreturn__);
// FIXME: enum STATIC_ASSERT = _Static_assert;
// FIXME: enum CONST = const;
// FIXME: enum STATIC = static;

Copilot AI review requested due to automatic review settings March 19, 2026 19:03
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR updates macro translation output so that macros which expand to C compiler builtins/keywords/attributes (and therefore generate invalid D code) are emitted as commented-out // FIXME: lines instead of uncompilable declarations, aligning with the behavior introduced in #303.

Changes:

  • Detect compiler builtins (e.g., __builtin_*) and _Alignof in function-alias macro translation and prefix the generated alias with // FIXME:.
  • Detect keyword/attribute-like macro values (e.g., const, static, _Static_assert, __attribute__*) in alias/const macro translation and prefix the generated declaration with // FIXME:.
  • Avoid repeated translation of the same macro expression by storing the translated value once.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +624 to +631
string value = debraced.translate(expressionContext);
if (["const", "static", "volatile", "_Static_assert"].canFind(value) ||
// Handle compiler builtins
value.startsWith("__builtin_") ||
value.startsWith("__attribute__"))
{
formatString = "// FIXME: " ~ formatString;
}
Context context,
TypedMacroDefinition definition)
{
import std.algorithm.searching : startsWith, endsWith, canFind;
Copy link
Copy Markdown
Owner

@jacob-carlborg jacob-carlborg left a comment

Choose a reason for hiding this comment

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

There's no test added.

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