From c40a860ba332716bbf08c3e898348f1c4b3ee0b7 Mon Sep 17 00:00:00 2001 From: Marc-Andre Laperle Date: Tue, 13 Mar 2012 20:21:27 -0400 Subject: [PATCH] Bug 373695 - Quick fix fails to create local variable --- .../ui/quickfix/QuickFixCreateParameter.java | 6 ++++- .../cdt/codan/core/cxx/CxxAstUtils.java | 26 ++++++++++++------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixCreateParameter.java b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixCreateParameter.java index 0ffefec2e0d..34fd96f0851 100644 --- a/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixCreateParameter.java +++ b/codan/org.eclipse.cdt.codan.checkers.ui/src/org/eclipse/cdt/codan/internal/checkers/ui/quickfix/QuickFixCreateParameter.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2010 Tomasz Wesolowski + * Copyright (c) 2010, 2012 Tomasz Wesolowski * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -7,6 +7,7 @@ * * Contributors: * Tomasz Wesolowski - initial API and implementation + * Marc-Andre Laperle *******************************************************************************/ package org.eclipse.cdt.codan.internal.checkers.ui.quickfix; @@ -74,6 +75,9 @@ public class QuickFixCreateParameter extends AbstractAstRewriteQuickFix { HashMap cachedRewrites = new HashMap(); for (IIndexName iname : declarations) { ITranslationUnit declTU = CxxAstUtils.getTranslationUnitFromIndexName(iname); + if (declTU == null) { + continue; + } ASTRewrite rewrite; IASTTranslationUnit declAST; if (!cachedASTs.containsKey(declTU)) { diff --git a/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/CxxAstUtils.java b/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/CxxAstUtils.java index 32b4897795b..4f4dc1560f0 100644 --- a/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/CxxAstUtils.java +++ b/codan/org.eclipse.cdt.codan.core.cxx/src/org/eclipse/cdt/codan/core/cxx/CxxAstUtils.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2011 Alena Laskavaia, Tomasz Wesolowski + * Copyright (c) 2009, 2012 Alena Laskavaia, Tomasz Wesolowski * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -8,6 +8,7 @@ * Contributors: * Alena Laskavaia - initial API and implementation * Tomasz Wesolowski - extension + * Marc-Andre Laperle *******************************************************************************/ package org.eclipse.cdt.codan.core.cxx; @@ -51,13 +52,11 @@ import org.eclipse.cdt.core.dom.ast.ITypedef; import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName; import org.eclipse.cdt.core.dom.rewrite.DeclarationGenerator; import org.eclipse.cdt.core.index.IIndex; +import org.eclipse.cdt.core.index.IIndexFile; import org.eclipse.cdt.core.index.IIndexName; -import org.eclipse.cdt.core.model.CoreModel; +import org.eclipse.cdt.core.model.CoreModelUtil; import org.eclipse.cdt.core.model.ITranslationUnit; -import org.eclipse.core.resources.IFile; -import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.Path; /** * Useful functions for doing code analysis on c/c++ AST @@ -254,6 +253,10 @@ public final class CxxAstUtils { for (IIndexName decl : declSet) { // for now, just use the first overload found ITranslationUnit tu = getTranslationUnitFromIndexName(decl); + if (tu == null) { + continue; + } + IASTTranslationUnit ast = null; if(astCache.containsKey(tu)) { ast = astCache.get(tu); @@ -305,10 +308,11 @@ public final class CxxAstUtils { } public static ITranslationUnit getTranslationUnitFromIndexName(IIndexName decl) throws CoreException { - Path path = new Path(decl.getFile().getLocation().getFullPath()); - IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path); - ITranslationUnit tu = (ITranslationUnit) CoreModel.getDefault().create(file); - return tu; + IIndexFile file = decl.getFile(); + if (file != null) { + return CoreModelUtil.findTranslationUnitForLocation(file.getLocation().getURI(), null); + } + return null; } /** @@ -350,6 +354,10 @@ public final class CxxAstUtils { // Check the declarations and use first suitable for (IIndexName decl : declarations) { ITranslationUnit tu = getTranslationUnitFromIndexName(decl); + if (tu == null) { + continue; + } + IASTTranslationUnit ast = null; if(astCache.containsKey(tu)) { ast = astCache.get(tu);