1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Bug 436616 - NPE in ASTCommenter$PreprocessorRangeChecker.checkOffsets

This commit is contained in:
Sergey Prigogin 2014-06-04 10:37:21 -07:00
parent aa70cfe5d7
commit 0aa493f3d8

View file

@ -1,5 +1,5 @@
/******************************************************************************* /*******************************************************************************
* Copyright (c) 2008, 2013 Institute for Software, HSR Hochschule fuer Technik * Copyright (c) 2008, 2014 Institute for Software, HSR Hochschule fuer Technik
* Rapperswil, University of applied sciences and others * Rapperswil, University of applied sciences and others
* All rights reserved. This program and the accompanying materials * All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0 * are made available under the terms of the Eclipse Public License v1.0
@ -41,12 +41,13 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateParameter;
/** /**
* This is the starting point of the entire comment handling process. The creation of the * This is the starting point of the entire comment handling process. The creation of the
* NodeCommentMap is based on the IASTTranslationUnit. From this TranslationUnit the comments * {@link NodeCommentMap} is based on the {@link IASTTranslationUnit}. From this translation unit
* are extracted and skipped if they belong not to the same workspace. An ASTCommenterVisitor * the comments are extracted and skipped if they belong not to the same workspace.
* is initialized with this collection of comments. And the visit process can start. * An {@link ASTCommenterVisitor} is initialized with this collection of comments. And the visit
* process can start.
* *
* @see org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommenter * @see NodeCommenter
* @see org.eclipse.cdt.internal.core.dom.rewrite.commenthandler.NodeCommentMap * @see NodeCommentMap
* *
* @author Guido Zgraggen IFS * @author Guido Zgraggen IFS
*/ */
@ -65,20 +66,23 @@ public class ASTCommenter {
private int checkOffsets(IASTNode node) { private int checkOffsets(IASTNode node) {
IASTFileLocation nodeLocation = node.getFileLocation(); IASTFileLocation nodeLocation = node.getFileLocation();
if (nodeLocation == null)
return PROCESS_SKIP;
int nodeEndOffset = nodeLocation.getNodeOffset() + nodeLocation.getNodeLength(); int nodeEndOffset = nodeLocation.getNodeOffset() + nodeLocation.getNodeLength();
int status = PROCESS_CONTINUE;
boolean nodeInBetweenCommentAndPpStmt = nodeEndOffset > commentLocation.getNodeOffset() && nodeEndOffset < ppStmtOffset; boolean nodeInBetweenCommentAndPpStmt =
nodeEndOffset > commentLocation.getNodeOffset() && nodeEndOffset < ppStmtOffset;
if (isCommentOnSameLine(node) || nodeInBetweenCommentAndPpStmt) { if (isCommentOnSameLine(node) || nodeInBetweenCommentAndPpStmt) {
isPrePpStmtComment = false; isPrePpStmtComment = false;
status = PROCESS_ABORT; return PROCESS_ABORT;
} else if (nodeEndOffset < commentLocation.getNodeOffset()) { } else if (nodeEndOffset < commentLocation.getNodeOffset()) {
status = PROCESS_SKIP; return PROCESS_SKIP;
} else if (nodeLocation.getNodeOffset() > ppStmtOffset) { } else if (nodeLocation.getNodeOffset() > ppStmtOffset) {
status = PROCESS_ABORT; return PROCESS_ABORT;
} }
return status; return PROCESS_CONTINUE;
} }
private boolean isCommentOnSameLine(IASTNode node) { private boolean isCommentOnSameLine(IASTNode node) {
@ -184,8 +188,8 @@ public class ASTCommenter {
} }
/** /**
* Adds all comments given in {@code ast} to the {@code commentMap}. Calling this twice will have no * Adds all comments given in {@code ast} to the {@code commentMap}. Calling this twice has
* effect. * no effect.
* *
* @param ast * @param ast
* the AST which contains the comments to add * the AST which contains the comments to add
@ -197,7 +201,7 @@ public class ASTCommenter {
return; return;
} }
IASTComment[] commentsArray = ast.getComments(); IASTComment[] commentsArray = ast.getComments();
List<IASTComment> comments = new ArrayList<IASTComment>(commentsArray.length); List<IASTComment> comments = new ArrayList<>(commentsArray.length);
for (IASTComment comment : commentsArray) { for (IASTComment comment : commentsArray) {
if (comment.isPartOfTranslationUnitFile()) { if (comment.isPartOfTranslationUnitFile()) {
comments.add(comment); comments.add(comment);
@ -245,7 +249,7 @@ public class ASTCommenter {
return; return;
} }
List<IASTComment> freestandingComments = new ArrayList<IASTComment>(comments.size()); List<IASTComment> freestandingComments = new ArrayList<>(comments.size());
Iterator<IASTPreprocessorStatement> statementsIter = preprocessorStatements.iterator(); Iterator<IASTPreprocessorStatement> statementsIter = preprocessorStatements.iterator();
Iterator<IASTComment> commentIter = comments.iterator(); Iterator<IASTComment> commentIter = comments.iterator();
IASTPreprocessorStatement curStatement = getNextNodeInTu(statementsIter); IASTPreprocessorStatement curStatement = getNextNodeInTu(statementsIter);