Skip to content

"Already imported" inspection #543

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

Open
wants to merge 5 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>
Function already imported.
</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>
12 changes: 12 additions & 0 deletions src/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,18 @@
<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"/>
<localInspection language="Erlang" shortName="ErlangFunctionAlreadyImported" displayName="Function already imported"
groupName="Erlang" enabledByDefault="true" level="ERROR"
implementationClass="org.intellij.erlang.inspection.ErlangFunctionAlreadyImportedInspection"/>

<!--warnings-->
<localInspection language="Erlang" shortName="ErlangUnresolvedFunction" displayName="Unresolved function"
Expand Down
15 changes: 15 additions & 0 deletions src/org/intellij/erlang/bif/ErlangBifDescriptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,23 @@ public final class ErlangBifDescriptor implements Comparable<ErlangBifDescriptor
private final int myArity;
@NotNull private final String myParams;

private final boolean myIsAutoImported;

public ErlangBifDescriptor(@NotNull String module, @NotNull String name, int arity, @NotNull String params) {
this(module, name, arity, params, false);
}

public ErlangBifDescriptor(@NotNull String module, @NotNull String name, int arity, @NotNull String params,
boolean isAutoImported) {
myModule = module;
myName = name;
myArity = arity;
myParams = params;
myIsAutoImported = isAutoImported;
}

public boolean isAutoImported() {
return myIsAutoImported;
}

@NotNull
Expand All @@ -52,6 +64,9 @@ public String getParams() {

@Override
public String toString() {
if (myModule.isEmpty()) {
return myName + "/" + myArity;
}
return myModule + ":" + myName + "/" + myArity;
}

Expand Down
16 changes: 15 additions & 1 deletion src/org/intellij/erlang/bif/ErlangBifParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.testFramework.LightProjectDescriptor;
import com.intellij.testFramework.fixtures.DefaultLightProjectDescriptor;
import com.intellij.util.containers.ContainerUtil;
import org.intellij.erlang.documentation.ErlangDocumentationProvider;
import org.intellij.erlang.psi.ErlangFunctionCallExpression;
import org.intellij.erlang.psi.ErlangGlobalFunctionCallExpression;
Expand All @@ -35,7 +36,10 @@
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -44,6 +48,7 @@ public class ErlangBifParser extends ErlangLightPlatformCodeInsightFixtureTestCa
private static final Pattern PATTERN_FUNC_DECLARATION = Pattern.compile(
"<span\\s+class=\"bold_code\">.*\\((.*)\\) -&gt;.*</span>", Pattern.MULTILINE);
private static final String BIF_TABLE_PATH = "src/org/intellij/erlang/bif/bif.tab.txt";
private static final String BIF_AUTOIMPORT_TABLE_PATH = "src/org/intellij/erlang/bif/bif.autoimport.tab.txt";
private static final String GENERATED_FILE = "src/org/intellij/erlang/bif/ErlangBifTable.java";
private static final String ERLANG_SDK_PATH = "/usr/lib/erlang";
private static final String ERLANG_DOC_PATH = "/home/ignatov/Downloads/otp_doc_html_R16B";
Expand All @@ -52,11 +57,19 @@ public class ErlangBifParser extends ErlangLightPlatformCodeInsightFixtureTestCa

private ErlangDocumentationProvider myDocProvider;

private Set<String> getAutoimportedFunctions() throws IOException {
File bifAutoimportTableFile = new File(BIF_AUTOIMPORT_TABLE_PATH);
String[] bifAutoimportTableText = StringUtil.splitByLines(FileUtilRt.loadFile(bifAutoimportTableFile));
return new HashSet<String>(ContainerUtil.set(bifAutoimportTableText));
}

public void testSomething() throws Exception {
File bifTableFile = new File(BIF_TABLE_PATH);
String[] bifTableText = StringUtil.splitByLines(FileUtilRt.loadFile(bifTableFile));

PrintStream bifTableJavaBuilder = new PrintStream(new File(GENERATED_FILE));
try {
Set<String> autoimported = getAutoimportedFunctions();
bifTableJavaBuilder.append("package org.intellij.erlang.bif;\n" +
"\n" +
"import org.jetbrains.annotations.NotNull;\n" +
Expand All @@ -83,10 +96,11 @@ public void testSomething() throws Exception {
String module = matcher.group(1);
String name = matcher.group(2);
String arity = matcher.group(3);
String isAuroimport = autoimported.contains(name + "/" + arity) ? ", true" : "";
bifTableJavaBuilder.append(" bifMap.putValue(\"").append(module)
.append("\", new ErlangBifDescriptor(\"")
.append(module).append("\", \"").append(name).append("\", ").append(arity)
.append(", \"").append(fetchSpec(module, name, Integer.valueOf(arity))).append("\"));\n");
.append(", \"").append(fetchSpec(module, name, Integer.valueOf(arity))).append("\"").append(isAuroimport).append("));\n");
}
else if ((matcher = BIF_SEPARATOR.matcher(s)).find()) {
String version = matcher.group(1).replaceAll("\\.", "");
Expand Down
Loading