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 analysis.enumarrayliteral;
7 
8 import dparse.ast;
9 import dparse.lexer;
10 import analysis.base;
11 import std.algorithm : canFind;
12 import dsymbol.scope_ : Scope;
13 
14 void doNothing(string, size_t, size_t, string, bool)
15 {
16 }
17 
18 class EnumArrayLiteralCheck : BaseAnalyzer
19 {
20 	alias visit = BaseAnalyzer.visit;
21 
22 	this(string fileName, const(Scope)* sc)
23 	{
24 		super(fileName, sc);
25 	}
26 
27 	bool looking = false;
28 
29 	mixin visitTemplate!ClassDeclaration;
30 	mixin visitTemplate!InterfaceDeclaration;
31 	mixin visitTemplate!UnionDeclaration;
32 	mixin visitTemplate!StructDeclaration;
33 
34 	override void visit(const AutoDeclaration autoDec)
35 	{
36 		if (autoDec.storageClasses.canFind!(a => a.token == tok!"enum"))
37 		{
38 			foreach (i, initializer; autoDec.initializers)
39 			{
40 				if (initializer is null)
41 					continue;
42 				if (initializer.nonVoidInitializer is null)
43 					continue;
44 				if (initializer.nonVoidInitializer.arrayInitializer is null)
45 					continue;
46 				addErrorMessage(autoDec.identifiers[i].line, autoDec.identifiers[i].column,
47 						"dscanner.performance.enum_array_literal",
48 						"This enum may lead to unnecessary allocation at run-time."
49 						~ " Use 'static immutable "
50 						~ autoDec.identifiers[i].text ~ " = [ ...' instead.");
51 			}
52 		}
53 		autoDec.accept(this);
54 	}
55 }