Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import overrides autoimported #542

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!--
~ Copyright 2012-2014 Sergey Ignatov
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<html>
<body>
Ambiguous call of overridden pre R14 auto-imported BIF
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!--
~ Copyright 2012-2014 Sergey Ignatov
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<html>
<body>
Defining already imported function.
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<!--
~ Copyright 2012-2014 Sergey Ignatov
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->

<html>
<body>
Import directive overrides pre R14 auto-imported BIF.
</body>
</html>
9 changes: 9 additions & 0 deletions src/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,15 @@
<localInspection language="Erlang" shortName="Erlang17Syntax" displayName="Erlang 17.0 syntax"
groupName="Erlang" enabledByDefault="true" level="ERROR"
implementationClass="org.intellij.erlang.inspection.Erlang17SyntaxInspection"/>
<localInspection language="Erlang" shortName="ErlangDefiningImportedFunction" displayName="Defining imported function"
groupName="Erlang" enabledByDefault="true" level="ERROR"
implementationClass="org.intellij.erlang.inspection.ErlangDefiningImportedFunctionInspection"/>
<localInspection language="Erlang" shortName="ErlangAmbiguousCallOfAutoimportedFunction" displayName="Ambiguous call of auto-imported BIF"
groupName="Erlang" enabledByDefault="true" level="ERROR"
implementationClass="org.intellij.erlang.inspection.ErlangAmbiguousCallOfAutoimportedFunctionInspection"/>
<localInspection language="Erlang" shortName="ErlangImportDirectiveOverridesAutoimportedBif" displayName="Import overrides auto-imported BIF"
groupName="Erlang" enabledByDefault="true" level="ERROR"
implementationClass="org.intellij.erlang.inspection.ErlangImportDirectiveOverridesAutoimportedBifInspection"/>

<!--warnings-->
<localInspection language="Erlang" shortName="ErlangUnresolvedFunction" displayName="Unresolved function"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Copyright 2012-2014 Sergey Ignatov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.intellij.erlang.inspection;

import com.intellij.codeInspection.*;
import com.intellij.psi.PsiReference;
import org.intellij.erlang.bif.ErlangBifDescriptor;
import org.intellij.erlang.bif.ErlangBifTable;
import org.intellij.erlang.psi.*;
import org.intellij.erlang.psi.impl.ErlangFunctionReferenceImpl;
import org.intellij.erlang.quickfixes.*;
import org.intellij.erlang.sdk.ErlangSdkRelease;
import org.intellij.erlang.sdk.ErlangSdkType;
import org.jetbrains.annotations.NotNull;

