<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/63043>63043</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Clang ignores preprocessor directives when modularized headers are mixed with nonmodularized headers
</td>
</tr>
<tr>
<th>Labels</th>
<td>
clang:modules,
miscompilation
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
alanzhao1
</td>
</tr>
</table>
<pre>
This was found when I was attempting to find a minimal reproduction for https://crbug.com/1430962. It is likely that these two bugs have the same or at least a similar root cause.
Repro steps:
I have commit 1cf5188c72902e85e85095d788f5dfa138c320f8 checked out.
All of these files are in the same directory.
```cpp
// nonmod.h
#ifndef NONMOD_H_
#define NONMOD_H_
int foo() {
#ifdef FOO_RET_1
return 1;
#else
return 0;
#endif
}
#endif // NONMOD_H_
```
```cpp
// mod.h
#ifndef MOD_H_
#define MOD_H_
#include "nonmod.h"
#endif // MOD_H_
```
```
// module.modulemap
module mod {
export *
header "mod.h"
}
```
```cpp
// translation_unit.cc
#define FOO_RET_1
#include "nonmod.h"
#include "mod.h"
```
Expected behavior:
When compiling `translation_unit.cc` with modules enabled, `foo()` should return 1.
Actual behavior:
`foo()` correctly returns 1 when compiled with `-fno-modules` but `foo()` incorrectly returns 0 when compiled with `-fmodules`:
```sh
# All commands are run in the same directory where the files are saved.
# With modules
$ ~/src/llvm-project/build-debug/bin/clang++ -c -S -emit-llvm -o - -fno-modules translation_unit.cc
# extra lines removed...
; Function Attrs: mustprogress noinline nounwind optnone uwtable
define dso_local noundef i32 @_Z3foov() #0 {
entry:
ret i32 1
}
# ...
$ ~/src/llvm-project/build-debug/bin/clang++ -c -S -emit-llvm -o - -fmodules translation_unit.cc
# extra lines removed...
; Function Attrs: mustprogress noinline nounwind optnone uwtable
define dso_local noundef i32 @_Z3foov() #0 {
entry:
ret i32 0
}
# ...
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llv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