From dae80ef2cd7cb0c2797cd21f04c19888f8c16de3 Mon Sep 17 00:00:00 2001 From: Anton Leherbauer Date: Mon, 16 Apr 2007 10:39:10 +0000 Subject: [PATCH] Fix for 182180: Bug with parser and #ifdef statements and "/*" (follow-up) --- .../parser/tests/scanner2/Scanner2Test.java | 64 ++++++++++++++++++- .../core/parser/scanner2/BaseScanner.java | 39 +++++++---- 2 files changed, 88 insertions(+), 15 deletions(-) diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner2/Scanner2Test.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner2/Scanner2Test.java index 9e5f21dd5d8..14a0e79c504 100644 --- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner2/Scanner2Test.java +++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/core/parser/tests/scanner2/Scanner2Test.java @@ -2504,16 +2504,74 @@ public class Scanner2Test extends BaseScanner2Test validateIdentifier("a"); } + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=180172 + public void testBug180172() throws Exception { + StringBuffer buffer = new StringBuffer(); + String value= "\"https://bugs.eclipse.org/bugs/show_bug.cgi?id=180172\""; + buffer.append("#define bug180172 ").append(value).append(" // bla \n"); + initializeScanner(buffer.toString()); + fullyTokenize(); + validateDefinition("bug180172", value); + } + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=182180 - public void testBug182180() throws Exception { + public void testBug182180_1() throws Exception { StringBuffer buffer = new StringBuffer(); buffer .append("#ifdef _bug_182180_\n") - .append(" printf(\"Hello World /*.ap\\n\");\n") + .append("printf(\"Hello World /*.ap\\n\");\n") .append("#endif\n") .append("bug182180\n"); initializeScanner(buffer.toString()); validateIdentifier("bug182180"); } - + + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=182180 + public void testBug182180_2() throws Exception { + StringBuffer buffer = new StringBuffer(); + buffer + .append("#ifdef _bug_182180_\n") + .append("char c='\"'; printf(\"Hello World /*.ap\\n\");\n") + .append("#endif\n") + .append("bug182180\n"); + initializeScanner(buffer.toString()); + validateIdentifier("bug182180"); + } + + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=182180 + public void testBug182180_3() throws Exception { + StringBuffer buffer = new StringBuffer(); + buffer + .append("#ifdef _bug_182180_\n") + .append("char c1='\\'',c2='\\\"'; printf(\"Hello World /*.ap\\n\");\n") + .append("#endif\n") + .append("bug182180\n"); + initializeScanner(buffer.toString()); + validateIdentifier("bug182180"); + } + + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=182180 + public void testBug182180_4() throws Exception { + StringBuffer buffer = new StringBuffer(); + buffer + .append("#ifdef _bug_182180_\n") + .append("printf(\"Hello '\"'World /*.ap\\n\");\n") + .append("#endif\n") + .append("bug182180\n"); + initializeScanner(buffer.toString()); + validateIdentifier("bug182180"); + } + + // https://bugs.eclipse.org/bugs/show_bug.cgi?id=182180 + public void testBug182180_5() throws Exception { + StringBuffer buffer = new StringBuffer(); + buffer + .append("#ifdef _bug_182180_\n") + .append("printf(\"Hello \\\"World /*.ap\\n\");\n") + .append("#endif\n") + .append("bug182180\n"); + initializeScanner(buffer.toString()); + validateIdentifier("bug182180"); + } + } diff --git a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/BaseScanner.java b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/BaseScanner.java index 5e8c78fca75..9642b39fa61 100644 --- a/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/BaseScanner.java +++ b/core/org.eclipse.cdt.core/parser/org/eclipse/cdt/internal/core/parser/scanner2/BaseScanner.java @@ -2338,10 +2338,12 @@ abstract class BaseScanner implements IScanner { Arrays.fill(result, ' '); int resultCount = 0; boolean insideString= false; - boolean backslash= false; + boolean insideSingleQuote= false; + boolean escaped= false; // either a single-line or multi-line comment was found for (int i = 0; i < text.length; ++i) { - if (!insideString && (text[i] == '/' && (i + 1 < text.length) && (text[i + 1] == '*' || text[i + 1] == '/'))) { + if (!insideString && !insideSingleQuote && (text[i] == '/' + && (i + 1 < text.length) && (text[i + 1] == '*' || text[i + 1] == '/'))) { if (text[i + 1] == '/') { // done break; @@ -2353,13 +2355,20 @@ abstract class BaseScanner implements IScanner { } ++i; } else { - if (insideString && !backslash && text[i] == '\\') { - backslash= true; - } else { - backslash= false; - } - if (!backslash && text[i] == '"') { - insideString= !insideString; + switch (text[i]) { + case '\\': + escaped = !escaped; + break; + case '"': + if (!insideSingleQuote && !escaped) { + insideString= !insideString; + } + break; + case '\'': + if (!insideString && !escaped) { + insideSingleQuote= !insideSingleQuote; + } + break; } result[resultCount++] = text[i]; } @@ -3003,11 +3012,12 @@ abstract class BaseScanner implements IScanner { boolean escaped = false; boolean insideString= false; + boolean insideSingleQuote= false; while (++pos < limit) { char ch= buffer[pos]; switch (ch) { case '/': - if (insideComment || insideString) { + if (insideComment || insideString || insideSingleQuote) { break; } if (pos + 1 < limit) { @@ -3032,10 +3042,15 @@ abstract class BaseScanner implements IScanner { escaped = !escaped; continue; case '"': - if (!insideComment && !escaped) { + if (!insideComment && !insideSingleQuote && !escaped) { insideString= !insideString; } - break; + continue; + case '\'': + if (!insideComment && !insideString && !escaped) { + insideSingleQuote= !insideSingleQuote; + } + continue; case '\n': if (escaped) { break;