Skip to content

Commit 62cd6b7

Browse files
jurahulaokblast
authored andcommitted
[NFC][LLVM] Code cleanup in llvm-config.cpp (llvm#163993)
- Fix function names to conform to LLVM CS and mark local function static. - Use range for loops to simplify code. - use `interleave` instead of manual loops to print lists.
1 parent ee82923 commit 62cd6b7

File tree

1 file changed

+52
-71
lines changed

1 file changed

+52
-71
lines changed

llvm/tools/llvm-config/llvm-config.cpp

Lines changed: 52 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ enum LinkMode {
7676
/// libraries.
7777
/// \param GetComponentNames - Get the component names instead of the
7878
/// library name.
79-
static void VisitComponent(const std::string &Name,
79+
static void visitComponent(const std::string &Name,
8080
const StringMap<AvailableComponent *> &ComponentMap,
8181
std::set<AvailableComponent *> &VisitedComponents,
8282
std::vector<std::string> &RequiredLibs,
@@ -89,9 +89,8 @@ static void VisitComponent(const std::string &Name,
8989
AvailableComponent *AC = ComponentMap.lookup(Name);
9090
if (!AC) {
9191
errs() << "Can't find component: '" << Name << "' in the map. Available components are: ";
92-
for (const auto &Component : ComponentMap) {
92+
for (const auto &Component : ComponentMap)
9393
errs() << "'" << Component.first() << "' ";
94-
}
9594
errs() << "\n";
9695
report_fatal_error("abort");
9796
}
@@ -108,27 +107,29 @@ static void VisitComponent(const std::string &Name,
108107
return;
109108

110109
// Otherwise, visit all the dependencies.
111-
for (unsigned i = 0; AC->RequiredLibraries[i]; ++i) {
112-
VisitComponent(AC->RequiredLibraries[i], ComponentMap, VisitedComponents,
113-
RequiredLibs, IncludeNonInstalled, GetComponentNames,
110+
for (const char *Lib : AC->RequiredLibraries) {
111+
if (!Lib)
112+
break;
113+
visitComponent(Lib, ComponentMap, VisitedComponents, RequiredLibs,
114+
IncludeNonInstalled, GetComponentNames,
114115
GetComponentLibraryPath, Missing, DirSep);
115116
}
116117

117118
// Special handling for the special 'extensions' component. Its content is
118119
// not populated by llvm-build, but later in the process and loaded from
119120
// ExtensionDependencies.inc.
120121
if (Name == "extensions") {
121-
for (auto const &AvailableExtension : AvailableExtensions) {
122-
for (const char *const *Iter = &AvailableExtension.RequiredLibraries[0];
123-
*Iter; ++Iter) {
124-
AvailableComponent *AC = ComponentMap.lookup(*Iter);
125-
if (!AC) {
126-
RequiredLibs.push_back(*Iter);
127-
} else {
128-
VisitComponent(*Iter, ComponentMap, VisitedComponents, RequiredLibs,
122+
for (const ExtensionDescriptor &AvailableExtension : AvailableExtensions) {
123+
for (const char *Lib : AvailableExtension.RequiredLibraries) {
124+
if (!Lib)
125+
break;
126+
AvailableComponent *AC = ComponentMap.lookup(Lib);
127+
if (!AC)
128+
RequiredLibs.push_back(Lib);
129+
else
130+
visitComponent(Lib, ComponentMap, VisitedComponents, RequiredLibs,
129131
IncludeNonInstalled, GetComponentNames,
130132
GetComponentLibraryPath, Missing, DirSep);
131-
}
132133
}
133134
}
134135
}
@@ -159,11 +160,13 @@ static void VisitComponent(const std::string &Name,
159160
/// \param IncludeNonInstalled - Whether non-installed components should be
160161
/// reported.
161162
/// \param GetComponentNames - True if one would prefer the component names.
162-
static std::vector<std::string> ComputeLibsForComponents(
163-
const std::vector<StringRef> &Components, bool IncludeNonInstalled,
164-
bool GetComponentNames, const std::function<std::string(const StringRef &)>
165-
*GetComponentLibraryPath,
166-
std::vector<std::string> *Missing, const std::string &DirSep) {
163+
static std::vector<std::string>
164+
computeLibsForComponents(ArrayRef<StringRef> Components,
165+
bool IncludeNonInstalled, bool GetComponentNames,
166+
const std::function<std::string(const StringRef &)>
167+
*GetComponentLibraryPath,
168+
std::vector<std::string> *Missing,
169+
const std::string &DirSep) {
167170
std::vector<std::string> RequiredLibs;
168171
std::set<AvailableComponent *> VisitedComponents;
169172

@@ -173,18 +176,17 @@ static std::vector<std::string> ComputeLibsForComponents(
173176
ComponentMap[AC.Name] = &AC;
174177

175178
// Visit the components.
176-
for (unsigned i = 0, e = Components.size(); i != e; ++i) {
179+
for (StringRef Component : Components) {
177180
// Users are allowed to provide mixed case component names.
178-
std::string ComponentLower = Components[i].lower();
181+
std::string ComponentLower = Component.lower();
179182

180183
// Validate that the user supplied a valid component name.
181184
if (!ComponentMap.count(ComponentLower)) {
182-
llvm::errs() << "llvm-config: unknown component name: " << Components[i]
183-
<< "\n";
185+
errs() << "llvm-config: unknown component name: " << Component << "\n";
184186
exit(1);
185187
}
186188

187-
VisitComponent(ComponentLower, ComponentMap, VisitedComponents,
189+
visitComponent(ComponentLower, ComponentMap, VisitedComponents,
188190
RequiredLibs, IncludeNonInstalled, GetComponentNames,
189191
GetComponentLibraryPath, Missing, DirSep);
190192
}
@@ -196,8 +198,6 @@ static std::vector<std::string> ComputeLibsForComponents(
196198
return RequiredLibs;
197199
}
198200

199-
/* *** */
200-
201201
static void usage(bool ExitWithFailure = true) {
202202
errs() << "\
203203
usage: llvm-config <OPTION>... [<COMPONENT>...]\n\
@@ -244,34 +244,33 @@ Typical components:\n\
244244
}
245245

246246
/// Compute the path to the main executable.
247-
std::string GetExecutablePath(const char *Argv0) {
247+
static std::string getExecutablePath(const char *Argv0) {
248248
// This just needs to be some symbol in the binary; C++ doesn't
249249
// allow taking the address of ::main however.
250-
void *P = (void *)(intptr_t)GetExecutablePath;
251-
return llvm::sys::fs::getMainExecutable(Argv0, P);
250+
void *P = (void *)(intptr_t)getExecutablePath;
251+
return sys::fs::getMainExecutable(Argv0, P);
252252
}
253253

254254
/// Expand the semi-colon delimited LLVM_DYLIB_COMPONENTS into
255255
/// the full list of components.
256-
std::vector<std::string> GetAllDyLibComponents(const bool IsInDevelopmentTree,
257-
const bool GetComponentNames,
258-
const std::string &DirSep) {
256+
static std::vector<std::string>
257+
getAllDyLibComponents(const bool IsInDevelopmentTree,
258+
const bool GetComponentNames, const std::string &DirSep) {
259259
std::vector<StringRef> DyLibComponents;
260260

261261
StringRef DyLibComponentsStr(LLVM_DYLIB_COMPONENTS);
262262
size_t Offset = 0;
263263
while (true) {
264264
const size_t NextOffset = DyLibComponentsStr.find(';', Offset);
265265
DyLibComponents.push_back(DyLibComponentsStr.substr(Offset, NextOffset-Offset));
266-
if (NextOffset == std::string::npos) {
266+
if (NextOffset == std::string::npos)
267267
break;
268-
}
269268
Offset = NextOffset + 1;
270269
}
271270

272271
assert(!DyLibComponents.empty());
273272

274-
return ComputeLibsForComponents(DyLibComponents,
273+
return computeLibsForComponents(DyLibComponents,
275274
/*IncludeNonInstalled=*/IsInDevelopmentTree,
276275
GetComponentNames, nullptr, nullptr, DirSep);
277276
}
@@ -288,7 +287,7 @@ int main(int argc, char **argv) {
288287
// tree.
289288
bool IsInDevelopmentTree;
290289
enum { CMakeStyle, CMakeBuildModeStyle } DevelopmentTreeLayout;
291-
llvm::SmallString<256> CurrentPath(GetExecutablePath(argv[0]));
290+
SmallString<256> CurrentPath(getExecutablePath(argv[0]));
292291
std::string CurrentExecPrefix;
293292
std::string ActiveObjRoot;
294293

@@ -453,13 +452,12 @@ int main(int argc, char **argv) {
453452
StringRef &Out) {
454453
if (Lib.starts_with(StaticPrefix) || Lib.starts_with(SharedPrefix)) {
455454
unsigned FromEnd;
456-
if (Lib.ends_with(StaticExt)) {
455+
if (Lib.ends_with(StaticExt))
457456
FromEnd = StaticExt.size() + 1;
458-
} else if (Lib.ends_with(SharedExt)) {
457+
else if (Lib.ends_with(SharedExt))
459458
FromEnd = SharedExt.size() + 1;
460-
} else {
459+
else
461460
FromEnd = 0;
462-
}
463461

464462
if (FromEnd != 0) {
465463
unsigned FromStart = Lib.starts_with(SharedPrefix)
@@ -496,11 +494,10 @@ int main(int argc, char **argv) {
496494
/// Get the full path for a possibly shared component library.
497495
auto GetComponentLibraryPath = [&](const StringRef &Name, const bool Shared) {
498496
auto LibFileName = GetComponentLibraryFileName(Name, Shared);
499-
if (Shared) {
497+
if (Shared)
500498
return (SharedDir + DirSep + LibFileName).str();
501-
} else {
499+
else
502500
return (StaticDir + DirSep + LibFileName).str();
503-
}
504501
};
505502

506503
raw_ostream &OS = outs();
@@ -555,20 +552,14 @@ int main(int argc, char **argv) {
555552
llvm::replace(path, '/', '\\');
556553
if (DyLibExists && !sys::fs::exists(path)) {
557554
Components =
558-
GetAllDyLibComponents(IsInDevelopmentTree, true, DirSep);
555+
getAllDyLibComponents(IsInDevelopmentTree, true, DirSep);
559556
llvm::sort(Components);
560557
break;
561558
}
562559
}
563560
}
564561

565-
for (unsigned I = 0; I < Components.size(); ++I) {
566-
if (I) {
567-
OS << ' ';
568-
}
569-
570-
OS << Components[I];
571-
}
562+
interleave(Components, OS, " ");
572563
OS << '\n';
573564
} else if (Arg == "--targets-built") {
574565
OS << LLVM_TARGETS_BUILT << '\n';
@@ -633,7 +624,7 @@ int main(int argc, char **argv) {
633624
return GetComponentLibraryPath(Name, LinkMode == LinkModeShared);
634625
};
635626
std::vector<std::string> MissingLibs;
636-
std::vector<std::string> RequiredLibs = ComputeLibsForComponents(
627+
std::vector<std::string> RequiredLibs = computeLibsForComponents(
637628
Components,
638629
/*IncludeNonInstalled=*/IsInDevelopmentTree, false,
639630
&GetComponentLibraryPathFunction, &MissingLibs, DirSep);
@@ -666,11 +657,10 @@ int main(int argc, char **argv) {
666657
if (PrintSharedMode) {
667658
std::unordered_set<std::string> FullDyLibComponents;
668659
std::vector<std::string> DyLibComponents =
669-
GetAllDyLibComponents(IsInDevelopmentTree, false, DirSep);
660+
getAllDyLibComponents(IsInDevelopmentTree, false, DirSep);
670661

671-
for (auto &Component : DyLibComponents) {
662+
for (auto &Component : DyLibComponents)
672663
FullDyLibComponents.insert(Component);
673-
}
674664
DyLibComponents.clear();
675665

676666
for (auto &Lib : RequiredLibs) {
@@ -681,13 +671,11 @@ int main(int argc, char **argv) {
681671
}
682672
FullDyLibComponents.clear();
683673

684-
if (LinkMode == LinkModeShared) {
674+
if (LinkMode == LinkModeShared)
685675
OS << "shared\n";
686-
return 0;
687-
} else {
676+
else
688677
OS << "static\n";
689-
return 0;
690-
}
678+
return 0;
691679
}
692680

693681
if (PrintLibs || PrintLibNames || PrintLibFiles) {
@@ -716,17 +704,10 @@ int main(int argc, char **argv) {
716704
}
717705
};
718706

719-
if (LinkMode == LinkModeShared && LinkDyLib) {
707+
if (LinkMode == LinkModeShared && LinkDyLib)
720708
PrintForLib(DyLibName);
721-
} else {
722-
for (unsigned i = 0, e = RequiredLibs.size(); i != e; ++i) {
723-
auto Lib = RequiredLibs[i];
724-
if (i)
725-
OS << ' ';
726-
727-
PrintForLib(Lib);
728-
}
729-
}
709+
else
710+
interleave(RequiredLibs, OS, PrintForLib, " ");
730711
OS << '\n';
731712
}
732713

0 commit comments

Comments
 (0)