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.asm_style;
7 
8 import std.stdio;
9 import std.d.ast;
10 import std.d.lexer;
11 import analysis.base;
12 import analysis.helpers;
13 
14 /**
15  * Checks for confusing asm expressions.
16  * See_also: $(LINK https://issues.dlang.org/show_bug.cgi?id=9738)
17  */
18 class AsmStyleCheck : BaseAnalyzer
19 {
20 	alias visit = BaseAnalyzer.visit;
21 
22 	this(string fileName)
23 	{
24 		super(fileName);
25 	}
26 
27 	override void visit(const AsmBrExp brExp)
28 	{
29 		if (brExp.asmBrExp !is null && brExp.asmBrExp.asmUnaExp !is null
30 			&& brExp.asmBrExp.asmUnaExp.asmPrimaryExp !is null)
31 		{
32 			addErrorMessage(brExp.line, brExp.column, "dscanner.confusing.brexp",
33 				"This is confusing because it looks like an array index. Rewrite a[1] as [a + 1] to clarify.");
34 		}
35 		brExp.accept(this);
36 	}
37 }
38 
39 unittest
40 {
41 	import analysis.config : StaticAnalysisConfig;
42 
43 	StaticAnalysisConfig sac;
44 	sac.asm_style_check = true;
45 	assertAnalyzerWarnings(q{
46 		void testAsm()
47 		{
48 			asm
49 			{
50 				mov a, someArray[1]; // [warn]: This is confusing because it looks like an array index. Rewrite a[1] as [a + 1] to clarify.
51 				add near ptr [EAX], 3;
52 			}
53 		}
54 	}}, sac);
55 
56 	stderr.writeln("Unittest for AsmStyleCheck passed.");
57 }
58