Skip to content

Commit

Permalink
Fix a test failure, which occurres on cloudbees and one of my machines.
Browse files Browse the repository at this point in the history
The failure occurred because of parsing many files and not triggering the
finishedParsing() event on the preprocessor.
  • Loading branch information
wenns authored and Bertk committed Dec 27, 2013
1 parent a758ac8 commit 3f8a5e5
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 40 deletions.
2 changes: 1 addition & 1 deletion cxx-squid/src/main/java/org/sonar/cxx/CxxAstScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public SourceCode createSourceCode(SourceCode parentSourceCode, AstNode astNode)
.build());

// to emit a 'new file' event to the internals of the plugin
builder.withSquidAstVisitor(CxxParser.getFileVisitNotifier());
builder.withSquidAstVisitor(new CxxFileVisitor(context));

/* External visitors (typically Check ones) */
for (SquidAstVisitor<Grammar> visitor : visitors) {
Expand Down
56 changes: 56 additions & 0 deletions cxx-squid/src/main/java/org/sonar/cxx/CxxFileVisitor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Sonar C++ Plugin (Community)
* Copyright (C) 2011 Waleri Enns and CONTACT Software GmbH
* dev@sonar.codehaus.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
*/
package org.sonar.cxx;

import com.sonar.sslr.api.AstAndTokenVisitor;
import com.sonar.sslr.api.AstNode;
import com.sonar.sslr.api.Grammar;
import com.sonar.sslr.api.Token;
import com.sonar.sslr.squid.SquidAstVisitor;
import org.sonar.squid.measures.MetricDef;
import com.sonar.sslr.squid.SquidAstVisitorContext;

import org.sonar.cxx.parser.CxxParser;

public class CxxFileVisitor<GRAMMAR extends Grammar> extends SquidAstVisitor<GRAMMAR>
implements AstAndTokenVisitor {

private SquidAstVisitorContext context = null;

CxxFileVisitor(SquidAstVisitorContext context){
this.context = context;
}

/**
* {@inheritDoc}
*/
@Override
public void visitFile(AstNode node) {
CxxParser.finishedParsing(context.getFile());
}

/**
* {@inheritDoc}
*/
public void visitToken(Token token) {
}
}


46 changes: 8 additions & 38 deletions cxx-squid/src/main/java/org/sonar/cxx/parser/CxxParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@
*/
package org.sonar.cxx.parser;

import com.sonar.sslr.api.AstAndTokenVisitor;
import com.sonar.sslr.squid.SquidAstVisitor;
import com.sonar.sslr.api.AstNode;
import com.sonar.sslr.api.Token;

import com.sonar.sslr.impl.Parser;
import com.sonar.sslr.squid.SquidAstVisitorContext;
import com.sonar.sslr.squid.SquidAstVisitorContextImpl;
Expand All @@ -36,41 +31,16 @@
import org.sonar.cxx.preprocessor.JoinStringsPreprocessor;
import org.sonar.squid.api.SourceProject;

public final class CxxParser {
private static VisitFileNotifier visitFileNotifier = null;

/**
* Visitor that emits the 'new file' event to the preprocessor
*/
static class VisitFileNotifier<GRAMMAR extends Grammar> extends SquidAstVisitor<GRAMMAR> implements AstAndTokenVisitor {
private CxxPreprocessor cxxpp;
private SquidAstVisitorContext<Grammar> astVisitorContext;
import java.io.File;

VisitFileNotifier(CxxPreprocessor cxxpp, SquidAstVisitorContext<Grammar> astVisitorContext) {
this.cxxpp = cxxpp;
this.astVisitorContext = astVisitorContext;
}

/**
* {@inheritDoc}
*/
@Override
public void visitFile(AstNode node) {
this.cxxpp.beginPreprocessing(astVisitorContext.getFile());
}

/**
* {@inheritDoc}
*/
public void visitToken(Token token) {
}
}
public final class CxxParser {
private static CxxPreprocessor cxxpp = null;

private CxxParser() {
}

public static SquidAstVisitor<Grammar> getFileVisitNotifier(){
return visitFileNotifier;
public static void finishedParsing(File path){
cxxpp.finishedPreprocessing(path);
}

public static Parser<Grammar> create() {
Expand All @@ -82,9 +52,9 @@ public static Parser<Grammar> create(SquidAstVisitorContext<Grammar> context) {
return create(context, new CxxConfiguration());
}

public static Parser<Grammar> create(SquidAstVisitorContext<Grammar> context, CxxConfiguration conf) {
CxxPreprocessor cxxpp = new CxxPreprocessor(context, conf);
visitFileNotifier = new VisitFileNotifier(cxxpp, context);
public static Parser<Grammar> create(SquidAstVisitorContext<Grammar> context,
CxxConfiguration conf) {
cxxpp = new CxxPreprocessor(context, conf);
return Parser.builder(CxxGrammarImpl.create())
.withLexer(CxxLexer.create(conf, cxxpp, new JoinStringsPreprocessor()))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public PreprocessorAction process(List<Token> tokens) {
return PreprocessorAction.NO_OPERATION;
}

public void beginPreprocessing(File file) {
public void finishedPreprocessing(File file) {
// From 16.3.5 "Scope of macro definitions":
// A macro definition lasts (independent of block structure) until
// a corresponding #undef directive is encountered or (if none
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public void test() {
Collection<File> files = listFiles();
for (File file : files) {
parser.parse(file);
CxxParser.finishedParsing(file);
}
}

Expand Down

0 comments on commit 3f8a5e5

Please sign in to comment.