Skip to content

Commit af93217

Browse files
committed
Use ManagedStatic and lazy initialization of cl::opt in libSupport to make it free of global initializer
We can build it with -Werror=global-constructors now. This helps in situation where libSupport is embedded as a shared library, potential with dlopen/dlclose scenario, and when command-line parsing or other facilities may not be involved. Avoiding the implicit construction of these cl::opt can avoid double-registration issues and other kind of behavior. Reviewed By: lattner, jpienaar Differential Revision: https://reviews.llvm.org/D105959
1 parent 20113d6 commit af93217

36 files changed

+526
-289
lines changed

clang-tools-extra/clangd/index/dex/dexp/Dexp.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ class Command {
9191
// By resetting the parser options, we lost the standard -help flag.
9292
llvm::cl::opt<bool, false, llvm::cl::parser<bool>> Help{
9393
"help", llvm::cl::desc("Display available options"),
94-
llvm::cl::ValueDisallowed, llvm::cl::cat(llvm::cl::GeneralCategory)};
94+
llvm::cl::ValueDisallowed, llvm::cl::cat(llvm::cl::getGeneralCategory())};
9595
// FIXME: Allow commands to signal failure.
9696
virtual void run() = 0;
9797

clang-tools-extra/clangd/indexer/IndexerMain.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ int main(int argc, const char **argv) {
124124
)";
125125

126126
auto Executor = clang::tooling::createExecutorFromCommandLineArgs(
127-
argc, argv, llvm::cl::GeneralCategory, Overview);
127+
argc, argv, llvm::cl::getGeneralCategory(), Overview);
128128

