From 0e89df07f84dd2ce2a5cf52ff12cfbb3f65b6c93 Mon Sep 17 00:00:00 2001 From: Hackerpilot Date: Mon, 26 Jan 2015 01:34:28 -0800 Subject: [PATCH] Code cleanup --- src/analysis/local_imports.d | 3 +++ src/analysis/numbers.d | 9 ++++++--- src/analysis/undocumented.d | 11 +++++++---- src/astprinter.d | 21 ++++++++++++++------- src/ctags.d | 12 ++++++++++-- src/imports.d | 8 ++++++-- 6 files changed, 46 insertions(+), 18 deletions(-) diff --git a/src/analysis/local_imports.d b/src/analysis/local_imports.d index 71ecb50..002eda9 100644 --- a/src/analysis/local_imports.d +++ b/src/analysis/local_imports.d @@ -19,6 +19,9 @@ class LocalImportCheck : BaseAnalyzer { alias visit = BaseAnalyzer.visit; + /** + * Construct with the given file name. + */ this(string fileName) { super(fileName); diff --git a/src/analysis/numbers.d b/src/analysis/numbers.d index 384cd2f..2227589 100644 --- a/src/analysis/numbers.d +++ b/src/analysis/numbers.d @@ -17,8 +17,12 @@ import analysis.helpers; */ class NumberStyleCheck : BaseAnalyzer { +public: alias visit = BaseAnalyzer.visit; + /** + * Constructs the style checker with the given file name. + */ this(string fileName) { super(fileName); @@ -26,7 +30,6 @@ class NumberStyleCheck : BaseAnalyzer override void visit(const Token t) { - import std.algorithm; if (isNumberLiteral(t.type) && !t.text.startsWith("0x") && ((t.text.startsWith("0b") && !t.text.matchFirst(badBinaryRegex).empty) || !t.text.matchFirst(badDecimalRegex).empty)) @@ -35,14 +38,14 @@ class NumberStyleCheck : BaseAnalyzer "Use underscores to improve number constant readability."); } } - +private: auto badBinaryRegex = ctRegex!(`^0b[01]{9,}`); auto badDecimalRegex = ctRegex!(`^\d{5,}`); } unittest { - import analysis.config; + import analysis.config : StaticAnalysisConfig; StaticAnalysisConfig sac; sac.number_style_check = true; assertAnalyzerWarnings(q{ diff --git a/src/analysis/undocumented.d b/src/analysis/undocumented.d index c7c1019..165f807 100644 --- a/src/analysis/undocumented.d +++ b/src/analysis/undocumented.d @@ -43,16 +43,19 @@ class UndocumentedDeclarationCheck : BaseAnalyzer } } - bool shouldPop = false; - bool prevOverride = getOverride(); + immutable bool shouldPop = dec.attributeDeclaration is null; + immutable bool prevOverride = getOverride(); bool ovr = false; + bool pushed = false; foreach (attribute; dec.attributes) { - shouldPop = dec.attributeDeclaration !is null; if (isProtection(attribute.attribute.type)) { if (shouldPop) + { + pushed = true; push(attribute.attribute.type); + } else set(attribute.attribute.type); } @@ -62,7 +65,7 @@ class UndocumentedDeclarationCheck : BaseAnalyzer if (ovr) setOverride(true); dec.accept(this); - if (shouldPop) + if (shouldPop && pushed) pop(); if (ovr) setOverride(prevOverride); diff --git a/src/astprinter.d b/src/astprinter.d index 019c2de..5d0bed0 100644 --- a/src/astprinter.d +++ b/src/astprinter.d @@ -10,13 +10,9 @@ import std.stdio; import std.string; import std.array; -template tagAndAccept(string tagName) -{ - immutable tagAndAccept = `output.writeln("<` ~ tagName ~ `>");` - ~ tagName ~ `.accept(this);` - ~ `output.writeln("");`; -} - +/** + * AST visitor that outputs an XML representation of the AST to its file. + */ class XMLPrinter : ASTVisitor { override void visit(const AddExpression addExpression) @@ -1094,6 +1090,17 @@ class XMLPrinter : ASTVisitor output.writeln("", xmlEscape(comment), ""); } + /** + * File that output is written to. + */ File output; } +private: + +template tagAndAccept(string tagName) +{ + immutable tagAndAccept = `output.writeln("<` ~ tagName ~ `>");` + ~ tagName ~ `.accept(this);` + ~ `output.writeln("");`; +} diff --git a/src/ctags.d b/src/ctags.d index 8a896e4..5ea435b 100644 --- a/src/ctags.d +++ b/src/ctags.d @@ -14,8 +14,12 @@ import std.stdio; import std.array; import std.conv; -void doNothing(string, size_t, size_t, string, bool) {} - +/** + * Prints CTAGS information to the given file. + * Params: + * outpt = the file that CTAGS info is written to + * fileNames = tags will be generated from these files + */ void printCtags(File output, string[] fileNames) { string[] tags; @@ -41,6 +45,10 @@ void printCtags(File output, string[] fileNames) tags.sort().copy(output.lockingTextWriter); } +private: + +void doNothing(string, size_t, size_t, string, bool) {} + class CTagsPrinter : ASTVisitor { override void visit(const ClassDeclaration dec) diff --git a/src/imports.d b/src/imports.d index 6670dba..5009a68 100644 --- a/src/imports.d +++ b/src/imports.d @@ -9,6 +9,9 @@ import std.d.ast; import std.stdio; import std.container; +/** + * AST visitor that collects modules imported to an R-B tree. + */ class ImportPrinter : ASTVisitor { this() @@ -38,9 +41,10 @@ class ImportPrinter : ASTVisitor imports.insert(s); } - RedBlackTree!string imports; - alias visit = ASTVisitor.visit; +private: + + RedBlackTree!string imports; bool ignore = true; }