Skip to content

Commit 87104dd

Browse files
authored
Merge pull request #2200 from guwirth/whitespace-channel
optimize the whitespace channel
2 parents 1ca187d + 7b91c78 commit 87104dd

File tree

3 files changed

+68
-7
lines changed

3 files changed

+68
-7
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* C++ Community Plugin (cxx plugin)
3+
* Copyright (C) 2010-2021 SonarOpenCommunity
4+
* http://github.com/SonarOpenCommunity/sonar-cxx
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU Lesser General Public
8+
* License as published by the Free Software Foundation; either
9+
* version 3 of the License, or (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14+
* Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with this program; if not, write to the Free Software Foundation,
18+
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19+
*/
20+
package org.sonar.cxx.channels;
21+
22+
import com.sonar.sslr.api.Token;
23+
import com.sonar.sslr.impl.Lexer;
24+
import org.sonar.cxx.parser.CxxTokenType;
25+
import org.sonar.sslr.channel.Channel;
26+
import org.sonar.sslr.channel.CodeReader;
27+
28+
public class WhitespaceChannel extends Channel<Lexer> {
29+
30+
@Override
31+
public boolean consume(CodeReader code, Lexer output) {
32+
var isWhitespace = false;
33+
while (Character.isWhitespace(code.peek())) {
34+
if (!isWhitespace) {
35+
int line = code.getLinePosition();
36+
int column = code.getColumnPosition();
37+
// merge multiple whitespaces into one
38+
output.addToken(Token.builder()
39+
.setLine(line)
40+
.setColumn(column)
41+
.setURI(output.getURI())
42+
.setValueAndOriginalValue(" ")
43+
.setType(CxxTokenType.WS)
44+
.build());
45+
isWhitespace = true;
46+
}
47+
code.pop();
48+
}
49+
50+
return isWhitespace;
51+
}
52+
53+
}

cxx-squid/src/main/java/org/sonar/cxx/preprocessor/CppLexer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.sonar.cxx.channels.CharacterLiteralsChannel;
3737
import org.sonar.cxx.channels.KeywordChannel;
3838
import org.sonar.cxx.channels.StringLiteralsChannel;
39+
import org.sonar.cxx.channels.WhitespaceChannel;
3940
import org.sonar.cxx.parser.CxxTokenType;
4041

4142
public final class CppLexer {
@@ -68,7 +69,7 @@ public static Lexer create(Charset charset) {
6869
var builder = Lexer.builder()
6970
.withCharset(charset)
7071
.withFailIfNoChannelToConsumeOneCharacter(true)
71-
.withChannel(regexp(CxxTokenType.WS, "\\s+"))
72+
.withChannel(new WhitespaceChannel())
7273
.withChannel(commentRegexp("//[^\\n\\r]*+"))
7374
.withChannel(commentRegexp("/\\*", ANY_CHAR + "*?", "\\*/"))
7475
.withChannel(new CharacterLiteralsChannel())

cxx-squid/src/main/java/org/sonar/cxx/preprocessor/Macro.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,23 @@ private static void add(Map<String, Macro> map, String name, String body) {
7676

7777
@Override
7878
public String toString() {
79-
var paramsStr = "";
79+
StringBuilder ab = new StringBuilder(64);
80+
ab.append("{");
81+
ab.append(name);
8082
if (params != null) {
81-
final String joinedParams = params.stream().map(Token::getValue).collect(Collectors.joining(", "));
82-
paramsStr = "(" + joinedParams + (isVariadic ? "..." : "") + ")";
83+
ab.append("(");
84+
ab.append(params.stream().map(Token::getValue).collect(Collectors.joining(", ")));
85+
if (isVariadic) {
86+
ab.append("...");
87+
}
88+
ab.append(")");
8389
}
84-
var bodyStr = "";
8590
if (body != null) {
86-
bodyStr = body.stream().map(Token::getValue).collect(Collectors.joining(" "));
91+
ab.append(":");
92+
ab.append(body.stream().map(Token::getValue).collect(Collectors.joining()));
8793
}
88-
return "{" + name + paramsStr + ":" + bodyStr + "}";
94+
ab.append("}");
95+
return ab.toString();
8996
}
9097

9198
public boolean checkArgumentsCount(int count) {

0 commit comments

Comments
 (0)