mirror of
https://github.com/NationalSecurityAgency/ghidra.git
synced 2025-10-04 02:09:44 +02:00
GP-5025 Fix for comment parsing and commenting out of #ifdefed out lines
This commit is contained in:
parent
40603ee962
commit
553d7103b7
5 changed files with 113 additions and 47 deletions
|
@ -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();
|
||||
|
|
|
@ -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=<STANDARD>{
|
||||
|
@ -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=<MACEXPPATH>{
|
||||
|
@ -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=<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;
|
||||
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=<ERROR_EXPRN>{
|
||||
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=<WARNING_EXPRN>{
|
||||
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=<OUTER_TEXT>{ 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=<OUTER_TEXT> { bufAppendWithComment(buf,u,lastToken,false); lastToken = u; }
|
||||
(LOOKAHEAD(2) nl=NewLines() { bufAppendWithComment(buf,nl,lastToken,false); lastToken = null; } )* |
|
||||
|
||||
u=<OTHER_TEXT> {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=<OTHER_TEXT> {bufAppendWithComment(buf,u); })
|
||||
[LOOKAHEAD(2)t=NewLines() {if (emitExecSwitch==true) buf.append(t.image,true); else buf.append("//// " + u.image, false); }]
|
||||
[LOOKAHEAD(2)u=<OTHER_TEXT> {bufAppendWithComment(buf,u); }]
|
||||
[LOOKAHEAD(2)u=NewLines() {bufAppendWithComment(buf,u); }])+]
|
||||
[LOOKAHEAD(2)u=<OTHER_TEXT> {bufAppendWithComment(buf,u); }
|
||||
(LOOKAHEAD(2)u=NewLines(){ bufAppendWithComment(buf,u);})*])+|
|
||||
u=<OTHER_TEXT> { 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=<OTHER_TEXT> { bufAppendWithComment(buf,u,lastToken,false); lastToken = u; })
|
||||
[LOOKAHEAD(2) nl=NewLines() { bufAppendWithComment(buf,nl,lastToken,false); lastToken = null; } ]
|
||||
[LOOKAHEAD(2) u=<OTHER_TEXT> { bufAppendWithComment(buf,u,lastToken,false); lastToken = u; }]
|
||||
[LOOKAHEAD(2) nl=NewLines() { bufAppendWithComment(buf,nl,lastToken,false); lastToken = null; }])+]
|
||||
[LOOKAHEAD(2) u=<OTHER_TEXT> { 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=<OTHER_TEXT> {bufAppendWithComment(buf,u); })
|
||||
[LOOKAHEAD(2)t=NewLines() {if (emitExecSwitch==true) buf.append(t.image,true); else buf.append("//// " + u.image, false); }]
|
||||
[LOOKAHEAD(2)u=<OTHER_TEXT> {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=<OTHER_TEXT> { bufAppendWithComment(buf, u,lastToken,false); lastToken = u; })
|
||||
[LOOKAHEAD(2) nl=NewLines() { bufAppendWithComment(buf,nl,lastToken,false); lastToken = null; }]
|
||||
[LOOKAHEAD(2) u=<OTHER_TEXT> { 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;}
|
||||
}
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
**/
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue