1*67e74705SXin Li# This file provides common utility functions for the test suite. 2*67e74705SXin Li 3*67e74705SXin Lifrom clang.cindex import Cursor 4*67e74705SXin Lifrom clang.cindex import TranslationUnit 5*67e74705SXin Li 6*67e74705SXin Lidef get_tu(source, lang='c', all_warnings=False, flags=[]): 7*67e74705SXin Li """Obtain a translation unit from source and language. 8*67e74705SXin Li 9*67e74705SXin Li By default, the translation unit is created from source file "t.<ext>" 10*67e74705SXin Li where <ext> is the default file extension for the specified language. By 11*67e74705SXin Li default it is C, so "t.c" is the default file name. 12*67e74705SXin Li 13*67e74705SXin Li Supported languages are {c, cpp, objc}. 14*67e74705SXin Li 15*67e74705SXin Li all_warnings is a convenience argument to enable all compiler warnings. 16*67e74705SXin Li """ 17*67e74705SXin Li args = list(flags) 18*67e74705SXin Li name = 't.c' 19*67e74705SXin Li if lang == 'cpp': 20*67e74705SXin Li name = 't.cpp' 21*67e74705SXin Li args.append('-std=c++11') 22*67e74705SXin Li elif lang == 'objc': 23*67e74705SXin Li name = 't.m' 24*67e74705SXin Li elif lang != 'c': 25*67e74705SXin Li raise Exception('Unknown language: %s' % lang) 26*67e74705SXin Li 27*67e74705SXin Li if all_warnings: 28*67e74705SXin Li args += ['-Wall', '-Wextra'] 29*67e74705SXin Li 30*67e74705SXin Li return TranslationUnit.from_source(name, args, unsaved_files=[(name, 31*67e74705SXin Li source)]) 32*67e74705SXin Li 33*67e74705SXin Lidef get_cursor(source, spelling): 34*67e74705SXin Li """Obtain a cursor from a source object. 35*67e74705SXin Li 36*67e74705SXin Li This provides a convenient search mechanism to find a cursor with specific 37*67e74705SXin Li spelling within a source. The first argument can be either a 38*67e74705SXin Li TranslationUnit or Cursor instance. 39*67e74705SXin Li 40*67e74705SXin Li If the cursor is not found, None is returned. 41*67e74705SXin Li """ 42*67e74705SXin Li # Convenience for calling on a TU. 43*67e74705SXin Li root_cursor = source if isinstance(source, Cursor) else source.cursor 44*67e74705SXin Li 45*67e74705SXin Li for cursor in root_cursor.walk_preorder(): 46*67e74705SXin Li if cursor.spelling == spelling: 47*67e74705SXin Li return cursor 48*67e74705SXin Li 49*67e74705SXin Li return None 50*67e74705SXin Li 51*67e74705SXin Lidef get_cursors(source, spelling): 52*67e74705SXin Li """Obtain all cursors from a source object with a specific spelling. 53*67e74705SXin Li 54*67e74705SXin Li This provides a convenient search mechanism to find all cursors with 55*67e74705SXin Li specific spelling within a source. The first argument can be either a 56*67e74705SXin Li TranslationUnit or Cursor instance. 57*67e74705SXin Li 58*67e74705SXin Li If no cursors are found, an empty list is returned. 59*67e74705SXin Li """ 60*67e74705SXin Li # Convenience for calling on a TU. 61*67e74705SXin Li root_cursor = source if isinstance(source, Cursor) else source.cursor 62*67e74705SXin Li 63*67e74705SXin Li cursors = [] 64*67e74705SXin Li for cursor in root_cursor.walk_preorder(): 65*67e74705SXin Li if cursor.spelling == spelling: 66*67e74705SXin Li cursors.append(cursor) 67*67e74705SXin Li 68*67e74705SXin Li return cursors 69*67e74705SXin Li 70*67e74705SXin Li 71*67e74705SXin Li__all__ = [ 72*67e74705SXin Li 'get_cursor', 73*67e74705SXin Li 'get_cursors', 74*67e74705SXin Li 'get_tu', 75*67e74705SXin Li] 76