1 //          Copyright Brian Schott (Hackerpilot) 2014.
2 // Distributed under the Boost Software License, Version 1.0.
3 //    (See accompanying file LICENSE_1_0.txt or copy at
4 //          http://www.boost.org/LICENSE_1_0.txt)
5 
6 module dscanner.analysis.enumarrayliteral;
7 
8 import dparse.ast;
9 import dparse.lexer;
10 import dscanner.analysis.base;
11 import std.algorithm : canFind, map;
12 import dsymbol.scope_ : Scope;
13 
14 void doNothing(string, size_t, size_t, string, bool)
15 {
16 }
17 
18 final class EnumArrayLiteralCheck : BaseAnalyzer
19 {
20 	alias visit = BaseAnalyzer.visit;
21 
22 	mixin AnalyzerInfo!"enum_array_literal_check";
23 
24 	this(string fileName, const(Scope)* sc, bool skipTests = false)
25 	{
26 		super(fileName, sc, skipTests);
27 	}
28 
29 	bool looking;
30 
31 	mixin visitTemplate!ClassDeclaration;
32 	mixin visitTemplate!InterfaceDeclaration;
33 	mixin visitTemplate!UnionDeclaration;
34 	mixin visitTemplate!StructDeclaration;
35 
36 	override void visit(const AutoDeclaration autoDec)
37 	{
38 		if (autoDec.storageClasses.canFind!(a => a.token == tok!"enum"))
39 		{
40 			foreach (part; autoDec.parts)
41 			{
42 				if (part.initializer is null)
43 					continue;
44 				if (part.initializer.nonVoidInitializer is null)
45 					continue;
46 				if (part.initializer.nonVoidInitializer.arrayInitializer is null)
47 					continue;
48 				addErrorMessage(part.identifier.line, part.identifier.column,
49 						"dscanner.performance.enum_array_literal",
50 						"This enum may lead to unnecessary allocation at run-time."
51 						~ " Use 'static immutable "
52 						~ part.identifier.text ~ " = [ ...' instead.");
53 			}
54 		}
55 		autoDec.accept(this);
56 	}
57 }