commit 7174c46345ece7b7c76af0bae10958bb17fa56d9
parent d03ec63dc2fcd2583d3f13bf5dbe19b78fe85a47
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Wed, 26 Oct 2022 19:41:52 +0200
cc1: Detect conditional preprocessor disaligned
The code was not able to detect cases when a #else or
aligned with a previous #if.
Diffstat:
1 file changed, 20 insertions(+), 12 deletions(-)
diff --git a/src/cmd/cc/cc1/cpp.c b/src/cmd/cc/cc1/cpp.c
@@ -9,9 +9,14 @@
#include <scc/scc.h>
#include "cc1.h"
+struct ifstate {
+ unsigned char enabled;
+ unsigned char iselse;
+};
+
static unsigned ncmdlines;
static Symbol *symline, *symfile;
-static unsigned char ifstatus[NR_COND];
+static struct ifstate ifstate[NR_COND];
static int cppoff;
static struct items dirinclude;
@@ -785,8 +790,10 @@ ifclause(int negate, int isifdef)
if (negate)
status = !status;
- if ((ifstatus[n] = status) == 0)
+ if (status == 0)
++cppoff;
+ ifstate[n].enabled = status;
+ ifstate[n].iselse = 0;
}
static void
@@ -813,19 +820,20 @@ elseclause(void)
{
int status;
- if (cppctx == 0) {
- cpperror("#else without #ifdef/ifndef");
- return;
- }
-
- status = ifstatus[cppctx-1];
- ifstatus[cppctx-1] = !status;
+ status = ifstate[cppctx-1].enabled;
+ ifstate[cppctx-1].enabled = !status;
cppoff += (status) ? 1 : -1;
}
static void
cppelse(void)
{
+ if (cppctx == 0 || ifstate[cppctx-1].iselse) {
+ cpperror("#else without #ifdef/ifndef");
+ return;
+ }
+
+ ifstate[cppctx-1].iselse = 1;
elseclause();
next();
}
@@ -833,13 +841,13 @@ cppelse(void)
static void
elif(void)
{
- if (cppctx == 0) {
+ if (cppctx == 0 || ifstate[cppctx-1].iselse) {
cpperror("#elif without #ifdef/ifndef");
return;
}
elseclause();
- if (ifstatus[cppctx-1]) {
+ if (ifstate[cppctx-1].enabled) {
--cppctx;
cppif();
}
@@ -850,7 +858,7 @@ endif(void)
{
if (cppctx == 0)
error("#endif without #if");
- if (!ifstatus[--cppctx])
+ if (!ifstate[--cppctx].enabled)
--cppoff;
next();
}