Skip to content

Commit 4120b97

Browse files
authored
Escape '{' '}' brackets in original command line (#170)
It's possible the original command line has brackets that otherwise python string formatting will interpret as part of the worker contextualization formatting (i.e. LoadedModuleSpec.build_command_line). We need to escape such brackets upfront when loading the command line.
1 parent 65e1ec3 commit 4120b97

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

compiler_opt/rl/corpus.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,8 @@ def get_cmdline(name: str):
315315
ret = cmd_override
316316
else:
317317
with tf.io.gfile.GFile(os.path.join(data_path, name + '.cmd')) as f:
318-
ret = tuple(f.read().split('\0'))
318+
ret = tuple(f.read().replace(r'{', r'{{').replace(r'}',
319+
r'}}').split('\0'))
319320
# The options read from a .cmd file must be run with -cc1
320321
if ret[0] != '-cc1':
321322
raise ValueError('-cc1 flag not present in .cmd file.')

compiler_opt/rl/corpus_test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,21 @@ def test_ctor_thinlto(self):
178178
('-x', 'ir', '{context.module_full_path}', '-mllvm',
179179
'-thinlto-assume-merged'))
180180

181+
def test_braces_in_cmd(self):
182+
corpusdir = self.create_tempdir()
183+
cps = corpus.create_corpus_for_testing(
184+
location=corpusdir,
185+
elements=[corpus.ModuleSpec(name='somename', size=1)],
186+
cmdline=('-cc1', r'-DMACRO(expr)=do {} while(0)'),
187+
additional_flags=('-additional_flag={context.module_full_path}',))
188+
mod_spec = cps.module_specs[0]
189+
loaded_spec = cps.load_module_spec(mod_spec)
190+
final_cmdline = loaded_spec.build_command_line('some/temp/dir')
191+
self.assertEqual(final_cmdline,
192+
('-cc1', r'-DMACRO(expr)=do {} while(0)', '-x', 'ir',
193+
'some/temp/dir/somename/input.bc',
194+
'-additional_flag=some/temp/dir/somename/input.bc'))
195+
181196
def test_cmd_override_thinlto(self):
182197
cps = corpus.create_corpus_for_testing(
183198
location=self.create_tempdir(),

0 commit comments

Comments
 (0)