Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[llvm] Add a new Triple::str(N) method #145150

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

royitaqi
Copy link
Contributor

@royitaqi royitaqi commented Jun 21, 2025

Change

Adds a new Triple::str(N) method, which returns the triple string but only keep the first N components. See method doc for details.

Note that the return type is StringRef. This is different from the existing methods, where const std::string & is returned by Triple::str() and Triple::getTriple(). The reason for StringRef is performance - it's unnecessary to create a new std::string object, which would copy the content of the triple string.

Usage

This method can remove trailing components (e.g. "object format") for edit or display purposes.

Alternatives

Alternative 1: Add the same method but return std::string for slightly more consistency with the existing methods. However, we probably don't want to make unnecessary copies just to make the API look more consistent. Also, it wouldn't be completely the same, because one is std::string and the other is const std::string &.

Alternative 2: Add a new method called Triple::clone(N = 5), which will return a new triple with the triple string that would be returned by Triple::str(N). However, the user might not want to construct a new triple object, or maybe they want to normalize it before creating such object. Triple::str(N) is more flexible.

Tests

Added TripleTest::StrFirstN.

$ ninja llvm-test-depends
$ unittests/TargetParser/TargetParserTests --gtest_filter="*StrFirstN*"
...
[ RUN      ] TripleTest.StrFirstN
[       OK ] TripleTest.StrFirstN (0 ms)

Copy link
Contributor

@jhuber6 jhuber6 left a comment

Choose a reason for hiding this comment

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

What's the expected use here? Seems very specific and more easily done with str.split('-').

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.

2 participants