mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-04-29 19:45:01 +02:00
Proper handling of comments before preprocessing statements by Emanuel Graf, bug 232232.
This commit is contained in:
parent
e250ee6a33
commit
fec33bb137
2 changed files with 127 additions and 0 deletions
|
@ -2960,3 +2960,97 @@ void Demo::methode3()
|
||||||
{
|
{
|
||||||
} = /*Am Schluss*/
|
} = /*Am Schluss*/
|
||||||
|
|
||||||
|
//!CommentRecognitionAtTheBeginningBeforePreprocessorStatement 1
|
||||||
|
//#org.eclipse.cdt.core.parser.tests.rewrite.comenthandler.CommentHandlingTest
|
||||||
|
//@test.h
|
||||||
|
//TEST
|
||||||
|
#ifndef TEST_H_
|
||||||
|
#define TEST_H_
|
||||||
|
|
||||||
|
class test
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//=
|
||||||
|
=>leading
|
||||||
|
=>trailing
|
||||||
|
=>freestanding
|
||||||
|
|
||||||
|
//!CommentRecognitionAtTheBeginningBeforePreprocessorStatement 2
|
||||||
|
//#org.eclipse.cdt.core.parser.tests.rewrite.comenthandler.CommentHandlingTest
|
||||||
|
//@test.h
|
||||||
|
/*
|
||||||
|
* Licence information...
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef TEST_H_
|
||||||
|
#define TEST_H_
|
||||||
|
|
||||||
|
class test
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//=
|
||||||
|
=>leading
|
||||||
|
=>trailing
|
||||||
|
=>freestanding
|
||||||
|
|
||||||
|
//!CommentRecognitionAtTheBeginningBeforePreprocessorStatement 3
|
||||||
|
//#org.eclipse.cdt.core.parser.tests.rewrite.comenthandler.CommentHandlingTest
|
||||||
|
//@test.h
|
||||||
|
/*
|
||||||
|
* Licence information...
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef TEST_H_
|
||||||
|
#define TEST_H_
|
||||||
|
|
||||||
|
//test
|
||||||
|
class test
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
//=
|
||||||
|
=>leading
|
||||||
|
class test
|
||||||
|
{
|
||||||
|
}; = //test
|
||||||
|
=>trailing
|
||||||
|
=>freestanding
|
||||||
|
|
||||||
|
//!CommentRecognitionAtTheBeginningBeforePreprocessorStatement 4
|
||||||
|
//#org.eclipse.cdt.core.parser.tests.rewrite.comenthandler.CommentHandlingTest
|
||||||
|
//@test.h
|
||||||
|
/*
|
||||||
|
* HideMethod.h
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef HIDEMETHOD_H_
|
||||||
|
#define HIDEMETHOD_H_
|
||||||
|
|
||||||
|
|
||||||
|
class HideMethod {
|
||||||
|
public:
|
||||||
|
HideMethod();
|
||||||
|
virtual ~HideMethod();
|
||||||
|
void methode2();
|
||||||
|
void methode3();
|
||||||
|
|
||||||
|
private:
|
||||||
|
int i;
|
||||||
|
bool isOk;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* HIDEMETHOD_H_ */
|
||||||
|
|
||||||
|
//=
|
||||||
|
=>leading
|
||||||
|
=>trailing
|
||||||
|
=>freestanding
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
package org.eclipse.cdt.internal.core.dom.rewrite.commenthandler;
|
package org.eclipse.cdt.internal.core.dom.rewrite.commenthandler;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
|
|
||||||
import org.eclipse.cdt.core.dom.ast.IASTComment;
|
import org.eclipse.cdt.core.dom.ast.IASTComment;
|
||||||
|
@ -83,10 +84,12 @@ public class ASTCommenter {
|
||||||
private static ArrayList<IASTComment> removeAllPreprocessorComments(IASTTranslationUnit tu, ArrayList<IASTComment> comments) {
|
private static ArrayList<IASTComment> removeAllPreprocessorComments(IASTTranslationUnit tu, ArrayList<IASTComment> comments) {
|
||||||
IASTPreprocessorStatement[] preprocessorStatements = tu.getAllPreprocessorStatements();
|
IASTPreprocessorStatement[] preprocessorStatements = tu.getAllPreprocessorStatements();
|
||||||
TreeMap<Integer,Object> treeOfPreProcessorLines = new TreeMap<Integer,Object>();
|
TreeMap<Integer,Object> treeOfPreProcessorLines = new TreeMap<Integer,Object>();
|
||||||
|
ArrayList<Integer> listOfPreProcessorOffset = new ArrayList<Integer>();
|
||||||
|
|
||||||
for (IASTPreprocessorStatement statement : preprocessorStatements) {
|
for (IASTPreprocessorStatement statement : preprocessorStatements) {
|
||||||
if (isInWorkspace(statement)) {
|
if (isInWorkspace(statement)) {
|
||||||
treeOfPreProcessorLines.put(OffsetHelper.getStartingLineNumber(statement),null);
|
treeOfPreProcessorLines.put(OffsetHelper.getStartingLineNumber(statement),null);
|
||||||
|
listOfPreProcessorOffset.add(statement.getFileLocation().getNodeOffset());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -96,11 +99,41 @@ public class ASTCommenter {
|
||||||
if (treeOfPreProcessorLines.containsKey(comStartLineNumber)) {
|
if (treeOfPreProcessorLines.containsKey(comStartLineNumber)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if(commentIsAtTheBeginningBeforePreprocessorStatements(comment, listOfPreProcessorOffset)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
commentsInCode.add(comment);
|
commentsInCode.add(comment);
|
||||||
}
|
}
|
||||||
return commentsInCode;
|
return commentsInCode;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean commentIsAtTheBeginningBeforePreprocessorStatements(
|
||||||
|
IASTComment comment,
|
||||||
|
ArrayList<Integer> listOfPreProcessorOffset) {
|
||||||
|
if(listOfPreProcessorOffset.size() <1) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(comment.getTranslationUnit()==null || comment.getTranslationUnit().getDeclarations().length < 1) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
IASTDeclaration decl = comment.getTranslationUnit().getDeclarations()[0];
|
||||||
|
if(decl.getFileLocation().getNodeOffset() < comment.getFileLocation().getNodeOffset()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Collections.sort(listOfPreProcessorOffset);
|
||||||
|
if(listOfPreProcessorOffset.get(0) < comment.getFileLocation().getNodeOffset()) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(listOfPreProcessorOffset.get(0) < decl.getFileLocation().getNodeOffset()) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
private static boolean isInWorkspace(IASTNode node) {
|
private static boolean isInWorkspace(IASTNode node) {
|
||||||
IPath workspacePath = Platform.getLocation();
|
IPath workspacePath = Platform.getLocation();
|
||||||
IPath nodePath = new Path(node.getContainingFilename());
|
IPath nodePath = new Path(node.getContainingFilename());
|
||||||
|
|
Loading…
Add table
Reference in a new issue