- Notifications
You must be signed in to change notification settings - Fork14.5k
RFC: [llvm] specialize cl::opt_storage for std::string#149172
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Draft
andrurogerz wants to merge1 commit intollvm:mainChoose a base branch fromandrurogerz:clopt-string
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
+190 −0
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
github-actionsbot commentedJul 16, 2025 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
You can test this locally with the following command:git-clang-format --diff HEAD~1 HEAD --extensions h -- llvm/include/llvm/Support/CommandLine.h llvm/include/llvm/TargetParser/Triple.h View the diff from clang-format here.diff --git a/llvm/include/llvm/Support/CommandLine.h b/llvm/include/llvm/Support/CommandLine.hindex 823e31a80..7c041fbff 100644--- a/llvm/include/llvm/Support/CommandLine.h+++ b/llvm/include/llvm/Support/CommandLine.h@@ -1417,23 +1417,23 @@ public: const OptionValue<std::string> &getDefault() const { return Default; } // Support implicit conversions to std::string and LLVM string-like types.- //operator std::string() const { return Value; }- operator std::string&() { return Value; }- operator const std::string&() const { return Value; }+ // operator std::string() const { return Value; }+ operator std::string &() { return Value; }+ operator const std::string &() const { return Value; } operator llvm::StringRef() const { return llvm::StringRef(Value); } operator llvm::Twine() const { return llvm::Twine(llvm::StringRef(Value)); } // If the datatype is a pointer, support -> on it. std::string operator->() const { return Value; }- Self& operator=(const Self& rhs) = default;+ Self &operator=(const Self &rhs) = default;- Self& operator=(const std::string& rhs) {+ Self &operator=(const std::string &rhs) { Value = rhs; return *this; }- Self& operator=(const char* rhs) {+ Self &operator=(const char *rhs) { Value = rhs; return *this; }@@ -1491,43 +1491,39 @@ public: char operator[](size_t pos) const { return Value.at(pos); }- Self& assign(const std::string& rhs) {+ Self &assign(const std::string &rhs) { Value.assign(rhs); return *this; }- Self& assign(const char* rhs) {+ Self &assign(const char *rhs) { Value.assign(rhs); return *this; }- Self& operator+=(const std::string& rhs) {+ Self &operator+=(const std::string &rhs) { Value += rhs; return *this; }- Self& operator+=(const char* rhs) {+ Self &operator+=(const char *rhs) { Value += rhs; return *this; }- friend std::string- operator+(Self const& lhs, std::string const& rhs) {+ friend std::string operator+(Self const &lhs, std::string const &rhs) { return lhs.Value + rhs; }- friend std::string- operator+(std::string const& lhs, Self const& rhs) {+ friend std::string operator+(std::string const &lhs, Self const &rhs) { return lhs + rhs.Value; }- friend std::string- operator+(Self const& lhs, char const* rhs) {+ friend std::string operator+(Self const &lhs, char const *rhs) { return lhs.Value + rhs; }- friend std::string- operator+(char const* lhs, Self const& rhs) {+ friend std::string operator+(char const *lhs, Self const &rhs) { return lhs + rhs.Value; } |
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Purpose
Specialize
llvm::cl::opt_storage
for data typestd::string
so that it no longer inherits fromstd::string
. With this patch in place, explicitly instantiatedcl::opt<std::string>
can be safely exported from an LLVM Windows DLL.Overview
cl::opt_storage
for data typestd::string
. It does NOT inherit fromstd::string
as it did previously when data typestd::string
matched the partially specified templatecl::opt_storage<DataType, false, true>
std::string
,llvm::StringRef
, andllvm::Twine
so that instances can be used in many places wherestd::string
is used.std::string
methods so that instances of the class are mostly interoperable withstd::string
.Triple
so it properly constructs fromcl::opt_storage<std::string>
. This change avoids having to modify clients that constructTriple
s fromcl::opt<std::string>
instances.Background
This patch is in support of annotating LLVM's public symbols for Windows DLL export., tracked in#109483. Additional context is provided inthis discourse.
This change is needed because, without it, we cannot export
opt<std::string>
from an LLVM Windows DLL. This is because MSVC exports all ancestor classes when exporting an instantiated template class. Since one ofopt
's ancestor classes is its type argument (viaopt_storage
), it is an ancestor ofstd::string
. Therefore, if we exportopt<std::string>
from the LLVM DLL, MSVC forcesstd::basic_string
to also be exported. This leads to duplicate symbol errors and generally seems like a bad idea. Compiling withclang-cl
does not exhibit this behavior.Validation
cl
andclang-cl
.clang
andgcc
.