예제 #1
0
 def get_macros(self, text):
     if text is None:
         return
     text = "".join(text)
     # preprocessor definitions that look like macros with one or more arguments
     for m in text.splitlines():
         name, body = m.split(None, 1)
         name, args = name.split("(", 1)
         args = "(%s" % args
         self.all[name] = typedesc.Macro(name, args, body)
예제 #2
0
 def MACRO_DEFINITION(self, cursor):
     """
     Parse MACRO_DEFINITION, only present if the TranslationUnit is
     used with TranslationUnit.PARSE_DETAILED_PROCESSING_RECORD.
     """
     # TODO: optionalize macro parsing. It takes a LOT of time.
     # ignore system macro
     if (not hasattr(cursor, 'location') or cursor.location is None or
             cursor.location.file is None):
         return False
     name = self.get_unique_name(cursor)
     # if name == 'A':
     #    code.interact(local=locals())
     # Tokens !!! .kind = {IDENTIFIER, KEYWORD, LITERAL, PUNCTUATION,
     # COMMENT ? } etc. see TokenKinds.def
     comment = None
     tokens = self._literal_handling(cursor)
     # Macro name is tokens[0]
     # get Macro value(s)
     value = True
     if isinstance(tokens, list):
         if len(tokens) == 2:
             value = tokens[1]
         else:
             # just merge the list of tokens
             value = ''.join(tokens[1:])
     # macro comment maybe in tokens. Not in cursor.raw_comment
     for t in cursor.get_tokens():
         if t.kind == TokenKind.COMMENT:
             comment = t.spelling
     # special case. internal __null
     # FIXME, there are probable a lot of others.
     # why not Cursor.kind GNU_NULL_EXPR child instead of a token ?
     if name == 'NULL' or value == '__null':
         value = None
     log.debug('MACRO: #define %s %s', tokens[0], value)
     obj = typedesc.Macro(name, None, value)
     try:
         self.register(name, obj)
     except DuplicateDefinitionException:
         log.info(
             'Redefinition of %s %s->%s',
             name, self.parser.all[name].args, value)
         # HACK
         self.parser.all[name] = obj
     self.set_location(obj, cursor)
     # set the comment in the obj
     obj.comment = comment
     return True