129129
if (!Executor) {
130130
llvm::errs() << llvm::toString(Executor.takeError()) << "\n";

clang/tools/clang-refactor/ClangRefactor.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ namespace opts {
3838
static cl::OptionCategory CommonRefactorOptions("Refactoring options");
3939

4040
static cl::opt<bool> Verbose("v", cl::desc("Use verbose output"),
41-
cl::cat(cl::GeneralCategory),
41+
cl::cat(cl::getGeneralCategory()),
4242
cl::sub(*cl::AllSubCommands));
4343

4444
static cl::opt<bool> Inplace("i", cl::desc("Inplace edit <file>s"),
45-
cl::cat(cl::GeneralCategory),
45+
cl::cat(cl::getGeneralCategory()),
4646
cl::sub(*cl::AllSubCommands));
4747

4848
} // end namespace opts
@@ -613,7 +613,7 @@ int main(int argc, const char **argv) {
613613
ClangRefactorTool RefactorTool;
614614

615615
auto ExpectedParser = CommonOptionsParser::create(
616-
argc, argv, cl::GeneralCategory, cl::ZeroOrMore,
616+
argc, argv, cl::getGeneralCategory(), cl::ZeroOrMore,
617617
"Clang-based refactoring tool for C, C++ and Objective-C");
618618
if (!ExpectedParser) {
619619
llvm::errs() << ExpectedParser.takeError();

llvm/docs/CommandLine.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ declared, the command line option ``-help-list`` becomes visible which will
661661
print the command line options as uncategorized list.
662662

663663
Note that Options that are not explicitly categorized will be placed in the
664-
``cl::GeneralCategory`` category.
664+
``cl::getGeneralCategory()`` category.
665665

666666
.. _Reference Guide:
667667

llvm/include/llvm/Support/ARMAttributeParser.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ class ARMAttributeParser : public ELFAttributeParser {
7171

7272
public:
7373
ARMAttributeParser(ScopedPrinter *sw)
74-
: ELFAttributeParser(sw, ARMBuildAttrs::ARMAttributeTags, "aeabi") {}
74+
: ELFAttributeParser(sw, ARMBuildAttrs::getARMAttributeTags(), "aeabi") {}
7575
ARMAttributeParser()
76-
: ELFAttributeParser(ARMBuildAttrs::ARMAttributeTags, "aeabi") {}
76+
: ELFAttributeParser(ARMBuildAttrs::getARMAttributeTags(), "aeabi") {}
7777
};
7878
}
7979

llvm/include/llvm/Support/ARMBuildAttributes.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
namespace llvm {
2424
namespace ARMBuildAttrs {
2525

26-
extern const TagNameMap ARMAttributeTags;
26+
const TagNameMap &getARMAttributeTags();
2727

2828
enum SpecialAttr {
2929
// This is for the .cpu asm attr. It translates into one or more

llvm/include/llvm/Support/CommandLine.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ class OptionCategory {
202202
};
203203

204204
// The general Option Category (used as default category).
205-
extern OptionCategory GeneralCategory;
205+
OptionCategory &getGeneralCategory();
206206

207207
//===----------------------------------------------------------------------===//
208208
// SubCommand class
@@ -342,7 +342,7 @@ class Option {
342342
: NumOccurrences(0), Occurrences(OccurrencesFlag), Value(0),
343343
HiddenFlag(Hidden), Formatting(NormalFormatting), Misc(0),
344344
FullyInitialized(false), Position(0), AdditionalVals(0) {
345-
Categories.push_back(&GeneralCategory);
345+
Categories.push_back(&getGeneralCategory());
346346
}
347347

348348
inline void setNumAdditionalVals(unsigned n) { AdditionalVals = n; }

llvm/include/llvm/Support/RISCVAttributeParser.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ class RISCVAttributeParser : public ELFAttributeParser {
2727

2828
public:
2929
RISCVAttributeParser(ScopedPrinter *sw)
30-
: ELFAttributeParser(sw, RISCVAttrs::RISCVAttributeTags, "riscv") {}
30+
: ELFAttributeParser(sw, RISCVAttrs::getRISCVAttributeTags(), "riscv") {}
3131
RISCVAttributeParser()
32-
: ELFAttributeParser(RISCVAttrs::RISCVAttributeTags, "riscv") {}
32+
: ELFAttributeParser(RISCVAttrs::getRISCVAttributeTags(), "riscv") {}
3333
};
3434

3535
} // namespace llvm

llvm/include/llvm/Support/RISCVAttributes.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
namespace llvm {
2323
namespace RISCVAttrs {
2424

25-
extern const TagNameMap RISCVAttributeTags;
25+
const TagNameMap &getRISCVAttributeTags();
2626

2727
enum AttrType : unsigned {
2828
// Attribute types in ELF/.riscv.attributes.

llvm/include/llvm/Support/ScopedPrinter.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ template <typename T> struct EnumEntry {
3232
// "Advanced Micro Devices X86-64" on GNU style
3333
StringRef AltName;
3434
T Value;
35-
EnumEntry(StringRef N, StringRef A, T V) : Name(N), AltName(A), Value(V) {}
36-
EnumEntry(StringRef N, T V) : Name(N), AltName(N), Value(V) {}
35+
constexpr EnumEntry(StringRef N, StringRef A, T V)
36+
: Name(N), AltName(A), Value(V) {}
37+
constexpr EnumEntry(StringRef N, T V) : Name(N), AltName(N), Value(V) {}
3738
};
3839

3940
struct HexNumber {

llvm/include/llvm/Support/WithColor.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ namespace cl {
2020
class OptionCategory;
2121
}
2222

23-
extern cl::OptionCategory ColorCategory;
23+
extern cl::OptionCategory &getColorCategory();
2424

2525
// Symbolic names for various syntax elements.
2626
enum class HighlightColor {

llvm/lib/Support/ARMBuildAttrs.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ static const TagNameItem tagData[] = {
6363
{ARMBuildAttrs::ABI_align_preserved, "Tag_ABI_align8_preserved"},
6464
};
6565

66-
const TagNameMap llvm::ARMBuildAttrs::ARMAttributeTags(tagData,
67-
sizeof(tagData) /
68-
sizeof(TagNameItem));
66+
constexpr TagNameMap ARMAttributeTags{tagData};
67+
const TagNameMap &llvm::ARMBuildAttrs::getARMAttributeTags() {
68+
return ARMAttributeTags;
69+
}

0 commit comments

Comments
 (0)