Skip to content

Commit ff15f5e

Browse files
committed
Rename Compiler and CompilerConfiguration classes
While sketching out documentation and other functionality, it became clear that the names of these classes were pretty unclear. For example, an instance of the "Compiler" class couldn't actually compile anything. The new names are much clearer, and in keeping with what the classes do and how they're used within CBI: - An ArgumentParser parses arguments, and may be configured to emulate the behavior of specific compilers. - A PreprocessorConfiguration describes the configuration of CBI's preprocessor. Signed-off-by: John Pennycook <john.pennycook@intel.com>
1 parent 3a46d7e commit ff15f5e

File tree

2 files changed

+47
-47
lines changed

2 files changed

+47
-47
lines changed

codebasin/config.py

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def __call__(
152152

153153

154154
@dataclass
155-
class CompilerConfiguration:
155+
class PreprocessorConfiguration:
156156
"""
157157
Represents the configuration for a specific file, including:
158158
- Macro definitions
@@ -167,7 +167,7 @@ class CompilerConfiguration:
167167
pass_name: str = "default"
168168

169169

170-
class Compiler:
170+
class ArgumentParser:
171171
"""
172172
Represents the behavior of a specific compiler.
173173
"""
@@ -180,7 +180,7 @@ def __init__(self, path: str):
180180
if _importcfg is None:
181181
load_importcfg()
182182

183-
def parse_args(self, argv: list[str]) -> list[CompilerConfiguration]:
183+
def parse_args(self, argv: list[str]) -> list[PreprocessorConfiguration]:
184184
"""
185185
Parameters
186186
----------
@@ -189,21 +189,21 @@ def parse_args(self, argv: list[str]) -> list[CompilerConfiguration]:
189189
190190
Returns
191191
-------
192-
list[CompilerConfiguration]
192+
list[PreprocessorConfiguration]
193193
A list of compiler configurations, each representing
194194
a separate pass, that describe the compiler's behavior
195195
after parsing `argv`.
196196
"""
197197
args = _parse_compiler_args(argv + _importcfg[self.name])
198-
configuration = CompilerConfiguration(
198+
configuration = PreprocessorConfiguration(
199199
args.defines,
200200
args.include_paths,
201201
args.include_files,
202202
)
203203
return [configuration]
204204

205205

206-
class ClangCompiler(Compiler):
206+
class ClangArgumentParser(ArgumentParser):
207207
"""
208208
Represents the behavior of Clang-based compilers.
209209
"""
@@ -216,7 +216,7 @@ class ClangCompiler(Compiler):
216216
"spir64_fpga",
217217
]
218218

219-
def parse_args(self, argv: list[str]) -> list[CompilerConfiguration]:
219+
def parse_args(self, argv: list[str]) -> list[PreprocessorConfiguration]:
220220
args = _parse_compiler_args(argv + _importcfg[self.name])
221221

222222
sycl = False
@@ -252,7 +252,7 @@ def parse_args(self, argv: list[str]) -> list[CompilerConfiguration]:
252252
include_files = args.include_files.copy()
253253
include_paths = args.include_paths.copy()
254254

255-
if pass_ in ClangCompiler.device_passes:
255+
if pass_ in ClangArgumentParser.device_passes:
256256
defines.append("__SYCL_DEVICE_ONLY__")
257257

258258
if "spir64" in pass_ or pass_ == "x86_64":
@@ -262,7 +262,7 @@ def parse_args(self, argv: list[str]) -> list[CompilerConfiguration]:
262262
if pass_ == "default" and omp:
263263
defines.append("_OPENMP")
264264

265-
configuration = CompilerConfiguration(
265+
configuration = PreprocessorConfiguration(
266266
defines,
267267
include_paths,
268268
include_files,
@@ -274,30 +274,30 @@ def parse_args(self, argv: list[str]) -> list[CompilerConfiguration]:
274274
return configurations
275275

276276

277-
class GnuCompiler(Compiler):
277+
class GnuArgumentParser(ArgumentParser):
278278
"""
279279
Represents the behavior of GNU-based compilers.
280280
"""
281281

282-
def parse_args(self, argv: list[str]) -> list[CompilerConfiguration]:
282+
def parse_args(self, argv: list[str]) -> list[PreprocessorConfiguration]:
283283
args = _parse_compiler_args(argv + _importcfg[self.name])
284284
for arg in argv:
285285
if arg in ["-fopenmp"]:
286286
args.defines.append("_OPENMP")
287-
configuration = CompilerConfiguration(
287+
configuration = PreprocessorConfiguration(
288288
args.defines,
289289
args.include_paths,
290290
args.include_files,
291291
)
292292
return [configuration]
293293

294294

295-
class NvccCompiler(Compiler):
295+
class NvccArgumentParser(ArgumentParser):
296296
"""
297297
Represents the behavior of the NVCC compiler.
298298
"""
299299

300-
def parse_args(self, argv: list[str]) -> list[CompilerConfiguration]:
300+
def parse_args(self, argv: list[str]) -> list[PreprocessorConfiguration]:
301301
args = _parse_compiler_args(argv + _importcfg[self.name])
302302

303303
omp = False
@@ -328,7 +328,7 @@ def parse_args(self, argv: list[str]) -> list[CompilerConfiguration]:
328328
if pass_ == "default" and omp:
329329
defines.append("_OPENMP")
330330

331-
configuration = CompilerConfiguration(
331+
configuration = PreprocessorConfiguration(
332332
defines,
333333
include_paths,
334334
include_files,
@@ -343,34 +343,34 @@ def parse_args(self, argv: list[str]) -> list[CompilerConfiguration]:
343343
_seen_compiler = collections.defaultdict(lambda: False)
344344

345345

346-
def recognize_compiler(path: str) -> Compiler:
346+
def recognize_compiler(path: str) -> ArgumentParser:
347347
"""
348348
Attempt to recognize the compiler, given a path.
349-
Return a Compiler object.
349+
Return a ArgumentParser object.
350350
"""
351-
compiler = None
351+
parser = None
352352
compiler_name = os.path.basename(path)
353353
if compiler_name in ["clang", "clang++"]:
354-
compiler = ClangCompiler(path)
354+
parser = ClangArgumentParser(path)
355355
elif compiler_name in ["gcc", "g++"]:
356-
compiler = GnuCompiler(path)
356+
parser = GnuArgumentParser(path)
357357
elif compiler_name in ["icx", "icpx", "ifx"]:
358-
compiler = ClangCompiler(path)
358+
parser = ClangArgumentParser(path)
359359
elif compiler_name == "nvcc":
360-
compiler = NvccCompiler(path)
360+
parser = NvccArgumentParser(path)
361361
else:
362-
compiler = Compiler(path)
362+
parser = ArgumentParser(path)
363363

364364
if not _seen_compiler[compiler_name]:
365-
if compiler:
366-
log.info(f"Recognized compiler: {compiler.name}.")
365+
if parser:
366+
log.info(f"Recognized compiler: {compiler_name}.")
367367
else:
368368
log.warning(
369369
f"Unrecognized compiler: {compiler_name}. "
370370
+ "Some implicit behavior may be missed.",
371371
)
372372
_seen_compiler[compiler_name] = True
373-
return compiler
373+
return parser
374374

375375

376376
def load_database(dbpath, rootdir):
@@ -414,13 +414,13 @@ def load_database(dbpath, rootdir):
414414

415415
# Parse command-line arguments, emulating compiler-specific behavior.
416416
compiler_name = os.path.basename(command.arguments[0])
417-
compiler = recognize_compiler(compiler_name)
418-
compiler_configs = compiler.parse_args(command.arguments[1:])
417+
parser = recognize_compiler(compiler_name)
418+
preprocessor_configs = parser.parse_args(command.arguments[1:])
419419

420420
# Create a configuration entry for each compiler pass.
421421
# Each compiler pass may set different defines, etc.
422-
for compiler_config in compiler_configs:
423-
entry = asdict(compiler_config)
422+
for preprocessor_config in preprocessor_configs:
423+
entry = asdict(preprocessor_config)
424424

425425
entry["file"] = path
426426

tests/compilers/test_compilers.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ def test_gnu(self):
5757
"""compilers/gnu"""
5858
argv = ["g++", "-fopenmp", "test.cpp"]
5959

60-
compiler = config.recognize_compiler(argv[0])
61-
self.assertTrue(type(compiler) is config.GnuCompiler)
60+
parser = config.recognize_compiler(argv[0])
61+
self.assertTrue(type(parser) is config.GnuArgumentParser)
6262

63-
passes = compiler.parse_args(argv[1:])
63+
passes = parser.parse_args(argv[1:])
6464
self.assertEqual(len(passes), 1)
6565

6666
self.assertEqual(passes[0].pass_name, "default")
@@ -72,10 +72,10 @@ def test_clang(self):
7272
"""compilers/clang"""
7373
argv = ["clang", "-fsycl-is-device", "test.cpp"]
7474

75-
compiler = config.recognize_compiler(argv[0])
76-
self.assertTrue(type(compiler) is config.ClangCompiler)
75+
parser = config.recognize_compiler(argv[0])
76+
self.assertTrue(type(parser) is config.ClangArgumentParser)
7777

78-
passes = compiler.parse_args(argv[1:])
78+
passes = parser.parse_args(argv[1:])
7979
self.assertEqual(len(passes), 1)
8080

8181
self.assertEqual(passes[0].pass_name, "default")
@@ -87,10 +87,10 @@ def test_intel_sycl(self):
8787
"""compilers/intel_sycl"""
8888
argv = ["icpx", "-fsycl", "test.cpp"]
8989

90-
compiler = config.recognize_compiler(argv[0])
91-
self.assertTrue(type(compiler) is config.ClangCompiler)
90+
parser = config.recognize_compiler(argv[0])
91+
self.assertTrue(type(parser) is config.ClangArgumentParser)
9292

93-
passes = compiler.parse_args(argv[1:])
93+
passes = parser.parse_args(argv[1:])
9494
self.assertEqual(len(passes), 2)
9595

9696
pass_names = {p.pass_name for p in passes}
@@ -113,10 +113,10 @@ def test_intel_targets(self):
113113
"test.cpp",
114114
]
115115

116-
compiler = config.recognize_compiler(argv[0])
117-
self.assertTrue(type(compiler) is config.ClangCompiler)
116+
parser = config.recognize_compiler(argv[0])
117+
self.assertTrue(type(parser) is config.ClangArgumentParser)
118118

119-
passes = compiler.parse_args(argv[1:])
119+
passes = parser.parse_args(argv[1:])
120120

121121
pass_names = {p.pass_name for p in passes}
122122
self.assertEqual(pass_names, {"default", "spir64", "x86_64"})
@@ -139,10 +139,10 @@ def test_nvcc(self):
139139
"test.cpp",
140140
]
141141

142-
compiler = config.recognize_compiler(argv[0])
143-
self.assertTrue(type(compiler) is config.NvccCompiler)
142+
parser = config.recognize_compiler(argv[0])
143+
self.assertTrue(type(parser) is config.NvccArgumentParser)
144144

145-
passes = compiler.parse_args(argv[1:])
145+
passes = parser.parse_args(argv[1:])
146146

147147
pass_names = {p.pass_name for p in passes}
148148
self.assertEqual(pass_names, {"default", "50", "52"})
@@ -173,8 +173,8 @@ def test_user_options(self):
173173
"test.cpp",
174174
]
175175

176-
compiler = config.recognize_compiler(argv[0])
177-
passes = compiler.parse_args(argv[1:])
176+
parser = config.recognize_compiler(argv[0])
177+
passes = parser.parse_args(argv[1:])
178178
self.assertEqual(len(passes), 1)
179179
self.assertCountEqual(passes[0].defines, ["ASDF"])
180180

0 commit comments

Comments
 (0)