Skip to content

[NFC][Clang] Adopt TrailingObjects convenience API in MacroArgs #139635

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 13, 2025

Conversation

jurahul
Copy link
Contributor

@jurahul jurahul commented May 12, 2025

Adopt convenience API for single trailing type added in #138970.

@jurahul jurahul changed the title [NFC][Clang] Adopt TrailingObjects convienence API in MacroArgs [NFC][Clang] Adopt TrailingObjects convenience API in MacroArgs May 12, 2025
@jurahul jurahul marked this pull request as ready for review May 12, 2025 23:23
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels May 12, 2025
@jurahul jurahul requested a review from kazutakahirata May 12, 2025 23:23
@llvmbot
Copy link
Member

llvmbot commented May 12, 2025

@llvm/pr-subscribers-clang

Author: Rahul Joshi (jurahul)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/139635.diff

1 Files Affected:

  • (modified) clang/lib/Lex/MacroArgs.cpp (+2-2)
diff --git a/clang/lib/Lex/MacroArgs.cpp b/clang/lib/Lex/MacroArgs.cpp
index 2f97d9e02bc11..548df16c59f6b 100644
--- a/clang/lib/Lex/MacroArgs.cpp
+++ b/clang/lib/Lex/MacroArgs.cpp
@@ -66,7 +66,7 @@ MacroArgs *MacroArgs::create(const MacroInfo *MI,
                   "uninitialized array (as opposed to reusing a cached "
                   "MacroArgs)");
     std::copy(UnexpArgTokens.begin(), UnexpArgTokens.end(),
-              Result->getTrailingObjects<Token>());
+              Result->getTrailingObjects());
   }
 
   return Result;
@@ -119,7 +119,7 @@ const Token *MacroArgs::getUnexpArgument(unsigned Arg) const {
   assert(Arg < getNumMacroArguments() && "Invalid arg #");
   // The unexpanded argument tokens start immediately after the MacroArgs object
   // in memory.
-  const Token *Start = getTrailingObjects<Token>();
+  const Token *Start = getTrailingObjects();
   const Token *Result = Start;
 
   // Scan to find Arg.

Copy link
Collaborator

@shafik shafik left a comment

Choose a reason for hiding this comment

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

Can you please justify this change, we don't use getTrailingObjects like this anywhere else and I am not sure I see the point in this change. This should always go in the summary so that a reviewer does not have to guess you intent.

In general we prefer being as explicit as possible since that leads to less ambiguity for future developers working on the code later.

@shafik shafik requested a review from AaronBallman May 13, 2025 00:45
@jurahul
Copy link
Contributor Author

jurahul commented May 13, 2025

I filled in the description now. I am doing similar changes on LLVM/MLIR side (change has been approved).

I added these convenience APIs to TrailingObjects to support the common code patterns of a single trailing type as well as creating an ArrayRef from the trailing objects pointer.

@jurahul
Copy link
Contributor Author

jurahul commented May 13, 2025

FYI, LLVM and MLIR change of similar nature: #138554

@kazutakahirata
Copy link
Contributor

@jurahul Thanks for the pointer in the description. So, in this particular case, we end up invoking the following?

  FirstTrailingType *getTrailingObjects() {
    static_assert(sizeof...(TrailingTys) == 1,
                  "Can use non-templated getTrailingObjects() only when there "
                  "is a single trailing type");
    return getTrailingObjects<FirstTrailingType>();
  }

@jurahul
Copy link
Contributor Author

jurahul commented May 13, 2025

@jurahul Thanks for the pointer in the description. So, in this particular case, we end up invoking the following?

  FirstTrailingType *getTrailingObjects() {
    static_assert(sizeof...(TrailingTys) == 1,
                  "Can use non-templated getTrailingObjects() only when there "
                  "is a single trailing type");
    return getTrailingObjects<FirstTrailingType>();
  }

Right. With this API , when there is a single trailing type, you can call getTrailingObjects directly without having to redundantly specify that single trailing type again.

@jurahul jurahul closed this May 13, 2025
@jurahul jurahul reopened this May 13, 2025
@jurahul
Copy link
Contributor Author

jurahul commented May 13, 2025

Closed by mistake.

@jurahul jurahul requested a review from shafik May 13, 2025 02:05
@jurahul
Copy link
Contributor Author

jurahul commented May 13, 2025

Note: I tried to do a change similar to LLVM/MLIR where I tried to convert all usage under clang/AST and it became too large, so will split it up into multiple ones.

Copy link
Contributor

@kazutakahirata kazutakahirata left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

@shafik
Copy link
Collaborator

shafik commented May 13, 2025

I filled in the description now. I am doing similar changes on LLVM/MLIR side (change has been approved).

I added these convenience APIs to TrailingObjects to support the common code patterns of a single trailing type as well as creating an ArrayRef from the trailing objects pointer.

Thank you, the change makes a lot more sense with the context.

@jurahul jurahul merged commit 726d2cf into llvm:main May 13, 2025
27 checks passed
@jurahul jurahul deleted the clang_trailingobjects_macroargs branch May 13, 2025 13:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants