Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-3611 fix C-parsing of #pragma lines with multiple lines and comments

emteere2d2584517c86
diff --git a/Ghidra/Features/Base/src/main/javacc/ghidra/app/util/cparser/CPP/CPP.jj b/Ghidra/Features/Base/src/main/javacc/ghidra/app/util/cparser/CPP/CPP.jj
index 14a0b8c3fc..33560bbcb8 100644
--- a/Ghidra/Features/Base/src/main/javacc/ghidra/app/util/cparser/CPP/CPP.jj
+++ b/Ghidra/Features/Base/src/main/javacc/ghidra/app/util/cparser/CPP/CPP.jj
@@ -1726,10 +1726,10 @@ PPToken MacroVals() : {Token s,t,u=new Token();u.image="";}
}
PPToken Pragma() :
-{Token t;}
-{ t=<PRAGMA_EXPRN> {
- PPToken pt = new PPToken(t);
- if (emitExecSwitch==true) println("#pragma " + defs.expand(t.image,true)); return pt;
+{Token t,u=null; }
+{ (LOOKAHEAD(2)(t=<PRAGMA_EXPRN> { if (u==null) { u = t; } else { u.image += t.image; } } ))+ {
+ PPToken pt = new PPToken(u);
+ if (emitExecSwitch==true) println("#pragma " + defs.expand(u.image,true)); return pt;
}
}
@@ -1908,7 +1908,7 @@ PPToken NewLines() :
t=<EOLCMNTNL> | // newline coming from //
t=<ERRLINE> |
t=<LINLINE> |
- t=<PRGLINE> |
+ t=<PRAGLINE> |
t=<IFDLINE> |
t=<IFNDLINE> |
t=<DIRLINE> |
@@ -2691,13 +2691,19 @@ TOKEN : {
<PRAGMA>
TOKEN : {
- <PRAGMA_EXPRN: <NOTWS> (<NOTENDL>)* > : DEFAULT |
- <PRGLINE: <ENDL> > : DEFAULT
+ <PRAGMA_EXPRN:
+ <WSP> ( ~["\\","\n","\r","/"," ","\t"] |
+ (<CMT><NOTENDLSTAR>)
+ )+ > : PRAGMA |
+ <PRAGLINE: <ENDL> > : DEFAULT
}
<PRAGMA>
SKIP : {
- <_WSP1: <WSP> > : PRAGMA
+ <_LCMTPRAG: <CMT><CMT>> : LINECOMMENT |
+ <_SCMT_PRAG: (<STARTCMT> (<NOTENDLSTAR> | ("*" ~["/"]) | ("/"))+ <ENDCMT>) > : PRAGMA |
+ <_COD_WSP: <WSP>> : PRAGMA |
+ <_COD_PRAG: <COD>> : PRAGMA
}
diff --git a/Ghidra/Features/Base/src/test/java/ghidra/app/util/cparser/PreProcessorTest.java b/Ghidra/Features/Base/src/test/java/ghidra/app/util/cparser/PreProcessorTest.java
index ae699694b2..fcbd7af84e 100644
--- a/Ghidra/Features/Base/src/test/java/ghidra/app/util/cparser/PreProcessorTest.java
+++ b/Ghidra/Features/Base/src/test/java/ghidra/app/util/cparser/PreProcessorTest.java
@@ -127,6 +127,15 @@ public class PreProcessorTest extends AbstractGenericTest {
"\" second line\"\n" +
"\" third line\"\n" +
"\" fourth line\")") != -1);
+
+ assertTrue("multi line #pragma failed ", results
+ .indexOf("#pragma multiple lines pragma") != -1);
+
+ assertTrue("#pragma with comment failed ", results
+ .indexOf("#pragma no comment here") != -1);
+
+ assertTrue("#pragma with EOL comment failed ", results
+ .indexOf("#pragma with no EOL comment here") != -1);
}
@Test
diff --git a/Ghidra/Features/Base/src/test/resources/ghidra/app/util/cparser/PreProcessorTest.h b/Ghidra/Features/Base/src/test/resources/ghidra/app/util/cparser/PreProcessorTest.h
index 093a181628..cc3afcb769 100644
--- a/Ghidra/Features/Base/src/test/resources/ghidra/app/util/cparser/PreProcessorTest.h
+++ b/Ghidra/Features/Base/src/test/resources/ghidra/app/util/cparser/PreProcessorTest.h
@@ -106,6 +106,14 @@ int foo;
#pragma once
+#pragma multiple \
+ lines \
+ pragma
+
+#pragma no comment here /* no comment / here */
+
+#pragma with no EOL comment here // no comment here
+
#define PTYPE 4
#define TYPE2 2 /* 2 */