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