public class ErlangAmbiguousCallOfAutoimportedFunctionInspection extends ErlangInspectionBase {
@Override
protected boolean canRunOn(@NotNull ErlangFile file) {
if (file.isNoAutoImportAll()) return false;
ErlangSdkRelease release = ErlangSdkType.getRelease(file);
return release == null || release.isNewerThan(ErlangSdkRelease.V_R14A);
}

@NotNull
protected ErlangVisitor buildErlangVisitor(@NotNull final ProblemsHolder holder, @NotNull LocalInspectionToolSession session) {
return new ErlangVisitor() {
@Override
public void visitFunctionCallExpression(@NotNull ErlangFunctionCallExpression o) {
if (o.getParent() instanceof ErlangGlobalFunctionCallExpression) return;
if (o.getQAtom().getMacros() != null) return;
PsiReference reference = o.getReference();
if (!(reference instanceof ErlangFunctionReferenceImpl) || reference.resolve() == null) return;
ErlangFunctionReferenceImpl r = (ErlangFunctionReferenceImpl) reference;
String name = r.getName();
int arity = r.getArity();
ErlangBifDescriptor bifDescriptor = ErlangBifTable.getBif("erlang", name, arity);
if (((ErlangFile)o.getContainingFile()).getFunction(name, arity) == null
|| bifDescriptor == null
|| !bifDescriptor.isAutoImported()
|| ((ErlangFile)o.getContainingFile()).isNoAutoImport(name, arity))
return;
holder.registerProblem(o.getNameIdentifier(),
"Ambiguous call of overridden pre R14 auto-imported BIF " + "'" + r.getSignature() + "'",
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
new ErlangSpecifyModulePrefixFix("erlang"));
}
};
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright 2012-2014 Sergey Ignatov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.intellij.erlang.inspection;

import com.intellij.codeInspection.*;
import com.intellij.util.containers.ContainerUtil;
import org.intellij.erlang.ErlangFileType;
import org.intellij.erlang.psi.ErlangFile;
import org.intellij.erlang.psi.ErlangFunction;
import org.intellij.erlang.psi.ErlangImportFunction;
import org.intellij.erlang.psi.impl.ErlangPsiImplUtil;
import org.intellij.erlang.quickfixes.ErlangRemoveFunctionFix;
import org.intellij.erlang.quickfixes.ErlangRemoveFunctionFromImportFixBase;
import org.jetbrains.annotations.NotNull;

import java.util.Set;

public class ErlangDefiningImportedFunctionInspection extends ErlangInspectionBase {
@Override
protected boolean canRunOn(@NotNull ErlangFile file) {
return file.getFileType() == ErlangFileType.MODULE && !file.isExportedAll();
}

protected void checkFile(@NotNull ErlangFile file, @NotNull ProblemsHolder problemsHolder) {
Set<String> importedFunctionNames = ContainerUtil.newHashSet();
for (ErlangImportFunction f : file.getImportedFunctions()) {
importedFunctionNames.add(ErlangPsiImplUtil.createFunctionPresentation(f));
}
for (ErlangFunction function : file.getFunctions()) {
String fullName = ErlangPsiImplUtil.createFunctionPresentation(function);
if (importedFunctionNames.contains(fullName)) {
problemsHolder.registerProblem(InspectionManager.getInstance(file.getProject()).createProblemDescriptor(
function.getNameIdentifier(),
function.getFunctionClauseList().get(0).getArgumentDefinitionList().getOriginalElement(),
"Defining imported function '" + fullName + "'",
ProblemHighlightType.GENERIC_ERROR_OR_WARNING, true,
new ErlangRemoveFunctionFix(),
new ErlangRemoveFunctionFromImportFixBase.ErlangRemoveFunctionFromAllImportsFix()));
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2012-2014 Sergey Ignatov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.intellij.erlang.inspection;

import com.intellij.codeInspection.ProblemHighlightType;
import com.intellij.codeInspection.ProblemsHolder;
import org.intellij.erlang.bif.ErlangBifDescriptor;
import org.intellij.erlang.bif.ErlangBifTable;
import org.intellij.erlang.psi.ErlangFile;
import org.intellij.erlang.psi.ErlangImportFunction;
import org.intellij.erlang.psi.impl.ErlangPsiImplUtil;
import org.intellij.erlang.quickfixes.ErlangRemoveFunctionFromImportFixBase;
import org.intellij.erlang.sdk.ErlangSdkRelease;
import org.intellij.erlang.sdk.ErlangSdkType;
import org.jetbrains.annotations.NotNull;

public class ErlangImportDirectiveOverridesAutoimportedBifInspection extends ErlangInspectionBase {
@Override
protected boolean canRunOn(@NotNull ErlangFile file) {
ErlangSdkRelease release = ErlangSdkType.getRelease(file);
return release == null || release.isNewerThan(ErlangSdkRelease.V_R14A);
}

protected void checkFile(@NotNull ErlangFile file, @NotNull ProblemsHolder problemsHolder) {
for (ErlangImportFunction importFunction : file.getImportedFunctions()) {
ErlangBifDescriptor bifDescriptor = ErlangBifTable.getBif(
"erlang",
ErlangPsiImplUtil.getName(importFunction.getQAtom()),
ErlangPsiImplUtil.getArity(importFunction.getInteger()));
if (bifDescriptor == null || !bifDescriptor.isAutoImported()) continue;
problemsHolder.registerProblem(importFunction,
"Import directive overrides pre R14 auto-imported BIF '" + ErlangPsiImplUtil.createFunctionPresentation(importFunction) + "'",
ProblemHighlightType.GENERIC_ERROR_OR_WARNING,
new ErlangRemoveFunctionFromImportFixBase.ErlangRemoveFunctionFromImportFix());
}
}

}
4 changes: 4 additions & 0 deletions src/org/intellij/erlang/psi/ErlangFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,12 @@ public interface ErlangFile extends PsiFile {

boolean isExported(@NotNull String signature);

boolean isNoAutoImport(@NotNull String name, int arity);

boolean isExportedAll();

boolean isNoAutoImportAll();

@NotNull
ArrayList<ErlangImportFunction> getImportedFunctions();

Expand Down
8 changes: 8 additions & 0 deletions src/org/intellij/erlang/psi/impl/ErlangElementFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ public static PsiElement createMacrosFromText(@NotNull Project project, @NotNull
return fileFromText.getMacroses().get(0).getMacrosName();
}

@NotNull
public static PsiElement createFunctionWithModuleCallExpression(@NotNull Project project,
@NotNull String moduleName,
@NotNull String functionCallExpr) {
ErlangFile fileFromText = createFileFromText(project, "f() -> " + moduleName + ":" + functionCallExpr + ".");
return fileFromText.getFunctions().get(0).getFunctionClauseList().get(0).getClauseBody().getLastChild();
}

@NotNull
public static PsiElement createStringFromText(@NotNull Project project, @NotNull String text) {
return createIncludeString(project, text).getString();
Expand Down
Loading