diff --git a/Ghidra/Features/Base/ghidra_scripts/CreateUEFIGDTArchivesScript.java b/Ghidra/Features/Base/ghidra_scripts/CreateUEFIGDTArchivesScript.java index 68a62be3f2..02fadb008f 100644 --- a/Ghidra/Features/Base/ghidra_scripts/CreateUEFIGDTArchivesScript.java +++ b/Ghidra/Features/Base/ghidra_scripts/CreateUEFIGDTArchivesScript.java @@ -74,12 +74,9 @@ public class CreateUEFIGDTArchivesScript extends GhidraScript { String dataTypeFile = outputDir + File.separator + gdtName + ".gdt"; File f = getArchiveFile(dataTypeFile); - - FileDataTypeManager dtMgr = FileDataTypeManager.createFileArchive(f,languageID, compiler); - CParseResults results = CParserUtils.parseHeaderFiles(openTypes, filenames, includePaths, args, dtMgr, monitor); - - Msg.info(this, results.getFormattedParseMessage(null)); + FileDataTypeManager dtMgr = CParserUtils.parseHeaderFiles(openTypes, filenames, + includePaths, args, f.getAbsolutePath(), languageID, compiler, monitor); dtMgr.save(); dtMgr.close(); 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 6ec2b81ae5..6e9617b614 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 @@ -526,7 +526,7 @@ public class PreProcessor { } } if (image.length() == 1 && image.startsWith("\n")) { - image = (!emitExecSwitch ? "////\n" : "\n"); + image = (!emitExecSwitch ? "///-\n" : "\n"); } print(image); } @@ -858,11 +858,25 @@ public class PreProcessor { return false; } - void bufAppendWithComment(PPToken buf, Token u) { - if (emitExecSwitch==true) - buf.append(u.image,true); - else - buf.append("//// " + u.image, false); +// void bufAppendWithComment(PPToken buf, Token u) { +// if (emitExecSwitch==true) +// buf.append(u.image,true); +// else +// 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 @@ -1079,9 +1093,9 @@ public class PreProcessor { private void printCommentedLines(boolean emitSwitch, String line, String state) { StringBuffer buf = new StringBuffer("///"); - buf.append(emitSwitch ? " " : "/" ); + buf.append(emitSwitch ? "+ " : "- " ); buf.append(line); - if (emitSwitch) { + if (emitSwitch && state != null) { buf.append(" ===" + state); } buf.append("\n"); @@ -1538,7 +1552,7 @@ PPToken IFGroup() : { PPToken t, e, olde; } } olde = (PPToken) execStack.pop(); emitExecSwitch = olde.getEmitSave(); - printCommentedLines(emitExecSwitch, "#else if " + t.image, "" + t.getTruth()); + printCommentedLines(emitExecSwitch, "#elif " + t.image, "" + t.getTruth()); e.setEmitSave(emitExecSwitch); if (!olde.getTruth() && emitExecSwitch==true) emitExecSwitch = e.getTruth(); @@ -1618,6 +1632,8 @@ PPToken Include() : {Token t;PPToken pt;int conditionDepth=execStack.size();} if (emitExecSwitch==true) { localPlace(pt, true); println("\n#line "+t.beginLine+": \""+curFileStackTop()+"\""); + } else { + printCommentedLines(emitExecSwitch, "#include <"+t.beginLine+">", ""); } } |t={ @@ -1625,6 +1641,8 @@ PPToken Include() : {Token t;PPToken pt;int conditionDepth=execStack.size();} if (emitExecSwitch==true) { standardPlace(pt, true); println("\n#line "+t.beginLine+": \""+curFileStackTop()+"\""); + } else { + printCommentedLines(emitExecSwitch, "#include \""+t.beginLine+"\"", ""); } } |t={ @@ -1738,6 +1756,7 @@ PPToken UnDef() : {Token t;} if (isDef(pt)==true) { UnDefine(pt); } + printCommentedLines(emitExecSwitch, "#undef " + def.image, null); return pt; } } @@ -1762,8 +1781,13 @@ PPToken MacroVals() : {Token s,t,u=new Token();u.image="";} PPToken Pragma() : {Token t,u=null; } { (LOOKAHEAD(2)(t= { 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; + PPToken pt = new PPToken("#pragma"); + 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() : {Token t;} { t={ + printCommentedLines(emitExecSwitch, "#error " + t.image, "Error!"); if (emitExecSwitch==true) { addParseMessage(null, curFileStackTop()+"'"+t.beginLine+" #error Error:"); addParseMessage(null, t.image); @@ -1825,6 +1850,7 @@ PPToken Error() : PPToken Warning() : {Token t;} { t={ + printCommentedLines(emitExecSwitch, "#warning " + t.image, "Warning!"); if (emitExecSwitch==true) { addParseMessage(null, curFileStackTop()+"'"+t.beginLine+" Warning: "); addParseMessage(null, t.image); @@ -1902,31 +1928,31 @@ PPToken QuotedValue() : } 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(2)(u={ bufAppendWithComment(buf,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)(u= { bufAppendWithComment(buf,u,lastToken,false); lastToken = u; } + (LOOKAHEAD(2) nl=NewLines() { bufAppendWithComment(buf,nl,lastToken,false); lastToken = null; } )* | - u= {bufAppendWithComment(buf,u); } ) - [LOOKAHEAD(2)u=NewLines() {bufAppendWithComment(buf,u); }] - [(LOOKAHEAD(2)(u=QuotedText() {bufAppendWithComment(buf,u); } - [LOOKAHEAD(2)u=NewLines(){ bufAppendWithComment(buf,u); }] | - u= {bufAppendWithComment(buf,u); }) - [LOOKAHEAD(2)t=NewLines() {if (emitExecSwitch==true) buf.append(t.image,true); else buf.append("//// " + u.image, false); }] - [LOOKAHEAD(2)u= {bufAppendWithComment(buf,u); }] - [LOOKAHEAD(2)u=NewLines() {bufAppendWithComment(buf,u); }])+] - [LOOKAHEAD(2)u= {bufAppendWithComment(buf,u); } - (LOOKAHEAD(2)u=NewLines(){ bufAppendWithComment(buf,u);})*])+| + u= { bufAppendWithComment(buf,u,lastToken,false); lastToken = u; } ) + [LOOKAHEAD(2)nl=NewLines() { bufAppendWithComment(buf,nl,lastToken,false); lastToken = null; }] + [(LOOKAHEAD(2)(u=QuotedText() { bufAppendWithComment(buf,u,lastToken,true); lastToken = u; } + [LOOKAHEAD(2) nl=NewLines(){ bufAppendWithComment(buf,nl,lastToken,false); lastToken = null; }] | + u= { bufAppendWithComment(buf,u,lastToken,false); lastToken = u; }) + [LOOKAHEAD(2) nl=NewLines() { bufAppendWithComment(buf,nl,lastToken,false); lastToken = null; } ] + [LOOKAHEAD(2) u= { bufAppendWithComment(buf,u,lastToken,false); lastToken = u; }] + [LOOKAHEAD(2) nl=NewLines() { bufAppendWithComment(buf,nl,lastToken,false); lastToken = null; }])+] + [LOOKAHEAD(2) u= { bufAppendWithComment(buf,u,lastToken,false); lastToken = u; } + (LOOKAHEAD(2) nl=NewLines(){ bufAppendWithComment(buf,nl,lastToken,false); lastToken = null; })*])+| - (LOOKAHEAD(2)(u=QuotedText() {bufAppendWithComment(buf,u); } - [LOOKAHEAD(2)u=NewLines(){ bufAppendWithComment(buf,u); }] | - u= {bufAppendWithComment(buf,u); }) - [LOOKAHEAD(2)t=NewLines() {if (emitExecSwitch==true) buf.append(t.image,true); else buf.append("//// " + u.image, false); }] - [LOOKAHEAD(2)u= {bufAppendWithComment(buf,u); }] - [LOOKAHEAD(2)u=NewLines() {bufAppendWithComment(buf,u); }])+ | + (LOOKAHEAD(2)(u=QuotedText() { bufAppendWithComment(buf,u,lastToken,true); lastToken = u; } + [LOOKAHEAD(2) nl=NewLines(){ bufAppendWithComment(buf,nl,lastToken,false); lastToken = null; }] | + u= { bufAppendWithComment(buf, u,lastToken,false); lastToken = u; }) + [LOOKAHEAD(2) nl=NewLines() { bufAppendWithComment(buf,nl,lastToken,false); lastToken = null; }] + [LOOKAHEAD(2) u= { bufAppendWithComment(buf, u,lastToken,false); lastToken = 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;} } 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 fcbd7af84e..5a1d087d61 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 @@ -4,9 +4,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * 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()); // Uncomment to print out parse results - // System.err.println(baos.toString()); + //System.err.println(baos.toString()); dtMgr = new StandAloneDataTypeManager("parsed"); parser.getDefinitions().populateDefineEquates(null, dtMgr); @@ -129,13 +129,29 @@ public class PreProcessorTest extends AbstractGenericTest { "\" fourth line\")") != -1); assertTrue("multi line #pragma failed ", results - .indexOf("#pragma multiple lines pragma") != -1); + .indexOf("#pragma multiple lines pragma") != -1); 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 - .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 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 72046da90e..cb3c19204e 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 @@ -37,6 +37,16 @@ #define DidFOO "true" /* test comment */ # endif +#ifdef NOFOO +int IntShouldBeCommented; +#pragma PragmaShouldBeCommented +#endif + +#ifndef NOFOO +int IntShouldNotBeCommented; +#pragma PragmaShouldNotBeCommented; +#endif + /* definition coming from -D, should evaluate to true */ #if FROM_ARG_VALUE @@ -460,6 +470,23 @@ ldp LDP(( _Pragma("clang diagnostic push") \ _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 **/ diff --git a/Ghidra/Features/Base/src/test/resources/ghidra/app/util/cparser/multinclude.h b/Ghidra/Features/Base/src/test/resources/ghidra/app/util/cparser/multinclude.h index d9df9c33f3..c298c2e5bc 100644 --- a/Ghidra/Features/Base/src/test/resources/ghidra/app/util/cparser/multinclude.h +++ b/Ghidra/Features/Base/src/test/resources/ghidra/app/util/cparser/multinclude.h @@ -4,9 +4,9 @@ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -17,15 +17,15 @@ #define INCLUDE1 #pragma pack(push,1) -#else if !defined(INCLUDE2) +#elif !defined(INCLUDE2) #define INCLUDE2 #pragma pack(push, 2) -#else if !defined(INCLUDE3) +#elif !defined(INCLUDE3) #define INCLUDE3 #pragma pack(push, 4) -#else if !defined(INCLUDE4) +#elif !defined(INCLUDE4) #define INCLUDE4 #pragma pack(push, 8)