1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

Bug 534420 - Add support for [[maybe_unused]] attribute

Change-Id: I7214b4f9b11a190c81bad50dace87c69fe404886
Signed-off-by: Marco Stornelli <marco.stornelli@gmail.com>
This commit is contained in:
Marco Stornelli 2020-01-25 10:04:16 +01:00
parent c0402d5d06
commit 24ce60ba3f
2 changed files with 11 additions and 2 deletions

View file

@ -57,6 +57,7 @@ import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPDeferredFunction; import org.eclipse.cdt.core.dom.ast.cpp.ICPPDeferredFunction;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction; import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod; import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
import org.eclipse.cdt.core.parser.StandardAttributes;
import org.eclipse.cdt.core.parser.util.AttributeUtil; import org.eclipse.cdt.core.parser.util.AttributeUtil;
/** /**
@ -72,7 +73,7 @@ public class UnusedSymbolInFileScopeChecker extends AbstractIndexAstChecker {
* Various attributes that when present for a symbol should make it considered as used. * Various attributes that when present for a symbol should make it considered as used.
*/ */
private static final String[] USAGE_ATTRIBUTES = new String[] { "__unused__", "unused", "constructor", //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ private static final String[] USAGE_ATTRIBUTES = new String[] { "__unused__", "unused", "constructor", //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
"destructor" }; //$NON-NLS-1$ "destructor", StandardAttributes.MAYBE_UNUSED }; //$NON-NLS-1$
private Map<IBinding, IASTDeclarator> externFunctionDeclarations = new HashMap<>(); private Map<IBinding, IASTDeclarator> externFunctionDeclarations = new HashMap<>();
private Map<IBinding, IASTDeclarator> staticFunctionDeclarations = new HashMap<>(); private Map<IBinding, IASTDeclarator> staticFunctionDeclarations = new HashMap<>();
@ -145,7 +146,7 @@ public class UnusedSymbolInFileScopeChecker extends AbstractIndexAstChecker {
IASTSimpleDeclaration simpleDeclaration = (IASTSimpleDeclaration) element; IASTSimpleDeclaration simpleDeclaration = (IASTSimpleDeclaration) element;
IASTDeclSpecifier declSpec = simpleDeclaration.getDeclSpecifier(); IASTDeclSpecifier declSpec = simpleDeclaration.getDeclSpecifier();
boolean hasUsageAttrib = hasUsageAttribute(declSpec); boolean hasUsageAttrib = hasUsageAttribute(declSpec) || hasUsageAttribute(simpleDeclaration);
IASTDeclarator[] declarators = simpleDeclaration.getDeclarators(); IASTDeclarator[] declarators = simpleDeclaration.getDeclarators();
for (IASTDeclarator decl : declarators) { for (IASTDeclarator decl : declarators) {
IASTName astName = decl.getName(); IASTName astName = decl.getName();

View file

@ -384,4 +384,12 @@ public class UnusedSymbolInFileScopeCheckerTest extends CheckerTestCase {
loadCodeAndRunCpp(getAboveComment()); loadCodeAndRunCpp(getAboveComment());
checkNoErrors(); checkNoErrors();
} }
// [[maybe_unused]] static int v1;
// [[maybe_unused]] int f1();
// [[maybe_unused]] extern int f2();
public void testAttributeCpp17Unused() throws Exception {
loadCodeAndRunCpp(getAboveComment());
checkNoErrors();
}
} }