Skip to content

Commit

Permalink
Merge pull request #313 from afbjorklund/macros
Browse files Browse the repository at this point in the history
Handle complex arguments, like assembler macros
  • Loading branch information
shawnl committed May 23, 2019
2 parents 18efcaf + 0caa4b0 commit 037051f
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
30 changes: 30 additions & 0 deletions include_server/parse_command.py
Expand Up @@ -119,6 +119,22 @@ def _RaiseNotImplemented(name, comment=''):
for key in CPP_OPTIONS_MAYBE_TWO_WORDS.keys():
assert key[1] in CPP_OPTIONS_MAYBE_TWO_WORDS_FIRST_LETTERS

PATH_EXPR='[/a-zA-Z_0-9.]+' # regular expression for a partial file path

# These are the cpp options that require regular expressions, m is Match.
CPP_OPTIONS_REGULAR_EXPRESSIONS = {
'-Wa,(%s\.s)' % PATH_EXPR: lambda ps, m: ps.include_files.append(m.group(1)),
'-Wa,\[(%s\.s)\]' % PATH_EXPR: lambda ps, m: ps.include_files.append(m.group(1)),
}

CPP_OPTIONS_REGULAR_EXPRESSIONS_STARTS_WITH = '-Wa,'
for key in CPP_OPTIONS_REGULAR_EXPRESSIONS.keys():
assert key.startswith(CPP_OPTIONS_REGULAR_EXPRESSIONS_STARTS_WITH)

CPP_OPTIONS_REGULAR_EXPRESSIONS_COMPILED = {}
for key in CPP_OPTIONS_REGULAR_EXPRESSIONS.keys():
CPP_OPTIONS_REGULAR_EXPRESSIONS_COMPILED[key] = re.compile(key)

# These are the cpp options that a) are more than one letter long,
# b) always take an argument, and c) must have that argument as a
# separate word in argv.
Expand Down Expand Up @@ -415,6 +431,20 @@ def ParseCommandArgs(args, current_dir, includepath_map, dir_map,
if found_action: # what we really need here is a goto!
continue

# Deal with the complex options requiring regular expressions last.
if args[i].startswith(CPP_OPTIONS_REGULAR_EXPRESSIONS_STARTS_WITH):
found_action = False
for (option, action) in CPP_OPTIONS_REGULAR_EXPRESSIONS.items():
r = CPP_OPTIONS_REGULAR_EXPRESSIONS_COMPILED[option]
m = r.match(args[i])
if action and m is not None:
action(parse_state, m)
i += 1
found_action = True
break
if found_action:
continue

# Whatever is left must be a one-word option (that is, an option
# without an arg) that it's safe to ignore.
i += 1
Expand Down
7 changes: 6 additions & 1 deletion include_server/parse_command_test.py
Expand Up @@ -133,6 +133,10 @@ def test_ParseCommandArgs(self):
+ " -isystem system -Imice -iquote/and -I/men a.c "
+ " -include included_A.h "
+ " -includeincluded_B.h "
+ " -Wa,macros_A.s "
+ " -Wa,[macros_B.s] "
+ " -Wa,arch/x86/kernel/macros.s -Wa,- "
+ " -Wa,other_directive "
+ "-Xlinker W,l -L /ignored_by_us -o a.o"),
os.getcwd(),
self.includepath_map,
Expand All @@ -146,7 +150,8 @@ def test_ParseCommandArgs(self):
filepath),
(('/and', 'mice', '/men', 'system'),
('mice', '/men', 'system'),
["included_A.h", "included_B.h"],
["included_A.h", "included_B.h",
"macros_A.s", "macros_B.s", "arch/x86/kernel/macros.s"],
'a.c'))


Expand Down

0 comments on commit 037051f

Please sign in to comment.