1
- // ===--- SILNM .cpp ------ --------------------------------------------------===//
1
+ // ===--- sil_nm_main .cpp --------------------------------------------------===//
2
2
//
3
3
// This source file is part of the Swift.org open source project
4
4
//
46
46
47
47
using namespace swift ;
48
48
49
- static llvm::cl::opt<std::string> InputFilename (llvm::cl::desc(" input file" ),
50
- llvm::cl::init(" -" ),
51
- llvm::cl::Positional);
52
-
53
- static llvm::cl::list<std::string>
54
- ImportPaths (" I" ,
55
- llvm::cl::desc (" add a directory to the import search path" ));
56
-
57
- static llvm::cl::opt<std::string>
58
- ModuleName (" module-name" ,
59
- llvm::cl::desc (" The name of the module if processing"
60
- " a module. Necessary for processing "
61
- " stdin." ));
62
-
63
- static llvm::cl::opt<bool >
64
- DemangleNames (" demangle" ,
65
- llvm::cl::desc (" Demangle names of entities outputted" ));
66
-
67
- static llvm::cl::opt<std::string>
68
- ModuleCachePath (" module-cache-path" ,
69
- llvm::cl::desc (" Clang module cache path" ));
70
-
71
- static llvm::cl::opt<std::string> ResourceDir (
72
- " resource-dir" ,
73
- llvm::cl::desc (" The directory that holds the compiler resource files" ));
74
-
75
- static llvm::cl::opt<std::string>
76
- SDKPath (" sdk" , llvm::cl::desc(" The path to the SDK for use with the clang "
77
- " importer." ),
78
- llvm::cl::init(" " ));
79
-
80
- static llvm::cl::opt<std::string> Triple (" target" ,
81
- llvm::cl::desc (" target triple" ));
82
-
83
- // This function isn't referenced outside its translation unit, but it
84
- // can't use the "static" keyword because its address is used for
85
- // getMainExecutable (since some platforms don't support taking the
86
- // address of main, and some platforms can't implement getMainExecutable
87
- // without being given the address of a function in the main executable).
88
- void anchorForGetMainExecutable () {}
89
-
90
- static void printAndSortNames (std::vector<StringRef> &Names, char Code) {
49
+ struct SILNMOptions {
50
+ llvm::cl::opt<std::string>
51
+ InputFilename = llvm::cl::opt<std::string>(llvm::cl::desc(" input file" ),
52
+ llvm::cl::init (" -" ),
53
+ llvm::cl::Positional);
54
+
55
+ llvm::cl::list<std::string>
56
+ ImportPaths = llvm::cl::list<std::string>(" I" ,
57
+ llvm::cl::desc (" add a directory to the import search path" ));
58
+
59
+ llvm::cl::opt<std::string>
60
+ ModuleName = llvm::cl::opt<std::string>(" module-name" ,
61
+ llvm::cl::desc (" The name of the module if processing"
62
+ " a module. Necessary for processing "
63
+ " stdin." ));
64
+
65
+ llvm::cl::opt<bool >
66
+ DemangleNames = llvm::cl::opt<bool >(" demangle" ,
67
+ llvm::cl::desc (" Demangle names of entities outputted" ));
68
+
69
+ llvm::cl::opt<std::string>
70
+ ModuleCachePath = llvm::cl::opt<std::string>(" module-cache-path" ,
71
+ llvm::cl::desc (" Clang module cache path" ));
72
+
73
+ llvm::cl::opt<std::string>
74
+ ResourceDir = llvm::cl::opt<std::string>(
75
+ " resource-dir" ,
76
+ llvm::cl::desc (" The directory that holds the compiler resource files" ));
77
+
78
+ llvm::cl::opt<std::string>
79
+ SDKPath = llvm::cl::opt<std::string>(" sdk" , llvm::cl::desc(" The path to the SDK for use with the clang "
80
+ " importer." ),
81
+ llvm::cl::init (" " ));
82
+
83
+ llvm::cl::opt<std::string>
84
+ Triple = llvm::cl::opt<std::string>(" target" , llvm::cl::desc(" target triple" ));
85
+ };
86
+
87
+ static void printAndSortNames (std::vector<StringRef> &Names, char Code,
88
+ const SILNMOptions &options) {
91
89
std::sort (Names.begin (), Names.end ());
92
90
for (StringRef N : Names) {
93
91
llvm::outs () << Code << " " ;
94
- if (DemangleNames) {
92
+ if (options. DemangleNames ) {
95
93
llvm::outs () << swift::Demangle::demangleSymbolAsString (N);
96
94
} else {
97
95
llvm::outs () << N;
@@ -100,77 +98,78 @@ static void printAndSortNames(std::vector<StringRef> &Names, char Code) {
100
98
}
101
99
}
102
100
103
- static void nmModule (SILModule *M) {
101
+ static void nmModule (SILModule *M, const SILNMOptions &options ) {
104
102
{
105
103
std::vector<StringRef> FuncNames;
106
- llvm::transform (*M, std::back_inserter (FuncNames),
107
- std::mem_fn (&SILFunction::getName));
108
- printAndSortNames (FuncNames, ' F' );
104
+ for (SILFunction &f : *M) {
105
+ FuncNames.push_back (f.getName ());
106
+ }
107
+ printAndSortNames (FuncNames, ' F' , options);
109
108
}
110
109
111
110
{
112
111
std::vector<StringRef> GlobalNames;
113
- llvm::transform (M->getSILGlobals (), std::back_inserter (GlobalNames),
114
- std::mem_fn (&SILGlobalVariable::getName));
115
- printAndSortNames (GlobalNames, ' G' );
112
+ for (SILGlobalVariable &g : M->getSILGlobals ()) {
113
+ GlobalNames.push_back (g.getName ());
114
+ }
115
+ printAndSortNames (GlobalNames, ' G' , options);
116
116
}
117
117
118
118
{
119
119
std::vector<StringRef> WitnessTableNames;
120
- llvm::transform ( M->getWitnessTables (),
121
- std::back_inserter ( WitnessTableNames),
122
- std::mem_fn (&SILWitnessTable::getName));
123
- printAndSortNames (WitnessTableNames, ' W' );
120
+ for (SILWitnessTable &wt : M->getWitnessTables ()) {
121
+ WitnessTableNames. push_back (wt. getName ());
122
+ }
123
+ printAndSortNames (WitnessTableNames, ' W' , options );
124
124
}
125
125
126
126
{
127
127
std::vector<StringRef> VTableNames;
128
- llvm::transform (M->getVTables (), std::back_inserter (VTableNames),
129
- [](const SILVTable *VT) -> StringRef {
130
- return VT->getClass ()->getName ().str ();
131
- });
132
- printAndSortNames (VTableNames, ' V' );
128
+ for (SILVTable *vt : M->getVTables ()) {
129
+ VTableNames.push_back (vt->getClass ()->getName ().str ());
130
+ }
131
+ printAndSortNames (VTableNames, ' V' , options);
133
132
}
134
133
}
135
134
136
- int main (int argc, char **argv) {
137
- PROGRAM_START (argc, argv);
135
+ int sil_nm_main (ArrayRef<const char *> argv, void *MainAddr) {
138
136
INITIALIZE_LLVM ();
139
137
140
- llvm::cl::ParseCommandLineOptions (argc, argv, " SIL NM\n " );
138
+ SILNMOptions options;
139
+
140
+ llvm::cl::ParseCommandLineOptions (argv.size (), argv.data (), " SIL NM\n " );
141
141
142
142
CompilerInvocation Invocation;
143
143
144
- Invocation.setMainExecutablePath (llvm::sys::fs::getMainExecutable (
145
- argv[0 ], reinterpret_cast <void *>(&anchorForGetMainExecutable)));
144
+ Invocation.setMainExecutablePath (llvm::sys::fs::getMainExecutable (argv[0 ], MainAddr));
146
145
147
146
// Give the context the list of search paths to use for modules.
148
- Invocation.setImportSearchPaths (ImportPaths);
147
+ Invocation.setImportSearchPaths (options. ImportPaths );
149
148
// Set the SDK path and target if given.
150
- if (SDKPath.getNumOccurrences () == 0 ) {
149
+ if (options. SDKPath .getNumOccurrences () == 0 ) {
151
150
const char *SDKROOT = getenv (" SDKROOT" );
152
151
if (SDKROOT)
153
- SDKPath = SDKROOT;
152
+ options. SDKPath = SDKROOT;
154
153
}
155
- if (!SDKPath.empty ())
156
- Invocation.setSDKPath (SDKPath);
157
- if (!Triple.empty ())
158
- Invocation.setTargetTriple (Triple);
159
- if (!ResourceDir.empty ())
160
- Invocation.setRuntimeResourcePath (ResourceDir);
161
- Invocation.getClangImporterOptions ().ModuleCachePath = ModuleCachePath;
154
+ if (!options. SDKPath .empty ())
155
+ Invocation.setSDKPath (options. SDKPath );
156
+ if (!options. Triple .empty ())
157
+ Invocation.setTargetTriple (options. Triple );
158
+ if (!options. ResourceDir .empty ())
159
+ Invocation.setRuntimeResourcePath (options. ResourceDir );
160
+ Invocation.getClangImporterOptions ().ModuleCachePath = options. ModuleCachePath ;
162
161
Invocation.setParseStdlib ();
163
162
Invocation.getLangOptions ().DisableAvailabilityChecking = true ;
164
163
Invocation.getLangOptions ().EnableAccessControl = false ;
165
164
Invocation.getLangOptions ().EnableObjCAttrRequiresFoundation = false ;
166
165
167
166
serialization::ExtendedValidationInfo extendedInfo;
168
167
llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> FileBufOrErr =
169
- Invocation.setUpInputForSILTool (InputFilename, ModuleName,
168
+ Invocation.setUpInputForSILTool (options. InputFilename , options. ModuleName ,
170
169
/* alwaysSetModuleToMain*/ true ,
171
170
/* bePrimary*/ false , extendedInfo);
172
171
if (!FileBufOrErr) {
173
- fprintf (stderr, " Error! Failed to open file: %s\n " , InputFilename.c_str ());
172
+ fprintf (stderr, " Error! Failed to open file: %s\n " , options. InputFilename .c_str ());
174
173
exit (-1 );
175
174
}
176
175
@@ -191,7 +190,7 @@ int main(int argc, char **argv) {
191
190
192
191
auto SILMod = performASTLowering (CI.getMainModule (), CI.getSILTypes (),
193
192
CI.getSILOptions ());
194
- nmModule (SILMod.get ());
193
+ nmModule (SILMod.get (), options );
195
194
196
195
return CI.getASTContext ().hadError ();
197
196
}
0 commit comments