Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[JENKINS-29804] Strip C-style block comments from memory config files…
… before parsing
  • Loading branch information
JKrag authored and MadsNielsen committed Aug 10, 2015
1 parent c60193e commit abb0de1
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -4,8 +4,10 @@
*.jar
*.war
*.ear
*.iml

.project
.idea
.classpath
.settings/
target/
Expand Down
Expand Up @@ -30,6 +30,7 @@
public class GccMemoryMapParser extends AbstractMemoryMapParser implements Serializable {

private static final Pattern MEM_SECTIONS = Pattern.compile("^\\s+(\\S+)( \\S+.*|\\(\\S+\\)| ):", Pattern.MULTILINE);
private static final Pattern COMMENT_BLOCKS = Pattern.compile("/\\*(?:.|[\\n\\r])*?\\*/");
private static final Logger LOG = Logger.getLogger(GccMemoryMapParser.class.getName());

@DataBoundConstructor
Expand All @@ -38,15 +39,25 @@ public GccMemoryMapParser(String parserUniqueName, String mapFile, String config
}

/**
* @param f
* @throws java.io.IOException
* @return a list of the defined MEMORY in the map file
* Strip any c-style block comments, e.g slash-star to star-slash.
* <b>Note:</b> this function down not correctly handle nested comment blocks.
* <b>Note:</b> this function is a bit greedy, and will incorrectly strip comments inside strings,
* but this shouldn't be a problem for memory config files.
* @param seq The content of a file that might contain c-style block-comments
* @return The same content, that has now had all block-comments stripped out.
*/
public MemoryMapConfigMemory getMemory(File f) throws IOException {
public static CharSequence stripComments(CharSequence seq) {
Matcher commentMatcher = COMMENT_BLOCKS.matcher(seq);
return commentMatcher.replaceAll("");
}

Pattern allMemory = Pattern.compile(".*?^(\\s+\\S+).*?ORIGIN.*?=([^,]*).*?LENGTH\\s\\=(.*).*$", Pattern.MULTILINE);
CharSequence seq = createCharSequenceFromFile(f);
/**
* @param seq The content of the map file
* @return a list of the defined MEMORY in the map file
*/
public MemoryMapConfigMemory getMemory(CharSequence seq) {

Pattern allMemory = Pattern.compile(".*?^(\\s+\\S+).*?[ORIGIN|org|o].*?=([^,]*).*?[LENGTH|len|l]\\s\\=\\s*([^\\s]*).*$", Pattern.MULTILINE);
Matcher match = allMemory.matcher(seq);
MemoryMapConfigMemory memory = new MemoryMapConfigMemory();
while (match.find()) {
Expand All @@ -66,9 +77,8 @@ public MemoryMapConfigMemory getMemory(File f) throws IOException {
}
*
*/
public List<MemoryMapConfigMemoryItem> getSections(File f) throws IOException {
public List<MemoryMapConfigMemoryItem> getSections(CharSequence m) {
List<MemoryMapConfigMemoryItem> items = new ArrayList<MemoryMapConfigMemoryItem>();
CharSequence m = createCharSequenceFromFile(f);

Pattern section = Pattern.compile(".*SECTIONS\\s?\\r?\\n?\\{(.*)\\n\\}", Pattern.MULTILINE | Pattern.DOTALL);

Expand Down Expand Up @@ -154,9 +164,11 @@ public MemoryMapConfigMemory parseMapFile(File f, MemoryMapConfigMemory configur
@Override
public MemoryMapConfigMemory parseConfigFile(File f) throws IOException {
//Collect sections from both the MEMORY and the SECTIONS areas from the command file.
//The memory are the top level components, sections belong to one of thsese sections
MemoryMapConfigMemory memconfig = getMemory(f);
memconfig.addAll(getSections(f));
//The memory are the top level components, sections belong to one of these sections
CharSequence stripped = stripComments(createCharSequenceFromFile(f));

MemoryMapConfigMemory memconfig = getMemory(stripped);
memconfig.addAll(getSections(stripped));
for (MemoryMapGraphConfiguration g : getGraphConfiguration()) {
for (String gItem : g.itemizeGraphDataList()) {
for (String gSplitItem : gItem.split("\\+")) {
Expand Down
63 changes: 63 additions & 0 deletions src/test/java/net/praqma/jenkins/unit/MemoryMapGccParserTest.java
Expand Up @@ -52,4 +52,67 @@ public void testParsingOfMemorySegmentInLinkerCommandFile() throws IOException {
File f2 = new File(fileNameMap);
mem = parser.parseMapFile(f2, mem);
}

@Test
public void testStrippingNoComments() {
String testData = "This string contains a line-change" +
"but no comments";
String expected = testData;
String result = GccMemoryMapParser.stripComments(testData).toString();
assertEquals("Testing successful stripping of lots of block comments", expected, result);
}

@Test
public void testStrippingSimpleComment() {
String testData = "This string contains an inline /* block comment */ and more";
String expected = "This string contains an inline and more";
String result = GccMemoryMapParser.stripComments(testData).toString();
assertEquals("Testing successful stripping of lots of block comments", expected, result);
}

@Test
public void testStrippingMultiLineBlockComment() {
String testData = "This string contains a " +
"/* multiline" +
" * block" +
" * comment */" +
" and more";
String expected = "This string contains a " +
"" +
" and more";
String result = GccMemoryMapParser.stripComments(testData).toString();
assertEquals("Testing successful stripping of lots of block comments", expected, result);
}

@Test
public void testStrippingLotsOfCrazyComments() {
String testDataWithLotsOfCrazyComments =
"MEMORY/*" +
"" +
"" +
"*/\n" +
" {\n" +
" /* start comment */\n" +
" /* start line comment */ reserved1 (!A) : ORIGIN = 0, LENGTH = 0x00FFFFF\n" +
" application (rx) : ORIGIN = 0x100000, LENGTH = 2M\n" +
" reserved2 (!A) : ORI/**/GIN = 0x03/* ***** */00000, LENGTH = 0x04FFFFF /* COMMENT */\n" +
" ram (w) : ORIGIN = 0x0800000, LENGTH = 2M /* 0x04FFFFF */\n" +
" /* more " +
"comment */\n" +
" }";

String expectedResult =
"MEMORY\n" +
" {\n" +
" \n" +
" reserved1 (!A) : ORIGIN = 0, LENGTH = 0x00FFFFF\n" +
" application (rx) : ORIGIN = 0x100000, LENGTH = 2M\n" +
" reserved2 (!A) : ORIGIN = 0x0300000, LENGTH = 0x04FFFFF \n" +
" ram (w) : ORIGIN = 0x0800000, LENGTH = 2M \n" +
" \n" +
" }";

String result = GccMemoryMapParser.stripComments(testDataWithLotsOfCrazyComments).toString();
assertEquals("Testing successful stripping of lots of block comments", expectedResult, result);
}
}
Expand Up @@ -2,10 +2,12 @@ OUTPUT_ARCH ( m68k )

MEMORY
{
reserved1 (!A) : ORIGIN = 0, LENGTH = 0x00FFFFF
/* start comment */
/* start line comment */ reserved1 (!A) : ORIGIN = 0, LENGTH = 0x00FFFFF
application (rx) : ORIGIN = 0x100000, LENGTH = 2M
reserved2 (!A) : ORIGIN = 0x0300000, LENGTH = 0x04FFFFF
ram (w) : ORIGIN = 0x0800000, LENGTH = 2M
reserved2 (!A) : ORI/**/GIN = 0x03/* ***/** */00000, LENGTH = 0x04FFFFF /* COMMENT */
ram (w) : ORIGIN = 0x0800000, LENGTH = 2M /* 0x04FFFFF */
/* more comment */
}

SECTIONS {
Expand Down

0 comments on commit abb0de1

Please sign in to comment.