Defines in preprocessed files

Hi all,

I’m working on a project to use clang as a frontend to a model checking tool.

It’s almost done but I’m now facing a problem that I couldn’t find a solution: How to force the preprocessor to run in already preprocessed files?

For example, something as simples as:

int main(void)
{
int a = x();
}

When using clangTool to generate the AST, it works fine if add something like -Dx=x1. The generated AST looks like:

FunctionDecl 0x3a8c290 </home/mramalho/main.c:2:1, line:5:1> line:2:5 main ‘int (void)’
-CompoundStmt 0x3a8c5c0 <line:3:1, line:5:1> -DeclStmt 0x3a8c5a8 <line:4:3, col:14>
-VarDecl 0x3a8c3b8 <col:3, col:13> col:7 a 'int' cinit -CallExpr 0x3a8c580 <:6:11, /home/mramalho/main.c:4:13> ‘int’
-ImplicitCastExpr 0x3a8c568 <<command line>:6:11> 'int (*)()' <FunctionToPointerDecay> **-DeclRefExpr 0x3a8c510 col:11 ‘int ()’ Function 0x3a8c468 ‘x1’ ‘int ()’**

But if I preprocess the file:

$ clang -E main.c > main.c.i

1 “main.c”

1 “” 1

1 “” 3

316 “” 3

1 “” 1

1 “” 2

1 “main.c” 2

int main(void)
{
int a = x();
}

and run with the same define -Dx=x1, the AST is:

FunctionDecl 0x2e583a0 <main.c:2:1, line:5:1> line:2:5 main ‘int (void)’
-CompoundStmt 0x2e586d0 <line:3:1, line:5:1> -DeclStmt 0x2e586b8 <line:4:3, col:14>
-VarDecl 0x2e584c8 <col:3, col:13> col:7 a 'int' cinit -CallExpr 0x2e58690 <col:11, col:13> ‘int’
-ImplicitCastExpr 0x2e58678 <col:11> 'int (*)()' <FunctionToPointerDecay> **-DeclRefExpr 0x2e58620 col:11 ‘int ()’ Function 0x2e58578 ‘x’ ‘int ()’**

I guess the preprocessor is not running, given the following message I get when directly using clang:

$ clang -E -Dx=x1 main.c.i
clang: warning: main.c.i: previously preprocessed input
clang: warning: argument unused during compilation: ‘-D x=x1’

Is there an option to force the preprocessor to run again?

Unfortunately, I need to change some function calls, even when the file is preprocessed.

Thank you,

Mikhail Ramalho via cfe-dev <cfe-dev@lists.llvm.org> writes:

Hi all,

I'm working on a project to use clang as a frontend to a model checking
tool.

It's almost done but I'm now facing a problem that I couldn't find a
solution: How to force the preprocessor to run in already preprocessed
files?

For example, something as simples as:

int main(void)
{
  int a = x();
}

When using clangTool to generate the AST, it works fine if add something
like -Dx=x1. The generated AST looks like:

FunctionDecl 0x3a8c290 </home/mramalho/main.c:2:1, line:5:1> line:2:5 main
'int (void)'
`-CompoundStmt 0x3a8c5c0 <line:3:1, line:5:1>
  `-DeclStmt 0x3a8c5a8 <line:4:3, col:14>
    `-VarDecl 0x3a8c3b8 <col:3, col:13> col:7 a 'int' cinit
      `-CallExpr 0x3a8c580 <<command line>:6:11,
/home/mramalho/main.c:4:13> 'int'
        `-ImplicitCastExpr 0x3a8c568 <<command line>:6:11> 'int (*)()'
<FunctionToPointerDecay>
* `-DeclRefExpr 0x3a8c510 <col:11> 'int ()' Function 0x3a8c468
'x1' 'int ()'*

But if I preprocess the file:

$ clang -E main.c > main.c.i

# 1 "main.c"
# 1 "<built-in>" 1
# 1 "<built-in>" 3
# 316 "<built-in>" 3
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "main.c" 2

int main(void)
{
  int a = x();
}

and run with the same define -Dx=x1, the AST is:

FunctionDecl 0x2e583a0 <main.c:2:1, line:5:1> line:2:5 main 'int (void)'
`-CompoundStmt 0x2e586d0 <line:3:1, line:5:1>
  `-DeclStmt 0x2e586b8 <line:4:3, col:14>
    `-VarDecl 0x2e584c8 <col:3, col:13> col:7 a 'int' cinit
      `-CallExpr 0x2e58690 <col:11, col:13> 'int'
        `-ImplicitCastExpr 0x2e58678 <col:11> 'int (*)()'
<FunctionToPointerDecay>
* `-DeclRefExpr 0x2e58620 <col:11> 'int ()' Function 0x2e58578 'x'
'int ()'*

I guess the preprocessor is not running, given the following message I get
when directly using clang:

$ clang -E -Dx=x1 main.c.i
clang: warning: main.c.i: previously preprocessed input
clang: warning: argument unused during compilation: '-D x=x1'

Is there an option to force the preprocessor to run again?

clang is guessing that this is preprocessed based on the extension. One
option is to tell clang what language you want to treat the file as with
a -x argument:

  clang -E -Dx=x1 -x c main.c.i

Hi Justin,

Thank you very much, it worked!