[PATCH] D63508: make -frewrite-includes also rewrite conditions in #if/#elif

Richard Smith - zygoloid via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 12 12:59:20 PDT 2019


rsmith added inline comments.


================
Comment at: clang/test/Frontend/rewrite-includes-conditions.c:17
+line4
+#elif value2 < value2
+line5
----------------
Did you mean for this to be `value1 < value2` rather than `value2 < value2`?


================
Comment at: clang/test/Frontend/rewrite-includes-conditions.c:54-67
+// CHECK: #if 0 /* disabled by -frewrite-includes */
+// CHECK-NEXT: #if value1 == value2
+// CHECK-NEXT: #endif
+// CHECK-NEXT: #endif /* disabled by -frewrite-includes */
+// CHECK-NEXT: #if 0 /* evaluated by -frewrite-includes */
+// CHECK-NEXT: # 14 "{{.*}}rewrite-includes-conditions.c"
+
----------------
I find this output pretty hard to read -- it's hard to tell what's an original `#if` / `#endif` that's been effectively commented out, and what's an added `#if` / `#endif` that's doing the commenting.

What do you think about modifying the original line so it's no longer recognized as a preprocessing directive? For instance, instead of:

```
#if 0 /* disabled by -frewrite-includes */
#if value1 == value2
#endif
#endif /* disabled by -frewrite-includes */
#if 0 /* evaluated by -frewrite-includes */
# 14 "{{.*}}rewrite-includes-conditions.c"
line3
#if 0 /* disabled by -frewrite-includes */
#if 0
#elif value1 > value2
#endif
#endif /* disabled by -frewrite-includes */
#elif 0 /* evaluated by -frewrite-includes */
# 16 "{{.*}}rewrite-includes-conditions.c"
line4
#if 0 /* disabled by -frewrite-includes */
#if 0
#elif value1 < value2
#endif
#endif /* disabled by -frewrite-includes */
#elif 1 /* evaluated by -frewrite-includes */
# 18 "{{.*}}rewrite-includes-conditions.c"
line5
[...]
```

you might produce

```
#if 0 /* rewritten by -frewrite-includes */
!#if value1 == value2
#endif
#if 0 /* evaluated by -frewrite-includes */
# 14 "{{.*}}rewrite-includes-conditions.c"
line3
#if 0 /* rewritten by -frewrite-includes */
!#elif value1 > value2
#endif
#elif 0 /* evaluated by -frewrite-includes */
# 16 "{{.*}}rewrite-includes-conditions.c"
line4
#if 0 /* rewritten by -frewrite-includes */
!#elif value1 < value2
#endif
#elif 1 /* evaluated by -frewrite-includes */
# 18 "{{.*}}rewrite-includes-conditions.c"
line5
[...]
```

(or whatever transformation you like that prevents recognition of a `#if`/`#elif` within the `#if 0` block).

Also, maybe we could move the quoted directives inside their respective `#if` / `#elif` block, and only comment them out if the block was entered:

```
#if 0 /* evaluated by -frewrite-includes */
was: #if value1 == value2
# 14 "{{.*}}rewrite-includes-conditions.c"
line3
#elif 0 /* evaluated by -frewrite-includes */
was: #elif value1 > value2
# 16 "{{.*}}rewrite-includes-conditions.c"
line4
#elif 1 /* evaluated by -frewrite-includes */
#if 0
was: #elif value1 < value2
#endif
# 18 "{{.*}}rewrite-includes-conditions.c"
line5
[...]
```


Repository:
  rC Clang

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D63508/new/

https://reviews.llvm.org/D63508





More information about the cfe-commits mailing list