From 3df1324e9b524a6c409450cb3b8e169fabc8ce61 Mon Sep 17 00:00:00 2001 From: Hackerpilot Date: Sun, 26 Jan 2014 23:29:45 -0800 Subject: [PATCH] Relaxed rules on number formatting for #84. Implemented #86 --- analysis/base.d | 12 +++++++ analysis/enumarrayliteral.d | 11 ------- analysis/numbers.d | 6 ++-- analysis/objectconst.d | 65 +++++++++++++++++++++++++++++++++++++ analysis/run.d | 8 +++-- 5 files changed, 87 insertions(+), 15 deletions(-) create mode 100644 analysis/objectconst.d diff --git a/analysis/base.d b/analysis/base.d index a5d6ae9..46a1986 100644 --- a/analysis/base.d +++ b/analysis/base.d @@ -18,6 +18,18 @@ public: protected: + bool inAggregate = false; + + template visitTemplate(T) + { + override void visit(T structDec) + { + inAggregate = true; + structDec.accept(this); + inAggregate = false; + } + } + import core.vararg; void addErrorMessage(size_t line, size_t column, string message) diff --git a/analysis/enumarrayliteral.d b/analysis/enumarrayliteral.d index 93d7458..55a0696 100644 --- a/analysis/enumarrayliteral.d +++ b/analysis/enumarrayliteral.d @@ -20,19 +20,8 @@ class EnumArrayLiteralCheck : BaseAnalyzer super(fileName); } - bool inAggregate = false; bool looking = false; - template visitTemplate(T) - { - override void visit(T structDec) - { - inAggregate = true; - structDec.accept(this); - inAggregate = false; - } - } - mixin visitTemplate!ClassDeclaration; mixin visitTemplate!InterfaceDeclaration; mixin visitTemplate!UnionDeclaration; diff --git a/analysis/numbers.d b/analysis/numbers.d index 4ad2dd2..2a75017 100644 --- a/analysis/numbers.d +++ b/analysis/numbers.d @@ -24,7 +24,9 @@ class NumberStyleCheck : BaseAnalyzer override void visit(Token t) { - if (isNumberLiteral(t.type) && (t.text.matchFirst(badBinaryRegex) + import std.algorithm; + if (isNumberLiteral(t.type) && !t.text.startsWith("0x") + && ((t.text.startsWith("0b") && t.text.matchFirst(badBinaryRegex)) || t.text.matchFirst(badDecimalRegex))) { addErrorMessage(t.line, t.column, @@ -33,5 +35,5 @@ class NumberStyleCheck : BaseAnalyzer } auto badBinaryRegex = ctRegex!(`0b[01]{9,}`); - auto badDecimalRegex = ctRegex!(`\d{4,}`); + auto badDecimalRegex = ctRegex!(`\d{5,}`); } diff --git a/analysis/objectconst.d b/analysis/objectconst.d new file mode 100644 index 0000000..c9ba531 --- /dev/null +++ b/analysis/objectconst.d @@ -0,0 +1,65 @@ +// Copyright Brian Schott (Sir Alaran) 2014. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +module analysis.objectconst; + +import std.regex; +import stdx.d.ast; +import stdx.d.lexer; +import analysis.base; + +/** + * Checks for use of the deprecated "delete" keyword + */ +class ObjectConstCheck : BaseAnalyzer +{ + alias visit = BaseAnalyzer.visit; + + this(string fileName) + { + super(fileName); + } + + mixin visitTemplate!ClassDeclaration; + mixin visitTemplate!InterfaceDeclaration; + mixin visitTemplate!UnionDeclaration; + mixin visitTemplate!StructDeclaration; + + override void visit(Declaration d) + { + if (inAggregate && d.functionDeclaration !is null + && isInteresting(d.functionDeclaration.name.text) + && (!hasConst(d.attributes) + && !hasConst(d.functionDeclaration.memberFunctionAttributes))) + { + addErrorMessage(d.functionDeclaration.name.line, + d.functionDeclaration.name.column, "opCmp, ToHash, opEquals," + ~ " and toString should be declared const"); + } + d.accept(this); + } + + private static bool hasConst(Attribute[] attributes) + { + import std.algorithm; + return attributes.any!(a => a.attribute == tok!"const" + || (a.storageClass !is null && a.storageClass.token == tok!"const")); + } + + private static bool hasConst(MemberFunctionAttribute[] attributes) + { + import std.algorithm; + return attributes.any!(a => a.tokenType == tok!"const"); + } + + private static bool isInteresting(string name) + { + return name == "opCmp" || name == "toHash" || name == "opEquals" + || name == "toString"; + } + + private bool looking = false; + +} diff --git a/analysis/run.d b/analysis/run.d index b8da31b..12d935f 100644 --- a/analysis/run.d +++ b/analysis/run.d @@ -18,6 +18,7 @@ import analysis.pokemon; import analysis.del; import analysis.fish; import analysis.numbers; +import analysis.objectconst; void messageFunction(string fileName, size_t line, size_t column, string message, bool isError) @@ -75,9 +76,12 @@ void analyze(File output, string[] fileNames, bool staticAnalyze = true) auto numbers = new NumberStyleCheck(fileName); numbers.visit(m); + auto objConst = new ObjectConstCheck(fileName); + objConst.visit(m); + foreach (message; sort(chain(enums.messages, style.messages, - pokemon.messages, del.messages, fish.messages, numbers.messages - ).array)) + pokemon.messages, del.messages, fish.messages, numbers.messages, + objConst.messages).array)) { writeln(message); }