Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_no_added_attr():
src = """
char* foo() {
return "";
}
int main() {
return 0;
}
"""
import pycparserext.ext_c_parser as ext_c_parser
import pycparserext.ext_c_generator as ext_c_generator
parser = ext_c_parser.GnuCParser()
ast = parser.parse(src)
gen = ext_c_generator.GnuCGenerator()
print(gen.visit(ast))
assert "attr" not in gen.visit(ast)
def visit_FuncDeclExt(self, node):
visits['FuncDeclExt'][0] += 1
NodeVisitor.generic_visit(self, node)
src_gnu = """
int func1(int a, int b) {
__typeof__(a) _a = __builtin_types_compatible_p(long char, short int);
__typeof__ (__typeof__ (char *)[4]) y;
asm("rdtsc" : "=A" (val));
__attribute__((unused)) static int c;
}
"""
import pycparserext.ext_c_parser as ext_c_parser
parser = ext_c_parser.GnuCParser()
ast = parser.parse(src_gnu)
ast.show()
TestVisitor().visit(ast)
for visit_type, visit_num in visits.items():
assert_msg = '{}: Should have visited {}, got {}'.format(
visit_type, visit_num[1], visit_num[0])
assert visit_num[0] == visit_num[1], assert_msg
def test_lvalue_gnu_statement_expression():
src = """
int func(int a) {
int ret=(int)({; ; *(int*)&a;});
return ret;
}
"""
from pycparserext.ext_c_parser import GnuCParser
p = GnuCParser()
ast = p.parse(src)
ast.show()
from pycparserext.ext_c_generator import GnuCGenerator
print(GnuCGenerator().visit(ast))
def test_funky_header_code_2():
src = """
extern __inline int __attribute__ ((__nothrow__)) __signbitf (float __x)
{
int __m;
if (__x == 0)
__asm ("pmovmskb %1, %0" : "=r" (__m) : "x" (__x));
else
__asm ("pmovmskb %1, %0" : "=r" (__m) : "x" (__x));
return __m & 0x8;
}
"""
from pycparserext.ext_c_parser import GnuCParser
p = GnuCParser()
ast = p.parse(src)
ast.show()
from pycparserext.ext_c_generator import GnuCGenerator
print(GnuCGenerator().visit(ast))
def test_double_pointer():
src = """
typedef struct Error {
int dummy;
} Error;
void func_with_p2pp(const char *, Error **);
"""
import pycparserext.ext_c_parser as ext_c_parser
import pycparserext.ext_c_generator as ext_c_generator
parser = ext_c_parser.GnuCParser()
ast = parser.parse(src)
gen = ext_c_generator.GnuCGenerator()
ast.show()
assert gen.visit(ast).find("func_with_p2pp(const char *, Error **)") != -1
def test_asm_volatile_1():
src = """
void read_tsc(void) {
long val;
asm("rdtsc" : "=A" (val));
} """
from pycparserext.ext_c_parser import GnuCParser
p = GnuCParser()
ast = p.parse(src)
ast.show()
from pycparserext.ext_c_generator import GnuCGenerator
print(GnuCGenerator().visit(ast))
def test_funky_header_code_5():
src = """ void do_foo(void) __asm(__STRING(do_foo));"""
from pycparserext.ext_c_parser import GnuCParser
p = GnuCParser()
ast = p.parse(src)
ast.show()
from pycparserext.ext_c_generator import GnuCGenerator
print(GnuCGenerator().visit(ast))
int __m;
if (__x == 0)
__asm ("pmovmskb %1, %0" : "=r" (__m) : "x" (__x));
else
__asm ("pmovmskb %1, %0" : "=r" (__m) : "x" (__x));
return __m & 0x8;
}
"""
from pycparserext.ext_c_parser import GnuCParser
p = GnuCParser()
ast = p.parse(src)
ast.show()
from pycparserext.ext_c_generator import GnuCGenerator
print(GnuCGenerator().visit(ast))
def test_no_added_attr():
src = """
char* foo() {
return "";
}
int main() {
return 0;
}
"""
import pycparserext.ext_c_parser as ext_c_parser
import pycparserext.ext_c_generator as ext_c_generator
parser = ext_c_parser.GnuCParser()
ast = parser.parse(src)
gen = ext_c_generator.GnuCGenerator()
print(gen.visit(ast))
assert "attr" not in gen.visit(ast)
def test_array_ptr_decl_attribute():
src = """
int* __attribute__((weak)) array[256];
"""
from pycparserext.ext_c_parser import GnuCParser
p = GnuCParser()
ast = p.parse(src)
ast.show()
from pycparserext.ext_c_generator import GnuCGenerator
print(GnuCGenerator().visit(ast))