GP-5025 Fix for comment parsing and commenting out of #ifdefed out lines

This commit is contained in:
emteere 2025-01-22 22:16:52 +00:00
parent 40603ee962
commit 553d7103b7
5 changed files with 113 additions and 47 deletions

View file

@ -74,12 +74,9 @@ public class CreateUEFIGDTArchivesScript extends GhidraScript {
String dataTypeFile = outputDir + File.separator + gdtName + ".gdt"; String dataTypeFile = outputDir + File.separator + gdtName + ".gdt";
File f = getArchiveFile(dataTypeFile); File f = getArchiveFile(dataTypeFile);
FileDataTypeManager dtMgr = FileDataTypeManager.createFileArchive(f,languageID, compiler);
CParseResults results = CParserUtils.parseHeaderFiles(openTypes, filenames, includePaths, args, dtMgr, monitor); FileDataTypeManager dtMgr = CParserUtils.parseHeaderFiles(openTypes, filenames,
includePaths, args, f.getAbsolutePath(), languageID, compiler, monitor);
Msg.info(this, results.getFormattedParseMessage(null));
dtMgr.save(); dtMgr.save();
dtMgr.close(); dtMgr.close();

View file

@ -526,7 +526,7 @@ public class PreProcessor {
} }
} }
if (image.length() == 1 && image.startsWith("\n")) { if (image.length() == 1 && image.startsWith("\n")) {
image = (!emitExecSwitch ? "////\n" : "\n"); image = (!emitExecSwitch ? "///-\n" : "\n");
} }
print(image); print(image);
} }
@ -858,11 +858,25 @@ public class PreProcessor {
return false; return false;
} }
void bufAppendWithComment(PPToken buf, Token u) { // void bufAppendWithComment(PPToken buf, Token u) {
if (emitExecSwitch==true) // if (emitExecSwitch==true)
buf.append(u.image,true); // buf.append(u.image,true);
else // else
buf.append("//// " + u.image, false); // buf.append("///- " + u.image, false);
// }
void bufAppendWithComment(PPToken buf, Token u, Token previous, boolean commentNL) {
String str = u.image;
if (!emitExecSwitch) {
if (commentNL) {
// if not emmitting, replace any embedded \n with a \n-comment
str = str.replace("\n", "\n///- ");
}
if (previous == null || previous.image.length() == 0) {
str = "///- " + str;
}
}
buf.append(str, emitExecSwitch);
} }
// Parse include file // Parse include file
@ -1079,9 +1093,9 @@ public class PreProcessor {
private void printCommentedLines(boolean emitSwitch, String line, String state) { private void printCommentedLines(boolean emitSwitch, String line, String state) {
StringBuffer buf = new StringBuffer("///"); StringBuffer buf = new StringBuffer("///");
buf.append(emitSwitch ? " " : "/" ); buf.append(emitSwitch ? "+ " : "- " );
buf.append(line); buf.append(line);
if (emitSwitch) { if (emitSwitch && state != null) {
buf.append(" ===" + state); buf.append(" ===" + state);
} }
buf.append("\n"); buf.append("\n");
@ -1538,7 +1552,7 @@ PPToken IFGroup() : { PPToken t, e, olde; }
} }
olde = (PPToken) execStack.pop(); olde = (PPToken) execStack.pop();
emitExecSwitch = olde.getEmitSave(); emitExecSwitch = olde.getEmitSave();
printCommentedLines(emitExecSwitch, "#else if " + t.image, "" + t.getTruth()); printCommentedLines(emitExecSwitch, "#elif " + t.image, "" + t.getTruth());
e.setEmitSave(emitExecSwitch); e.setEmitSave(emitExecSwitch);
if (!olde.getTruth() && emitExecSwitch==true) if (!olde.getTruth() && emitExecSwitch==true)
emitExecSwitch = e.getTruth(); emitExecSwitch = e.getTruth();
@ -1618,6 +1632,8 @@ PPToken Include() : {Token t;PPToken pt;int conditionDepth=execStack.size();}
if (emitExecSwitch==true) { if (emitExecSwitch==true) {
localPlace(pt, true); localPlace(pt, true);
println("\n#line "+t.beginLine+": \""+curFileStackTop()+"\""); println("\n#line "+t.beginLine+": \""+curFileStackTop()+"\"");
} else {
printCommentedLines(emitExecSwitch, "#include <"+t.beginLine+">", "");
} }
} }
|t=<STANDARD>{ |t=<STANDARD>{
@ -1625,6 +1641,8 @@ PPToken Include() : {Token t;PPToken pt;int conditionDepth=execStack.size();}
if (emitExecSwitch==true) { if (emitExecSwitch==true) {
standardPlace(pt, true); standardPlace(pt, true);
println("\n#line "+t.beginLine+": \""+curFileStackTop()+"\""); println("\n#line "+t.beginLine+": \""+curFileStackTop()+"\"");
} else {
printCommentedLines(emitExecSwitch, "#include \""+t.beginLine+"\"", "");
} }
} }
|t=<MACEXPPATH>{ |t=<MACEXPPATH>{
@ -1738,6 +1756,7 @@ PPToken UnDef() : {Token t;}
if (isDef(pt)==true) { if (isDef(pt)==true) {
UnDefine(pt); UnDefine(pt);
} }
printCommentedLines(emitExecSwitch, "#undef " + def.image, null);
return pt; return pt;
} }
} }
@ -1762,8 +1781,13 @@ PPToken MacroVals() : {Token s,t,u=new Token();u.image="";}
PPToken Pragma() : PPToken Pragma() :
{Token t,u=null; } {Token t,u=null; }
{ (LOOKAHEAD(2)(t=<PRAGMA_EXPRN> { if (u==null) { u = t; } else { u.image += t.image; } } ))+ { { (LOOKAHEAD(2)(t=<PRAGMA_EXPRN> { if (u==null) { u = t; } else { u.image += t.image; } } ))+ {
PPToken pt = new PPToken(u); PPToken pt = new PPToken("#pragma");
if (emitExecSwitch==true) println("#pragma " + defs.expand(u.image,true)); return pt; pt.append(u.image,emitExecSwitch);
if (emitExecSwitch==true) {
println("#pragma" + defs.expand(u.image,true));
} else {
printCommentedLines(emitExecSwitch, "#pragma" + u.image, null);
}
} }
} }
@ -1813,6 +1837,7 @@ PPToken IfNDefExpr() :
PPToken Error() : PPToken Error() :
{Token t;} {Token t;}
{ t=<ERROR_EXPRN>{ { t=<ERROR_EXPRN>{
printCommentedLines(emitExecSwitch, "#error " + t.image, "Error!");
if (emitExecSwitch==true) { if (emitExecSwitch==true) {
addParseMessage(null, curFileStackTop()+"'"+t.beginLine+" #error Error:"); addParseMessage(null, curFileStackTop()+"'"+t.beginLine+" #error Error:");
addParseMessage(null, t.image); addParseMessage(null, t.image);
@ -1825,6 +1850,7 @@ PPToken Error() :
PPToken Warning() : PPToken Warning() :
{Token t;} {Token t;}
{ t=<WARNING_EXPRN>{ { t=<WARNING_EXPRN>{
printCommentedLines(emitExecSwitch, "#warning " + t.image, "Warning!");
if (emitExecSwitch==true) { if (emitExecSwitch==true) {
addParseMessage(null, curFileStackTop()+"'"+t.beginLine+" Warning: "); addParseMessage(null, curFileStackTop()+"'"+t.beginLine+" Warning: ");
addParseMessage(null, t.image); addParseMessage(null, t.image);
@ -1902,31 +1928,31 @@ PPToken QuotedValue() :
} }
PPToken Text() : PPToken Text() :
{Token u, nl, t = new Token(); PPToken buf = new PPToken(""); t.image="";} {Token u, nl, lastToken; PPToken buf = new PPToken(""); }
{ {
( LOOKAHEAD(3) ( LOOKAHEAD(3)
(LOOKAHEAD(2)(u=<OUTER_TEXT>{ bufAppendWithComment(buf,u); } (LOOKAHEAD(2)(u=<OUTER_TEXT> { bufAppendWithComment(buf,u,lastToken,false); lastToken = u; }
(LOOKAHEAD(2)nl=NewLines() {if (emitExecSwitch==true) buf.append(nl.image,true); else buf.append((u.image.length() == 0 ? "//// " : "") + nl.image, false); } )* | (LOOKAHEAD(2) nl=NewLines() { bufAppendWithComment(buf,nl,lastToken,false); lastToken = null; } )* |
u=<OTHER_TEXT> {bufAppendWithComment(buf,u); } ) u=<OTHER_TEXT> { bufAppendWithComment(buf,u,lastToken,false); lastToken = u; } )
[LOOKAHEAD(2)u=NewLines() {bufAppendWithComment(buf,u); }] [LOOKAHEAD(2)nl=NewLines() { bufAppendWithComment(buf,nl,lastToken,false); lastToken = null; }]
[(LOOKAHEAD(2)(u=QuotedText() {bufAppendWithComment(buf,u); } [(LOOKAHEAD(2)(u=QuotedText() { bufAppendWithComment(buf,u,lastToken,true); lastToken = u; }
[LOOKAHEAD(2)u=NewLines(){ bufAppendWithComment(buf,u); }] | [LOOKAHEAD(2) nl=NewLines(){ bufAppendWithComment(buf,nl,lastToken,false); lastToken = null; }] |
u=<OTHER_TEXT> {bufAppendWithComment(buf,u); }) u=<OTHER_TEXT> { bufAppendWithComment(buf,u,lastToken,false); lastToken = u; })
[LOOKAHEAD(2)t=NewLines() {if (emitExecSwitch==true) buf.append(t.image,true); else buf.append("//// " + u.image, false); }] [LOOKAHEAD(2) nl=NewLines() { bufAppendWithComment(buf,nl,lastToken,false); lastToken = null; } ]
[LOOKAHEAD(2)u=<OTHER_TEXT> {bufAppendWithComment(buf,u); }] [LOOKAHEAD(2) u=<OTHER_TEXT> { bufAppendWithComment(buf,u,lastToken,false); lastToken = u; }]
[LOOKAHEAD(2)u=NewLines() {bufAppendWithComment(buf,u); }])+] [LOOKAHEAD(2) nl=NewLines() { bufAppendWithComment(buf,nl,lastToken,false); lastToken = null; }])+]
[LOOKAHEAD(2)u=<OTHER_TEXT> {bufAppendWithComment(buf,u); } [LOOKAHEAD(2) u=<OTHER_TEXT> { bufAppendWithComment(buf,u,lastToken,false); lastToken = u; }
(LOOKAHEAD(2)u=NewLines(){ bufAppendWithComment(buf,u);})*])+| (LOOKAHEAD(2) nl=NewLines(){ bufAppendWithComment(buf,nl,lastToken,false); lastToken = null; })*])+|
(LOOKAHEAD(2)(u=QuotedText() {bufAppendWithComment(buf,u); } (LOOKAHEAD(2)(u=QuotedText() { bufAppendWithComment(buf,u,lastToken,true); lastToken = u; }
[LOOKAHEAD(2)u=NewLines(){ bufAppendWithComment(buf,u); }] | [LOOKAHEAD(2) nl=NewLines(){ bufAppendWithComment(buf,nl,lastToken,false); lastToken = null; }] |
u=<OTHER_TEXT> {bufAppendWithComment(buf,u); }) u=<OTHER_TEXT> { bufAppendWithComment(buf, u,lastToken,false); lastToken = u; })
[LOOKAHEAD(2)t=NewLines() {if (emitExecSwitch==true) buf.append(t.image,true); else buf.append("//// " + u.image, false); }] [LOOKAHEAD(2) nl=NewLines() { bufAppendWithComment(buf,nl,lastToken,false); lastToken = null; }]
[LOOKAHEAD(2)u=<OTHER_TEXT> {bufAppendWithComment(buf,u); }] [LOOKAHEAD(2) u=<OTHER_TEXT> { bufAppendWithComment(buf, u,lastToken,false); lastToken = u; }]
[LOOKAHEAD(2)u=NewLines() {bufAppendWithComment(buf,u); }])+ | [LOOKAHEAD(2) nl=NewLines() { bufAppendWithComment(buf, nl,lastToken,false); lastToken = null; }])+ |
u=NewLines() {bufAppendWithComment(buf,u); } nl=NewLines() { bufAppendWithComment(buf,nl); lastToken = null; }
) { return buf;} ) { return buf;}
} }

View file

@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -91,7 +91,7 @@ public class PreProcessorTest extends AbstractGenericTest {
System.out.println(parser.getParseMessages()); System.out.println(parser.getParseMessages());
// Uncomment to print out parse results // Uncomment to print out parse results
// System.err.println(baos.toString()); //System.err.println(baos.toString());
dtMgr = new StandAloneDataTypeManager("parsed"); dtMgr = new StandAloneDataTypeManager("parsed");
parser.getDefinitions().populateDefineEquates(null, dtMgr); parser.getDefinitions().populateDefineEquates(null, dtMgr);
@ -129,13 +129,29 @@ public class PreProcessorTest extends AbstractGenericTest {
"\" fourth line\")") != -1); "\" fourth line\")") != -1);
assertTrue("multi line #pragma failed ", results assertTrue("multi line #pragma failed ", results
.indexOf("#pragma multiple lines pragma") != -1); .indexOf("#pragma multiple lines pragma") != -1);
assertTrue("#pragma with comment failed ", results assertTrue("#pragma with comment failed ", results
.indexOf("#pragma no comment here") != -1); .indexOf("#pragma no comment here") != -1);
assertTrue("#pragma with EOL comment failed ", results assertTrue("#pragma with EOL comment failed ", results
.indexOf("#pragma with no EOL comment here") != -1); .indexOf("#pragma with no EOL comment here") != -1);
}
@Test
public void testCommenting() throws Exception {
String results = baos.toString("ASCII");
assertTrue("IntShouldBeCommented", results
.indexOf("///- int IntShouldBeCommented;") != -1);
assertTrue("PragmaShouldBeCommented", results
.indexOf("///- #pragma PragmaShouldBeCommented") != -1);
assertTrue("IntShouldNotBeCommented", results
.indexOf("int IntShouldBeCommented;") != -1);
assertTrue("PragmaShouldNotBeCommented", results
.indexOf("#pragma PragmaShouldNotBeCommented") != -1);
} }
@Test @Test

View file

@ -37,6 +37,16 @@
#define DidFOO "true" /* test comment */ #define DidFOO "true" /* test comment */
# endif # endif
#ifdef NOFOO
int IntShouldBeCommented;
#pragma PragmaShouldBeCommented
#endif
#ifndef NOFOO
int IntShouldNotBeCommented;
#pragma PragmaShouldNotBeCommented;
#endif
/* definition coming from -D, should evaluate to true */ /* definition coming from -D, should evaluate to true */
#if FROM_ARG_VALUE #if FROM_ARG_VALUE
@ -460,6 +470,23 @@ ldp LDP((
_Pragma("clang diagnostic push") \ _Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wmismatched-tags\"") _Pragma("clang diagnostic ignored \"-Wmismatched-tags\"")
/**
** Multi line false ifdef with Quoted string
*/
#define __USE_GNU "1"
#ifdef __USE_GNU
extern int pthread_yield (void) __THROW;
# ifdef __REDIRECT_NTH
extern int __REDIRECT_NTH (pthread_yield, (void), sched_yield)
__attribute_deprecated_msg__ ("\
pthread_yield is deprecated, use sched_yield instead");
# else
# define pthread_yield sched_yield
# endif
#endif
/** /**
** Protected from macro expansion ** Protected from macro expansion
**/ **/

View file

@ -4,9 +4,9 @@
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* *
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* *
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -17,15 +17,15 @@
#define INCLUDE1 #define INCLUDE1
#pragma pack(push,1) #pragma pack(push,1)
#else if !defined(INCLUDE2) #elif !defined(INCLUDE2)
#define INCLUDE2 #define INCLUDE2
#pragma pack(push, 2) #pragma pack(push, 2)
#else if !defined(INCLUDE3) #elif !defined(INCLUDE3)
#define INCLUDE3 #define INCLUDE3
#pragma pack(push, 4) #pragma pack(push, 4)
#else if !defined(INCLUDE4) #elif !defined(INCLUDE4)
#define INCLUDE4 #define INCLUDE4
#pragma pack(push, 8) #pragma pack(push, 8)