mirror of
https://github.com/eclipse-cdt/cdt
synced 2025-06-05 08:46:02 +02:00
Cosmetics.
This commit is contained in:
parent
acbd1e14f8
commit
7020c95a8b
1 changed files with 25 additions and 30 deletions
|
@ -59,7 +59,7 @@ import org.eclipse.cdt.core.model.ITranslationUnit;
|
||||||
import org.eclipse.core.runtime.CoreException;
|
import org.eclipse.core.runtime.CoreException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Useful functions for doing code analysis on c/c++ AST
|
* Useful functions for doing code analysis on C/C++ AST.
|
||||||
*/
|
*/
|
||||||
public final class CxxAstUtils {
|
public final class CxxAstUtils {
|
||||||
public static class NameFinderVisitor extends ASTVisitor {
|
public static class NameFinderVisitor extends ASTVisitor {
|
||||||
|
@ -102,10 +102,11 @@ public final class CxxAstUtils {
|
||||||
try {
|
try {
|
||||||
while (typeName instanceof ITypedef) {
|
while (typeName instanceof ITypedef) {
|
||||||
IType t = ((ITypedef) typeName).getType();
|
IType t = ((ITypedef) typeName).getType();
|
||||||
if (t instanceof IBinding)
|
if (t instanceof IBinding) {
|
||||||
typeName = (IBinding) t;
|
typeName = (IBinding) t;
|
||||||
else
|
} else {
|
||||||
return t;
|
return t;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) { // in CDT 6.0 getType throws DOMException
|
} catch (Exception e) { // in CDT 6.0 getType throws DOMException
|
||||||
Activator.log(e);
|
Activator.log(e);
|
||||||
|
@ -145,33 +146,32 @@ public final class CxxAstUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param astName
|
* @param astName the name for the declaration
|
||||||
* a name for the declaration
|
* @param factory the factory
|
||||||
* @param factory
|
* @param index the index
|
||||||
* the factory
|
* @return the created declaration
|
||||||
* @param index
|
|
||||||
* @return
|
|
||||||
*/
|
*/
|
||||||
public static IASTDeclaration createDeclaration(IASTName astName, INodeFactory factory, IIndex index) {
|
public static IASTDeclaration createDeclaration(IASTName astName, INodeFactory factory, IIndex index) {
|
||||||
// Depending on context, either a type or a declaration is easier to
|
// Depending on context, either a type or a declaration is easier to infer.
|
||||||
// infer
|
|
||||||
IType inferredType = null;
|
IType inferredType = null;
|
||||||
IASTSimpleDeclaration declaration = null;
|
IASTSimpleDeclaration declaration = null;
|
||||||
inferredType = tryInferTypeFromBinaryExpr(astName);
|
inferredType = tryInferTypeFromBinaryExpr(astName);
|
||||||
if (inferredType == null)
|
if (inferredType == null)
|
||||||
declaration = tryInferTypeFromFunctionCall(astName, factory, index);
|
declaration = tryInferTypeFromFunctionCall(astName, factory, index);
|
||||||
// After the inference, create the statement is needed
|
// After the inference, create the statement is needed.
|
||||||
if (declaration != null) { // A declaration was generated
|
if (declaration != null) {
|
||||||
|
// A declaration was generated.
|
||||||
return declaration;
|
return declaration;
|
||||||
} else if (inferredType != null) { // A type was inferred, create the
|
} else if (inferredType != null) {
|
||||||
// declaration and return it
|
// A type was inferred, create the declaration and return it.
|
||||||
DeclarationGenerator generator = DeclarationGenerator.create(factory);
|
DeclarationGenerator generator = DeclarationGenerator.create(factory);
|
||||||
IASTDeclarator declarator = generator.createDeclaratorFromType(inferredType, astName.toCharArray());
|
IASTDeclarator declarator = generator.createDeclaratorFromType(inferredType, astName.toCharArray());
|
||||||
IASTDeclSpecifier declspec = generator.createDeclSpecFromType(inferredType);
|
IASTDeclSpecifier declspec = generator.createDeclSpecFromType(inferredType);
|
||||||
IASTSimpleDeclaration simpleDeclaration = factory.newSimpleDeclaration(declspec);
|
IASTSimpleDeclaration simpleDeclaration = factory.newSimpleDeclaration(declspec);
|
||||||
simpleDeclaration.addDeclarator(declarator);
|
simpleDeclaration.addDeclarator(declarator);
|
||||||
return simpleDeclaration;
|
return simpleDeclaration;
|
||||||
} else { // Fallback - return a `void` declaration
|
} else {
|
||||||
|
// Fallback - return a `void` declaration.
|
||||||
IASTDeclarator declarator = factory.newDeclarator(astName.copy(CopyStyle.withLocations));
|
IASTDeclarator declarator = factory.newDeclarator(astName.copy(CopyStyle.withLocations));
|
||||||
IASTSimpleDeclSpecifier declspec = factory.newSimpleDeclSpecifier();
|
IASTSimpleDeclSpecifier declspec = factory.newSimpleDeclSpecifier();
|
||||||
declspec.setType(Kind.eInt);
|
declspec.setType(Kind.eInt);
|
||||||
|
@ -192,7 +192,7 @@ public final class CxxAstUtils {
|
||||||
IASTNode binaryExpr = astName.getParent().getParent();
|
IASTNode binaryExpr = astName.getParent().getParent();
|
||||||
for (IASTNode node : binaryExpr.getChildren()) {
|
for (IASTNode node : binaryExpr.getChildren()) {
|
||||||
if (node != astName.getParent()) {
|
if (node != astName.getParent()) {
|
||||||
// use this expression as type source
|
// Use this expression as type source.
|
||||||
return ((IASTExpression) node).getExpressionType();
|
return ((IASTExpression) node).getExpressionType();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -239,7 +239,7 @@ public final class CxxAstUtils {
|
||||||
}
|
}
|
||||||
try {
|
try {
|
||||||
index.acquireReadLock();
|
index.acquireReadLock();
|
||||||
Set<IIndexName> declSet = new HashSet<IIndexName>();
|
Set<IIndexName> declSet = new HashSet<>();
|
||||||
// fill declSet with proper declarations
|
// fill declSet with proper declarations
|
||||||
for (IBinding b : bindings) {
|
for (IBinding b : bindings) {
|
||||||
if (b instanceof IFunction) {
|
if (b instanceof IFunction) {
|
||||||
|
@ -251,7 +251,7 @@ public final class CxxAstUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
HashMap<ITranslationUnit, IASTTranslationUnit> astCache = new HashMap<ITranslationUnit, IASTTranslationUnit>();
|
HashMap<ITranslationUnit, IASTTranslationUnit> astCache = new HashMap<>();
|
||||||
for (IIndexName decl : declSet) {
|
for (IIndexName decl : declSet) {
|
||||||
// for now, just use the first overload found
|
// for now, just use the first overload found
|
||||||
ITranslationUnit tu = getTranslationUnitFromIndexName(decl);
|
ITranslationUnit tu = getTranslationUnitFromIndexName(decl);
|
||||||
|
@ -260,7 +260,7 @@ public final class CxxAstUtils {
|
||||||
}
|
}
|
||||||
|
|
||||||
IASTTranslationUnit ast = null;
|
IASTTranslationUnit ast = null;
|
||||||
if(astCache.containsKey(tu)) {
|
if (astCache.containsKey(tu)) {
|
||||||
ast = astCache.get(tu);
|
ast = astCache.get(tu);
|
||||||
} else {
|
} else {
|
||||||
ast = tu.getAST(index, ITranslationUnit.AST_SKIP_INDEXED_HEADERS);
|
ast = tu.getAST(index, ITranslationUnit.AST_SKIP_INDEXED_HEADERS);
|
||||||
|
@ -328,9 +328,9 @@ public final class CxxAstUtils {
|
||||||
* @return Either a type specifier or null
|
* @return Either a type specifier or null
|
||||||
*/
|
*/
|
||||||
public static IASTCompositeTypeSpecifier getCompositeTypeFromFunction(final IASTFunctionDefinition function, final IIndex index) {
|
public static IASTCompositeTypeSpecifier getCompositeTypeFromFunction(final IASTFunctionDefinition function, final IIndex index) {
|
||||||
// return value to be set via visitor
|
// Return value to be set via visitor.
|
||||||
final IASTCompositeTypeSpecifier returnSpecifier[] = { null };
|
final IASTCompositeTypeSpecifier returnSpecifier[] = { null };
|
||||||
final HashMap<ITranslationUnit, IASTTranslationUnit> astCache = new HashMap<ITranslationUnit, IASTTranslationUnit>();
|
final HashMap<ITranslationUnit, IASTTranslationUnit> astCache = new HashMap<>();
|
||||||
function.accept(new ASTVisitor() {
|
function.accept(new ASTVisitor() {
|
||||||
{
|
{
|
||||||
shouldVisitDeclarators = true;
|
shouldVisitDeclarators = true;
|
||||||
|
@ -342,8 +342,7 @@ public final class CxxAstUtils {
|
||||||
if (!(name instanceof ICPPASTQualifiedName && name.getParent().getParent() == function))
|
if (!(name instanceof ICPPASTQualifiedName && name.getParent().getParent() == function))
|
||||||
return PROCESS_CONTINUE;
|
return PROCESS_CONTINUE;
|
||||||
ICPPASTQualifiedName qname = (ICPPASTQualifiedName) name;
|
ICPPASTQualifiedName qname = (ICPPASTQualifiedName) name;
|
||||||
// A qualified name may have 1 name, but in our case needs to
|
// A qualified name may have 1 name, but in our case needs to have 2.
|
||||||
// have 2.
|
|
||||||
// The pre-last name is either a namespace or a class.
|
// The pre-last name is either a namespace or a class.
|
||||||
if (qname.getChildren().length < 2) {
|
if (qname.getChildren().length < 2) {
|
||||||
return PROCESS_CONTINUE;
|
return PROCESS_CONTINUE;
|
||||||
|
@ -388,10 +387,6 @@ public final class CxxAstUtils {
|
||||||
return returnSpecifier[0];
|
return returnSpecifier[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param body
|
|
||||||
* @return
|
|
||||||
*/
|
|
||||||
public static boolean isThrowStatement(IASTNode body) {
|
public static boolean isThrowStatement(IASTNode body) {
|
||||||
if (!(body instanceof IASTExpressionStatement))
|
if (!(body instanceof IASTExpressionStatement))
|
||||||
return false;
|
return false;
|
||||||
|
@ -409,10 +404,10 @@ public final class CxxAstUtils {
|
||||||
return false;
|
return false;
|
||||||
IASTExpression functionNameExpression = ((IASTFunctionCallExpression) expression).getFunctionNameExpression();
|
IASTExpression functionNameExpression = ((IASTFunctionCallExpression) expression).getFunctionNameExpression();
|
||||||
if (functionNameExpression instanceof IASTIdExpression) {
|
if (functionNameExpression instanceof IASTIdExpression) {
|
||||||
IASTName name = ((IASTIdExpression)functionNameExpression).getName();
|
IASTName name = ((IASTIdExpression) functionNameExpression).getName();
|
||||||
|
|
||||||
IBinding binding = name.resolveBinding();
|
IBinding binding = name.resolveBinding();
|
||||||
if (binding!=null && binding instanceof IFunction && ((IFunction)binding).isNoReturn()) {
|
if (binding != null && binding instanceof IFunction && ((IFunction) binding).isNoReturn()) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue