├── src ├── main │ ├── resources │ │ └── org │ │ │ └── sonar │ │ │ └── l10n │ │ │ └── pmd.properties │ └── java │ │ └── org │ │ └── sonar │ │ ├── plugins │ │ └── delphi │ │ │ ├── pmd │ │ │ ├── rules │ │ │ │ ├── EmptyFinallyBlockRule.java │ │ │ │ ├── DestructorWithoutInheritedStatementRule.java │ │ │ │ ├── DprVariableRule.java │ │ │ │ ├── RecordNameRule.java │ │ │ │ ├── ClassNameRule.java │ │ │ │ ├── InterfaceNameRule.java │ │ │ │ ├── PointerNameRule.java │ │ │ │ ├── UnitNameRule.java │ │ │ │ ├── IfNotFalseRule.java │ │ │ │ ├── WithAfterDoThenRule.java │ │ │ │ ├── FieldNameRule.java │ │ │ │ ├── IfTrueRule.java │ │ │ │ ├── MethodNameRule.java │ │ │ │ ├── ThenTryRule.java │ │ │ │ ├── ConstructorWithoutInheritedStatementRule.java │ │ │ │ ├── NoBeginAfterDoRule.java │ │ │ │ └── VariableCounter.java │ │ │ ├── DelphiPmdConstants.java │ │ │ ├── DelphiParserVisitor.java │ │ │ ├── DelphiRuleChain.java │ │ │ ├── DelphiRuleChainVisitor.java │ │ │ └── xml │ │ │ │ └── Property.java │ │ │ ├── codecoverage │ │ │ ├── CodeCoverageTool.java │ │ │ └── DelphiCodeCoverageParser.java │ │ │ ├── core │ │ │ ├── DelphiRecognizer.java │ │ │ ├── DelphiFootPrint.java │ │ │ ├── language │ │ │ │ ├── verifiers │ │ │ │ │ └── StatementVerifierException.java │ │ │ │ ├── ArgumentInterface.java │ │ │ │ ├── ClassFieldInterface.java │ │ │ │ └── HasNameInterface.java │ │ │ └── DelphiLanguage.java │ │ │ ├── antlr │ │ │ ├── ast │ │ │ │ ├── exceptions │ │ │ │ │ └── NodeNameForCodeDoesNotExistException.java │ │ │ │ ├── NodeName.java │ │ │ │ └── DelphiTreeAdaptor.java │ │ │ ├── analyzer │ │ │ │ ├── ASTAnalyzer.java │ │ │ │ ├── impl │ │ │ │ │ ├── operations │ │ │ │ │ │ ├── NodeOperation.java │ │ │ │ │ │ ├── AdvanceNodeOperation.java │ │ │ │ │ │ └── TraceBackNodeOperation.java │ │ │ │ │ └── VisibilityAnalyzer.java │ │ │ │ ├── CodeNode.java │ │ │ │ └── CodeTree.java │ │ │ ├── sanitizer │ │ │ │ ├── subranges │ │ │ │ │ ├── SubRangeFirstOccurenceComparator.java │ │ │ │ │ ├── impl │ │ │ │ │ │ └── ReplacementSubRange.java │ │ │ │ │ └── SubRange.java │ │ │ │ ├── resolvers │ │ │ │ │ ├── exceptions │ │ │ │ │ │ ├── DefineResolverException.java │ │ │ │ │ │ └── IncludeResolverException.java │ │ │ │ │ └── SourceFixerResolver.java │ │ │ │ └── SourceResolver.java │ │ │ └── directives │ │ │ │ ├── exceptions │ │ │ │ ├── CompilerDirectiveFactorySyntaxException.java │ │ │ │ └── CompilerDirectiveFactoryUnsupportedDirectiveException.java │ │ │ │ ├── impl │ │ │ │ ├── IfDirective.java │ │ │ │ ├── ElseDirective.java │ │ │ │ ├── EndIfDirective.java │ │ │ │ ├── IfEndDirective.java │ │ │ │ ├── DefineDirective.java │ │ │ │ ├── UndefineDirective.java │ │ │ │ ├── UnusedDirective.java │ │ │ │ └── IncludeDirective.java │ │ │ │ └── CompilerDirective.java │ │ │ ├── utils │ │ │ └── ProgressReporterLogger.java │ │ │ └── metrics │ │ │ └── DefaultMetrics.java │ │ └── squid │ │ └── text │ │ └── delphi │ │ └── LiteralValueHandler.java ├── test │ ├── resources │ │ └── org │ │ │ └── sonar │ │ │ └── plugins │ │ │ └── delphi │ │ │ ├── BadSyntax.pas │ │ │ ├── SimpleDelphiProject │ │ │ ├── includes1 │ │ │ │ └── includes.inc │ │ │ ├── includes2 │ │ │ │ └── includes2.inc │ │ │ ├── Globals.pas │ │ │ ├── tools │ │ │ │ ├── GlobalsTest.pas │ │ │ │ ├── AccessorsTest.Pas │ │ │ │ ├── CommentsTest.pas │ │ │ │ └── FunctionTest.pas │ │ │ ├── branch │ │ │ │ ├── StatementTest.pas │ │ │ │ └── OverloadTest.pas │ │ │ ├── dproj │ │ │ │ └── workgroup │ │ │ │ │ └── All.groupproj │ │ │ ├── dpk │ │ │ │ └── TestLib.dpk │ │ │ └── MainWindow.pas │ │ │ ├── grammar │ │ │ ├── info.inc │ │ │ ├── UTF8WithBOM.pas │ │ │ ├── info2.inc │ │ │ ├── KeyWordsAsIdentifier.pas │ │ │ ├── PackageAsIdentifier.pas │ │ │ ├── RecordInitialization.pas │ │ │ ├── LabelUsage.pas │ │ │ ├── ComplexArray.pas │ │ │ ├── EmptyProcs.pas │ │ │ ├── Generics.pas │ │ │ ├── RecordConstructor.pas │ │ │ ├── InterfaceMethodResolutionClause.pas │ │ │ ├── AnonymousMethods.pas │ │ │ ├── DUnitX.pas │ │ │ └── ListUtils.pas │ │ │ ├── syntax │ │ │ ├── DefineTest.pas │ │ │ ├── FunctionParametersAnalyzerTest.pas │ │ │ └── SyntaxTest.pas │ │ │ ├── PMDTest │ │ │ ├── smallpmd.pas │ │ │ └── UnusedArgumentRule.pas │ │ │ ├── UnitTest │ │ │ ├── tests │ │ │ │ ├── MyTest1.pas │ │ │ │ ├── MyTest2.pas │ │ │ │ └── uMyTest1.pas │ │ │ └── reports │ │ │ │ └── TEST-dunit-report.xml │ │ │ ├── metrics │ │ │ ├── PropertyTest.pas │ │ │ ├── FunctionMessageTest.pas │ │ │ ├── FunctionOperatorTest.pas │ │ │ ├── FunctionVirtualTest.pas │ │ │ ├── DeadCodeMetricsTest.pas │ │ │ ├── MetricsTest.pas │ │ │ ├── ComplexityMetricsTest.pas │ │ │ ├── FunctionMetricsTest.pas │ │ │ └── ListUtils.pas │ │ │ ├── verifier │ │ │ └── CalledFunctionsTest.pas │ │ │ ├── directives │ │ │ └── FileWithDirectives.pas │ │ │ └── cpd │ │ │ └── CpdTest.pas │ └── java │ │ └── org │ │ └── sonar │ │ └── plugins │ │ └── delphi │ │ ├── colorizer │ │ ├── DelphiKeywordsTest.java │ │ └── DelphiColorizerFormatTest.java │ │ ├── pmd │ │ ├── xml │ │ │ └── PropertyTest.java │ │ ├── RuleData.java │ │ ├── DelphiPMDTest.java │ │ └── TooLongMethodNameRuleTest.java │ │ ├── utils │ │ ├── StringSubRangeTest.java │ │ └── ProgressReporterTest.java │ │ ├── DelphiPluginTest.java │ │ ├── IssueMatchers.java │ │ ├── antlr │ │ ├── analyzer │ │ │ ├── CodeAnalisysResultsTest.java │ │ │ └── impl │ │ │ │ └── operations │ │ │ │ ├── OperationsTestsCommon.java │ │ │ │ └── TraceBackNodeOperationTest.java │ │ └── sanitizer │ │ │ ├── subranges │ │ │ └── impl │ │ │ │ └── StringSubRangeTest.java │ │ │ └── resolvers │ │ │ └── IncludeResolverTest.java │ │ ├── HasRuleKey.java │ │ ├── HasRuleLineNumber.java │ │ ├── debug │ │ └── FileTestsCommon.java │ │ ├── HasRuleMessage.java │ │ └── project │ │ └── DelphiWorkgroupTest.java ├── COLOMBO_LICENSE.txt ├── LUCK_LICENSE.txt └── SABRE_AIRLINE_LICENSE.txt ├── samples ├── ant │ └── SampleProject │ │ ├── includes │ │ ├── includes.inc │ │ └── includes2.inc │ │ ├── tests │ │ ├── MyTest1.pas │ │ └── MyTest2.pas │ │ ├── src │ │ ├── GlobalsTest.pas │ │ ├── Globals.pas │ │ ├── AccessorsTest.pas │ │ ├── StatementTest.pas │ │ ├── CommentsTest.pas │ │ ├── MainWindow.pas │ │ ├── OverloadTest.pas │ │ └── FunctionTest.pas │ │ ├── reports │ │ └── TEST-dunit.xml │ │ └── build.xml ├── sonar-scanner │ ├── validation.txt │ ├── src │ │ ├── GlobalsTest.pas │ │ ├── Globals.pas │ │ ├── AccessorsTest.pas │ │ ├── StatementTest.pas │ │ ├── CommentsTest.pas │ │ ├── MainWindow.pas │ │ ├── OverloadTest.pas │ │ └── FunctionTest.pas │ ├── README.md │ └── sonar-project.properties └── pom.xml ├── doc └── ReadMe.docx ├── .travis.yml └── .gitignore /src/main/resources/org/sonar/l10n/pmd.properties: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /samples/ant/SampleProject/includes/includes.inc: -------------------------------------------------------------------------------- 1 | x = 5; 2 | y = 7; -------------------------------------------------------------------------------- /samples/ant/SampleProject/includes/includes2.inc: -------------------------------------------------------------------------------- 1 | XXX = 5; 2 | YYY = 7; -------------------------------------------------------------------------------- /doc/ReadMe.docx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/SandroLuck/SonarDelphi/HEAD/doc/ReadMe.docx -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/BadSyntax.pas: -------------------------------------------------------------------------------- 1 | unit BadSyntax; 2 | 3 | 4 | end. -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/SimpleDelphiProject/includes1/includes.inc: -------------------------------------------------------------------------------- 1 | x = 5; 2 | y = 7; -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/SimpleDelphiProject/includes2/includes2.inc: -------------------------------------------------------------------------------- 1 | XXX = 5; 2 | YYY = 7; -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/grammar/info.inc: -------------------------------------------------------------------------------- 1 | VERSION_INFO = '666'; 2 | { some comment } 3 | {$I info2.inc} -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: java 2 | jdk: 3 | - oraclejdk8 4 | before_install: echo "MAVEN_OPTS='-Xmx2048m -XX:MaxPermSize=256m'" > ~/.mavenrc 5 | -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/grammar/UTF8WithBOM.pas: -------------------------------------------------------------------------------- 1 | unit UTF8WithBOM.pas; 2 | 3 | interface 4 | 5 | implementation 6 | 7 | end. -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/syntax/DefineTest.pas: -------------------------------------------------------------------------------- 1 | unit TestUnit; 2 | 3 | interface 4 | 5 | implementation 6 | 7 | begin 8 | end. -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/PMDTest/smallpmd.pas: -------------------------------------------------------------------------------- 1 | unit SmallPMDTest; 2 | 3 | interface 4 | 5 | implementation 6 | 7 | begin 8 | end. -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/grammar/info2.inc: -------------------------------------------------------------------------------- 1 | XYZ = '//comment'; 2 | XYZ = '{comment}'; 3 | XYZ = '(*comment*)'; 4 | XYZ = '{$include error.inc}'; 5 | XYZ = '{$define THERE_ARE_ERRORS}'; -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/grammar/KeyWordsAsIdentifier.pas: -------------------------------------------------------------------------------- 1 | unit KeyWordsAsIdentifier; 2 | 3 | interface 4 | 5 | implementation 6 | 7 | var 8 | Stored: Boolean; 9 | 10 | end. 11 | -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/grammar/PackageAsIdentifier.pas: -------------------------------------------------------------------------------- 1 | unit PackageAsIdentifier; 2 | 3 | interface 4 | 5 | implementation 6 | 7 | var 8 | Package: Boolean; 9 | 10 | end. 11 | -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/syntax/FunctionParametersAnalyzerTest.pas: -------------------------------------------------------------------------------- 1 | unit TestUnit; 2 | 3 | interface 4 | 5 | implementation 6 | 7 | procedure myTestProcedure(x: real; y,z: integer; var q); 8 | begin 9 | end; 10 | 11 | begin 12 | end. -------------------------------------------------------------------------------- /samples/ant/SampleProject/tests/MyTest1.pas: -------------------------------------------------------------------------------- 1 | unit MyTest1; 2 | 3 | interface 4 | 5 | implementation 6 | 7 | function FunctionTest: integer; 8 | begin 9 | result := 0; 10 | end; 11 | 12 | procedure ProcedureTest; 13 | begin 14 | 15 | end; 16 | 17 | end. -------------------------------------------------------------------------------- /samples/ant/SampleProject/tests/MyTest2.pas: -------------------------------------------------------------------------------- 1 | unit MyTest2; 2 | 3 | interface 4 | 5 | implementation 6 | 7 | function FunctionTest2: integer; 8 | begin 9 | result := 0; 10 | end; 11 | 12 | procedure ProcedureTest2; 13 | begin 14 | 15 | end; 16 | 17 | end. -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/grammar/RecordInitialization.pas: -------------------------------------------------------------------------------- 1 | unit RecordInitialization; 2 | 3 | interface 4 | 5 | const DATE_FORMAT: TFormatSettings = (DateSeparator: '-'; ShortDateFormat: 'YYYY-MM-DD'); 6 | 7 | implementation 8 | 9 | end. 10 | -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/UnitTest/tests/MyTest1.pas: -------------------------------------------------------------------------------- 1 | unit MyTest1; 2 | 3 | interface 4 | 5 | implementation 6 | 7 | function FunctionTest: integer; 8 | begin 9 | result := 0; 10 | end; 11 | 12 | procedure ProcedureTest; 13 | begin 14 | 15 | end; 16 | 17 | end. -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/UnitTest/tests/MyTest2.pas: -------------------------------------------------------------------------------- 1 | unit MyTest2; 2 | 3 | interface 4 | 5 | implementation 6 | 7 | function FunctionTest2: integer; 8 | begin 9 | result := 0; 10 | end; 11 | 12 | procedure ProcedureTest2; 13 | begin 14 | 15 | end; 16 | 17 | end. -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/UnitTest/tests/uMyTest1.pas: -------------------------------------------------------------------------------- 1 | unit MyTest1; 2 | 3 | interface 4 | 5 | implementation 6 | 7 | function FunctionTest: integer; 8 | begin 9 | result := 0; 10 | end; 11 | 12 | procedure ProcedureTest; 13 | begin 14 | 15 | end; 16 | 17 | end. -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/metrics/PropertyTest.pas: -------------------------------------------------------------------------------- 1 | unit DemoForm; 2 | 3 | interface 4 | 5 | type 6 | TDemo = class 7 | published 8 | property isFoo : Boolean read foo write foo; 9 | property isBar : Boolean read bar write bar; 10 | end; 11 | 12 | implementation 13 | 14 | begin 15 | end. -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/grammar/LabelUsage.pas: -------------------------------------------------------------------------------- 1 | unit LabelUsage; 2 | 3 | interface 4 | 5 | implementation 6 | 7 | function ParseBuffer: Integer; 8 | label 9 | redo; 10 | begin 11 | repeat 12 | redo : Inc(Result); 13 | 14 | goto redo; 15 | until true; 16 | 17 | redo: Inc(Result); 18 | end; 19 | 20 | 21 | end. 22 | -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/grammar/ComplexArray.pas: -------------------------------------------------------------------------------- 1 | unit ComplexArray; 2 | 3 | interface 4 | 5 | type 6 | TMyComplexArray = array[Low(Word)-1..(high(Word)+1)-1] of Byte; 7 | TMyComplexArray2 = array[0..(high(Integer))-1] of Byte; 8 | TMyComplexArray3 = array[0..(high(Integer) div sizeof(Integer))-1] of Integer; 9 | 10 | 11 | implementation 12 | 13 | end. 14 | -------------------------------------------------------------------------------- /samples/sonar-scanner/validation.txt: -------------------------------------------------------------------------------- 1 | * start server with Delphi Plugin 2 | * run analysis with sonar-runner 3 | * verify org.codehaus.sonar:delphi-sonar-runner 4 | measure lines is 349 5 | measure ncloc is 248 6 | measure files is 8 7 | measure classes is 7 8 | * verify org.codehaus.sonar:delphi-sonar-runner:src/MainWindow 9 | measure lines is 52 10 | measure ncloc is 38 -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/metrics/FunctionMessageTest.pas: -------------------------------------------------------------------------------- 1 | unit FunctionMessageTest; 2 | 3 | interface 4 | 5 | type 6 | TWithMessageFunction = class 7 | procedure CNCommand(var AMessage: TWMMouse); message WM_MOUSEWHEEL; 8 | end; 9 | 10 | implementation 11 | 12 | { TWithMessageFunction } 13 | 14 | procedure TWithMessageFunction.CNCommand(var AMessage: TWMMouse); 15 | begin 16 | 17 | end; 18 | 19 | end. 20 | -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/verifier/CalledFunctionsTest.pas: -------------------------------------------------------------------------------- 1 | unit TestUnit; 2 | 3 | interface 4 | 5 | procedure myProcedure; 6 | procedure mySecondProcedure; 7 | 8 | implementation 9 | 10 | procedure myProcedure; 11 | begin 12 | mySecondProcedure; 13 | UnitB_Procedure; 14 | end; 15 | 16 | procedure mySecondProcedure; 17 | begin 18 | UnitA_Procedure; 19 | UnitC_Procedure; //unresolved 20 | end; 21 | 22 | begin 23 | end. -------------------------------------------------------------------------------- /samples/sonar-scanner/src/GlobalsTest.pas: -------------------------------------------------------------------------------- 1 | unit GlobalsTest; 2 | 3 | interface 4 | 5 | type 6 | TGlobalsTest = class 7 | public 8 | function rfcFunction(xyz: integer): integer; 9 | private 10 | end; 11 | 12 | implementation 13 | 14 | {$R *.dfm} 15 | 16 | //rfc = 4: rfcFunction + globalProcedure (+1) + globalFunction 17 | function TGlobalsTest.rfcFunction(xyz: integer): integer; 18 | begin 19 | globalProcedure; 20 | globalFunction; 21 | xyz; 22 | end; 23 | 24 | end. -------------------------------------------------------------------------------- /samples/ant/SampleProject/src/GlobalsTest.pas: -------------------------------------------------------------------------------- 1 | unit GlobalsTest; 2 | 3 | interface 4 | 5 | type 6 | TGlobalsTest = class 7 | public 8 | function rfcFunction(xyz: integer): integer; 9 | private 10 | end; 11 | 12 | implementation 13 | 14 | {$R *.dfm} 15 | 16 | //rfc = 4: rfcFunction + globalProcedure (+1) + globalFunction 17 | function TGlobalsTest.rfcFunction(xyz: integer): integer; 18 | begin 19 | globalProcedure; 20 | globalFunction; 21 | xyz; 22 | end; 23 | 24 | end. -------------------------------------------------------------------------------- /samples/sonar-scanner/src/Globals.pas: -------------------------------------------------------------------------------- 1 | unit Globals; 2 | 3 | interface 4 | 5 | procedure globalProcedure(); 6 | function globalFunction(): integer; 7 | 8 | var 9 | globalX, globalY: Integer; 10 | globalR: real; 11 | 12 | implementation 13 | 14 | procedure globalProcedure(); 15 | begin 16 | globalX := 5; 17 | writeln(); 18 | end; 19 | 20 | {*** Documented public global function ****} 21 | function globalFunction(): integer; 22 | begin 23 | result := 7; 24 | end; 25 | 26 | end. -------------------------------------------------------------------------------- /samples/ant/SampleProject/src/Globals.pas: -------------------------------------------------------------------------------- 1 | unit Globals; 2 | 3 | interface 4 | 5 | procedure globalProcedure(); 6 | function globalFunction(): integer; 7 | 8 | var 9 | globalX, globalY: Integer; 10 | globalR: real; 11 | 12 | implementation 13 | 14 | procedure globalProcedure(); 15 | begin 16 | globalX := 5; 17 | writeln(); 18 | end; 19 | 20 | {*** Documented public global function ****} 21 | function globalFunction(): integer; 22 | begin 23 | result := 7; 24 | end; 25 | 26 | end. -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/metrics/FunctionOperatorTest.pas: -------------------------------------------------------------------------------- 1 | unit FunctionOperatorTest; 2 | 3 | interface 4 | 5 | type 6 | IGenericA = interface 7 | end; 8 | 9 | GenericA = record 10 | public 11 | class operator Implicit(const value: GenericA): IGenericA; 12 | end; 13 | 14 | implementation 15 | 16 | { GenericA } 17 | 18 | class operator GenericA.Implicit(const value: GenericA): IGenericA; 19 | begin 20 | Result := value; 21 | end; 22 | 23 | end. 24 | -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/SimpleDelphiProject/Globals.pas: -------------------------------------------------------------------------------- 1 | unit Globals; 2 | 3 | interface 4 | 5 | procedure globalProcedure(); 6 | function globalFunction(): integer; 7 | 8 | var 9 | globalX, globalY: Integer; 10 | globalR: real; 11 | 12 | implementation 13 | 14 | procedure globalProcedure(); 15 | begin 16 | globalX := 5; 17 | writeln(); 18 | end; 19 | 20 | {*** Documented public global function ****} 21 | function globalFunction(): integer; 22 | begin 23 | result := 7; 24 | end; 25 | 26 | end. -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/SimpleDelphiProject/tools/GlobalsTest.pas: -------------------------------------------------------------------------------- 1 | unit GlobalsTest; 2 | 3 | interface 4 | 5 | type 6 | TGlobalsTest = class 7 | public 8 | function rfcFunction(xyz: integer): integer; 9 | private 10 | end; 11 | 12 | implementation 13 | 14 | uses 15 | Globals; 16 | 17 | {$R *.dfm} 18 | 19 | //rfc = 3: self + globalProcedure (writeln in globalProcedure not counted) + globalFunction 20 | function TGlobalsTest.rfcFunction(xyz: integer): integer; 21 | begin 22 | globalProcedure; 23 | globalFunction; 24 | xyz; 25 | end; 26 | 27 | end. -------------------------------------------------------------------------------- /samples/sonar-scanner/README.md: -------------------------------------------------------------------------------- 1 | This example demonstrates how to analyze a Delphi project with the SonarQube Runner. 2 | 3 | Prerequisites 4 | ============= 5 | * [SonarQube Server](http://www.sonarsource.org/downloads/) Check the [supported versions](https://github.com/fabriciocolombo/sonar-delphi/releases/latest). 6 | * [SonarQube Runner](http://docs.sonarqube.org/display/SONAR/Installing+and+Configuring+SonarQube+Scanner) 7 | * [SonarQube Delphi Plugin](https://github.com/fabriciocolombo/sonar-delphi/releases/latest) 8 | 9 | Usage 10 | ===== 11 | * Analyze the project with SonarQube using the SonarQube Runner: 12 | 13 | sonar-runner 14 | -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/grammar/EmptyProcs.pas: -------------------------------------------------------------------------------- 1 | unit EmptyProcs; 2 | 3 | interface 4 | 5 | type 6 | TEmptyProcs = class 7 | public 8 | procedure One; 9 | procedure Two; 10 | procedure Three; 11 | procedure Four; 12 | procedure Five; 13 | end; 14 | 15 | implementation 16 | 17 | procedure TEmptyProcs.One; 18 | begin 19 | 20 | end; 21 | 22 | procedure TEmptyProcs.Two; 23 | begin 24 | 25 | end; 26 | 27 | procedure TEmptyProcs.Three; 28 | begin 29 | 30 | end; 31 | 32 | procedure TEmptyProcs.Four; 33 | begin 34 | 35 | end; 36 | 37 | procedure TEmptyProcs.Five; 38 | begin 39 | 40 | end; 41 | 42 | end. -------------------------------------------------------------------------------- /samples/ant/SampleProject/src/AccessorsTest.pas: -------------------------------------------------------------------------------- 1 | unit AccessorsTest; 2 | 3 | interface 4 | 5 | type 6 | TAccessorsTest = class(TForm) 7 | public 8 | function getField(): integer; 9 | procedure setField(); 10 | private 11 | procedure fooXX; 12 | procedure getPrivateField(); 13 | end; 14 | 15 | var 16 | window: TMainWindow; 17 | 18 | implementation 19 | 20 | function TAccessorsTest.getField(): integer; 21 | begin 22 | result := 0; 23 | end; 24 | 25 | procedure TAccessorsTest.setField(); 26 | begin 27 | end; 28 | 29 | procedure TAccessorsTest.fooXX; 30 | begin 31 | end; 32 | 33 | procedure TAccessorsTest.getPrivateField(); 34 | begin 35 | end; 36 | 37 | end. -------------------------------------------------------------------------------- /samples/sonar-scanner/src/AccessorsTest.pas: -------------------------------------------------------------------------------- 1 | unit AccessorsTest; 2 | 3 | interface 4 | 5 | type 6 | TAccessorsTest = class(TForm) 7 | public 8 | function getField(): integer; 9 | procedure setField(); 10 | private 11 | procedure fooXX; 12 | procedure getPrivateField(); 13 | end; 14 | 15 | var 16 | window: TMainWindow; 17 | 18 | implementation 19 | 20 | function TAccessorsTest.getField(): integer; 21 | begin 22 | result := 0; 23 | end; 24 | 25 | procedure TAccessorsTest.setField(); 26 | begin 27 | end; 28 | 29 | procedure TAccessorsTest.fooXX; 30 | begin 31 | end; 32 | 33 | procedure TAccessorsTest.getPrivateField(); 34 | begin 35 | end; 36 | 37 | end. -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/grammar/Generics.pas: -------------------------------------------------------------------------------- 1 | unit Generics; 2 | 3 | interface 4 | 5 | type 6 | TMyGeneric = class 7 | strict private 8 | function fn(arg1: T1): Boolean; 9 | end; 10 | 11 | implementation 12 | 13 | var 14 | generic1: TList; 15 | generic2: TList>; 16 | 17 | 18 | function TMyGeneric.fn(arg1: T1): Boolean; 19 | begin 20 | Result := True; 21 | end; 22 | 23 | initialization 24 | generic1 := TList.Create; 25 | // issue #25 expression with nested generics 26 | generic2 := TList>.Create; 27 | 28 | finalization 29 | FreeAndNil(generic2); 30 | FreeAndNil(generic1); 31 | end. 32 | -------------------------------------------------------------------------------- /samples/sonar-scanner/src/StatementTest.pas: -------------------------------------------------------------------------------- 1 | unit StatementTest; 2 | 3 | interface 4 | 5 | type 6 | TStatementTest = class(TForm) 7 | public 8 | procedure fooZZ; 9 | private 10 | procedure bar; 11 | end; 12 | 13 | var 14 | window: TMainWindow; 15 | 16 | implementation 17 | 18 | procedure TStatementTest.fooZZ; 19 | var 20 | x,y: integer; 21 | begin 22 | x := 0; 23 | y := 1; 24 | x := y + 5; 25 | y := x - 6; 26 | end; 27 | 28 | procedure TStatementTest.bar; 29 | begin 30 | if(5 > 4) then 31 | begin 32 | writeln('5 > 4') 33 | end 34 | else 35 | begin 36 | writeln('5 <= 4'); 37 | end; 38 | 39 | while(true = true) do 40 | writeln('endless loop'); 41 | end; 42 | 43 | end. -------------------------------------------------------------------------------- /samples/ant/SampleProject/src/StatementTest.pas: -------------------------------------------------------------------------------- 1 | unit StatementTest; 2 | 3 | interface 4 | 5 | type 6 | TStatementTest = class(TForm) 7 | public 8 | procedure fooZZ; 9 | private 10 | procedure bar; 11 | end; 12 | 13 | var 14 | window: TMainWindow; 15 | 16 | implementation 17 | 18 | procedure TStatementTest.fooZZ; 19 | var 20 | x,y: integer; 21 | begin 22 | x := 0; 23 | y := 1; 24 | x := y + 5; 25 | y := x - 6; 26 | end; 27 | 28 | procedure TStatementTest.bar; 29 | begin 30 | if(5 > 4) then 31 | begin 32 | writeln('5 > 4') 33 | end 34 | else 35 | begin 36 | writeln('5 <= 4'); 37 | end; 38 | 39 | while(true = true) do 40 | writeln('endless loop'); 41 | end; 42 | 43 | end. -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/SimpleDelphiProject/tools/AccessorsTest.Pas: -------------------------------------------------------------------------------- 1 | unit AccessorsTest; 2 | 3 | interface 4 | 5 | type 6 | TAccessorsTest = class(TForm) 7 | public 8 | function getField(): integer; 9 | procedure setField(); 10 | private 11 | procedure fooXX; 12 | procedure getPrivateField(); 13 | end; 14 | 15 | var 16 | window: TMainWindow; 17 | 18 | implementation 19 | 20 | function TAccessorsTest.getField(): integer; 21 | begin 22 | result := 0; 23 | end; 24 | 25 | procedure TAccessorsTest.setField(); 26 | begin 27 | end; 28 | 29 | procedure TAccessorsTest.fooXX; 30 | begin 31 | end; 32 | 33 | procedure TAccessorsTest.getPrivateField(); 34 | begin 35 | end; 36 | 37 | end. -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/directives/FileWithDirectives.pas: -------------------------------------------------------------------------------- 1 | unit TestUnit; 2 | 3 | {$include include1.inc} 4 | {$i include2.inc} 5 | 6 | {$define TEST} 7 | {$undef TEST} 8 | 9 | (* nasty 10 | { comment 11 | //test {$include error.inc} 12 | } 13 | *) 14 | 15 | {$if I_FEEL_HAPPY} 16 | 17 | {$else} 18 | 19 | {$ifend} 20 | 21 | {$ifdef TEST} 22 | 23 | {$ifndef UseMe} 24 | {$ifdef EnableMemoryLeakReporting} 25 | 26 | {$else} 27 | 28 | {$endif} 29 | {$else} 30 | {$if VERSION >= 18} 31 | 32 | {$ifend} 33 | 34 | {$if RTLVersion < 18} 35 | 36 | {$ifend} 37 | {$endif} 38 | 39 | {$endif} 40 | 41 | 42 | interface 43 | 44 | implementation 45 | 46 | begin 47 | end. -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/grammar/RecordConstructor.pas: -------------------------------------------------------------------------------- 1 | unit RecordConstructor; 2 | 3 | interface 4 | 5 | implementation 6 | 7 | type 8 | TDummyRec = record 9 | FData : Integer; 10 | constructor Create(aData : Integer); 11 | end; 12 | 13 | TDummyClass = class 14 | FData : Integer; 15 | constructor Create(aData : Integer); 16 | end; 17 | 18 | TEmptyRec = record 19 | end; 20 | 21 | TEmptyClass = class 22 | 23 | end; 24 | 25 | constructor TDummyRec.Create(aData : Integer); 26 | begin 27 | inherited; 28 | FData := aData; 29 | end; 30 | 31 | constructor TDummyClass.Create(aData : Integer); 32 | begin 33 | inherited; 34 | FData := aData; 35 | end; 36 | 37 | end. -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/syntax/SyntaxTest.pas: -------------------------------------------------------------------------------- 1 | unit TestUnit; 2 | 3 | {$deFIne XYZ} 4 | 5 | interface 6 | 7 | //excluded comment 8 | 9 | (* nasty 10 | { comment 11 | //test {$include error.inc} 12 | } *) 13 | 14 | 15 | implementation 16 | const 17 | {$include info.inc} 18 | {$I info.inc} 19 | 20 | {$ifdef THERE_ARE_ERRORS} 21 | THERE ARE ERRORS WITH $DEFINE IN A STRING 22 | {$endif} 23 | 24 | procedure TestProcedure(); 25 | var 26 | String: str, str2, unicodeString; 27 | begin 28 | { another 29 | excluded block 30 | comment } 31 | 32 | str = 'string to be {excluded}'; //comment 33 | str2 = 'another string'; 34 | string := '_¢Ã»Ã_''_¢Â_''_¢ Ã_''_¯Ã°Ã_'; 35 | end; 36 | 37 | begin 38 | end. -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/SimpleDelphiProject/branch/StatementTest.pas: -------------------------------------------------------------------------------- 1 | unit StatementTest; 2 | 3 | interface 4 | 5 | type 6 | TStatementTest = class(TForm) 7 | public 8 | procedure fooZZ; 9 | private 10 | procedure bar; 11 | end; 12 | 13 | var 14 | window: TMainWindow; 15 | 16 | implementation 17 | 18 | procedure TStatementTest.fooZZ; 19 | var 20 | x,y: integer; 21 | begin 22 | x := 0; 23 | y := 1; 24 | x := y + 5; 25 | y := x - 6; 26 | end; 27 | 28 | procedure TStatementTest.bar; 29 | begin 30 | if(5 > 4) then 31 | begin 32 | writeln('5 > 4') 33 | end 34 | else 35 | begin 36 | writeln('5 <= 4'); 37 | end; 38 | 39 | while(true = true) do 40 | writeln('endless loop'); 41 | end; 42 | 43 | end. -------------------------------------------------------------------------------- /samples/sonar-scanner/src/CommentsTest.pas: -------------------------------------------------------------------------------- 1 | unit CommentsTest; 2 | 3 | interface 4 | 5 | {** documented class **} 6 | type 7 | TCommentsTest = class(TForm) 8 | public 9 | 10 | {*** documented public procedure ***} 11 | procedure foo11; 12 | 13 | //undocumented public procedure 14 | procedure foo22; 15 | private 16 | field1: integer; 17 | protected 18 | { comment line } 19 | //another comment 20 | (* block comment *) 21 | end; 22 | 23 | var 24 | window: TMainWindow; 25 | 26 | (** block comment, empty comment lines below (3) 27 | 28 | 29 | 30 | blah blah **) 31 | 32 | { comment // blah (* blah 33 | *)) } 34 | 35 | (** { blah 36 | } **) 37 | 38 | { } implementation { } 39 | 40 | {$R *.dfm} 41 | 42 | end. -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/grammar/InterfaceMethodResolutionClause.pas: -------------------------------------------------------------------------------- 1 | unit InterfaceMethodResolutionClause; 2 | 3 | interface 4 | 5 | type 6 | IInterfaceA = interface 7 | ['{D9434374-0D61-44A6-A84B-06F574F140BE}'] 8 | procedure DoSomething; 9 | end; 10 | 11 | IInterfaceB = interface 12 | ['{D9434374-0D61-44A6-A84B-06F574F140BE}'] 13 | procedure DoSomethingToo; 14 | end; 15 | 16 | TMyClass = class(TInterfacedObject, IInterfaceA, IInterfaceB) 17 | public 18 | procedure DoSomething; 19 | procedure IInterfaceB.DoSomethingToo = DoSomething; 20 | end; 21 | 22 | implementation 23 | 24 | { TMyClass } 25 | 26 | procedure TMyClass.DoSomething; 27 | begin 28 | Writeln('Do Something'); 29 | end; 30 | 31 | end. 32 | -------------------------------------------------------------------------------- /samples/ant/SampleProject/src/CommentsTest.pas: -------------------------------------------------------------------------------- 1 | unit CommentsTest; 2 | 3 | interface 4 | 5 | {** documented class **} 6 | type 7 | TCommentsTest = class(TForm) 8 | public 9 | 10 | {*** documented public procedure ***} 11 | procedure foo11; 12 | 13 | //undocumented public procedure 14 | procedure foo22; 15 | private 16 | field1: integer; 17 | protected 18 | { comment line } 19 | //another comment 20 | (* block comment *) 21 | end; 22 | 23 | var 24 | window: TMainWindow; 25 | 26 | (** block comment, empty comment lines below (3) 27 | 28 | 29 | 30 | blah blah **) 31 | 32 | { comment // blah (* blah 33 | *)) } 34 | 35 | (** { blah 36 | } **) 37 | 38 | { } implementation { } 39 | 40 | {$R *.dfm} 41 | 42 | end. -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/metrics/FunctionVirtualTest.pas: -------------------------------------------------------------------------------- 1 | unit FunctionVirtualTest; 2 | 3 | interface 4 | 5 | type 6 | TWithVirtualFunction = class 7 | public 8 | procedure AfterConstruction; override; 9 | destructor Destroy; override; 10 | procedure a;virtual; 11 | function b: Integer; virtual; 12 | end; 13 | 14 | implementation 15 | 16 | { TWithVirtualFunction } 17 | 18 | procedure TWithVirtualFunction.a; 19 | begin 20 | 21 | end; 22 | 23 | procedure TWithVirtualFunction.AfterConstruction; 24 | begin 25 | inherited; 26 | 27 | end; 28 | 29 | function TWithVirtualFunction.b: Integer; 30 | begin 31 | 32 | end; 33 | 34 | destructor TWithVirtualFunction.Destroy; 35 | begin 36 | 37 | inherited; 38 | end; 39 | 40 | end. 41 | -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/grammar/AnonymousMethods.pas: -------------------------------------------------------------------------------- 1 | unit AnonymousMethods; 2 | 3 | interface 4 | 5 | type 6 | TStringPredicate = reference to function(const aValue : string) : Boolean; 7 | 8 | function StringMatches(const aValue : string; aPredicate : TStringPredicate) : Boolean; 9 | function IsStringEmpty(const aValue : string) : Boolean; 10 | 11 | implementation 12 | 13 | function StringMatches(const aValue : string; aPredicate : TStringPredicate) : Boolean; 14 | begin 15 | Result := aPredicate(aValue); 16 | end; 17 | 18 | function IsStringEmpty(const aValue : string) : Boolean; 19 | begin 20 | Result := StringMatches(aValue, 21 | function(const aValue : string) : Boolean 22 | begin 23 | Result := aValue = ''; 24 | end); 25 | end; 26 | 27 | end. 28 | -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/SimpleDelphiProject/tools/CommentsTest.pas: -------------------------------------------------------------------------------- 1 | unit CommentsTest; 2 | 3 | interface 4 | 5 | {** documented class **} 6 | type 7 | TCommentsTest = class(TForm) 8 | public 9 | 10 | {*** documented public procedure ***} 11 | procedure foo11; 12 | 13 | //undocumented public procedure 14 | procedure foo22; 15 | private 16 | field1: integer; 17 | protected 18 | { comment line } 19 | //another comment 20 | (* block comment *) 21 | end; 22 | 23 | var 24 | window: TMainWindow; 25 | 26 | (** block comment, empty comment lines below (3) 27 | 28 | 29 | 30 | blah blah **) 31 | 32 | { comment // blah (* blah 33 | *)) } 34 | 35 | (** { blah 36 | } **) 37 | 38 | { } implementation { } 39 | 40 | {$R *.dfm} 41 | 42 | end. -------------------------------------------------------------------------------- /src/COLOMBO_LICENSE.txt: -------------------------------------------------------------------------------- 1 | Sonar Delphi Plugin 2 | Copyright (C) 2015 Fabricio Colombo 3 | 4 | This program is free software; you can redistribute it and/or 5 | modify it under the terms of the GNU Lesser General Public 6 | License as published by the Free Software Foundation; either 7 | version 3 of the License, or (at your option) any later version. 8 | 9 | This program is distributed in the hope that it will be useful, 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 | Lesser General Public License for more details. 13 | 14 | You should have received a copy of the GNU Lesser General Public 15 | License along with this program; if not, write to the Free Software 16 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 -------------------------------------------------------------------------------- /src/LUCK_LICENSE.txt: -------------------------------------------------------------------------------- 1 | Sonar Delphi Plugin 2 | Copyright (C) 2016 Sandro Luck 3 | Author: Sandro Luck(sandro.luck@gmx.ch) 4 | 5 | This program is free software; you can redistribute it and/or 6 | modify it under the terms of the GNU Lesser General Public 7 | License as published by the Free Software Foundation; either 8 | version 3 of the License, or (at your option) any later version. 9 | 10 | This program is distributed in the hope that it will be useful, 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | Lesser General Public License for more details. 14 | 15 | You should have received a copy of the GNU Lesser General Public 16 | License along with this program; if not, write to the Free Software 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/SimpleDelphiProject/dproj/workgroup/All.groupproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | {FFE3ACE4-C758-4C09-B302-C8DEFDA40560} 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Default.Personality.12 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/PMDTest/UnusedArgumentRule.pas: -------------------------------------------------------------------------------- 1 | unit UnusedARgumentRule; 2 | 3 | interface 4 | 5 | type 6 | TCustomComponent = class(TComponent) 7 | protected 8 | procedure OnEvent(Sender: TObject); 9 | procedure OnEventB(ASender: TObject); 10 | procedure OnUnusedArg(x: Integer); 11 | public 12 | procedure CaseInsensitive(Arg: Integer); 13 | end; 14 | 15 | implementation 16 | 17 | { TCustomComponent } 18 | 19 | procedure TCustomComponent.OnEvent(Sender: TObject); 20 | begin 21 | Writeln('dummy'); 22 | end; 23 | 24 | procedure TCustomComponent.OnEventB(ASender: TObject); 25 | begin 26 | Writeln('dummy'); 27 | end; 28 | 29 | procedure TCustomComponent.OnUnusedArg(x: Integer); 30 | begin 31 | Writeln('dummy'); 32 | end; 33 | 34 | procedure TCustomComponent.CaseInsensitive(Arg: Integer); 35 | begin 36 | arg := arg + 1; 37 | end; 38 | 39 | end. -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/SimpleDelphiProject/dpk/TestLib.dpk: -------------------------------------------------------------------------------- 1 | package DRBLib2009; 2 | 3 | {$R *.res} 4 | {$R 'Color.dcr'} 5 | {$R 'TestTest.dcr'} 6 | {$ALIGN 8} 7 | {$ASSERTIONS OFF} 8 | {$BOOLEVAL OFF} 9 | {$DEBUGINFO OFF} 10 | {$EXTENDEDSYNTAX ON} 11 | {$IMPORTEDDATA OFF} 12 | {$IOCHECKS ON} 13 | {$LOCALSYMBOLS OFF} 14 | {$LONGSTRINGS ON} 15 | {$OPENSTRINGS ON} 16 | {$OPTIMIZATION ON} 17 | {$OVERFLOWCHECKS ON} 18 | {$RANGECHECKS ON} 19 | {$REFERENCEINFO OFF} 20 | {$SAFEDIVIDE OFF} 21 | {$STACKFRAMES OFF} 22 | {$TYPEDADDRESS OFF} 23 | {$VARSTRINGCHECKS OFF} 24 | {$WRITEABLECONST ON} 25 | {$MINENUMSIZE 1} 26 | {$IMAGEBASE $400000} 27 | {$DESIGNONLY} 28 | {$IMPLICITBUILD OFF} 29 | 30 | requires 31 | vcl, 32 | vclsmp, 33 | vcldb, 34 | rtl, 35 | LoggingPackage; 36 | 37 | contains 38 | ColorTest in '..\source\Color.pas', 39 | TestTest in '..\source\Test.pas'; 40 | 41 | end. -------------------------------------------------------------------------------- /samples/sonar-scanner/src/MainWindow.pas: -------------------------------------------------------------------------------- 1 | unit MainWindow; 2 | 3 | interface 4 | 5 | {** documented class **} 6 | type 7 | TMainWindow = class(TForm) 8 | public 9 | procedure foo1; 10 | procedure foo2; 11 | private 12 | field1: integer; 13 | protected 14 | { comment line } 15 | //another comment 16 | (* block comment *) 17 | end; 18 | 19 | var 20 | window: TMainWindow; 21 | 22 | implementation 23 | 24 | uses 25 | OverloadTest, FunctionTest; 26 | 27 | {$R *.dfm} 28 | 29 | procedure TMainWindow.foo1; 30 | begin 31 | foo2; 32 | end; 33 | 34 | procedure TMainWindow.foo2; 35 | begin 36 | foo1; 37 | globalProcedure(); 38 | x := globalFunction; 39 | TOverloadTest.over1(5); 40 | fooZZ; 41 | bar; 42 | getField; 43 | fooXX; 44 | getPrivateField(); 45 | TFunctionTest.setField(5); 46 | TFunctionTest.getField; 47 | TFunctionTest.foo; 48 | TFunctionTest.bar(); 49 | rfcFunction(123); 50 | end; 51 | 52 | end. -------------------------------------------------------------------------------- /samples/ant/SampleProject/src/MainWindow.pas: -------------------------------------------------------------------------------- 1 | unit MainWindow; 2 | 3 | interface 4 | 5 | {** documented class **} 6 | type 7 | TMainWindow = class(TForm) 8 | public 9 | procedure foo1; 10 | procedure foo2; 11 | private 12 | field1: integer; 13 | protected 14 | { comment line } 15 | //another comment 16 | (* block comment *) 17 | end; 18 | 19 | var 20 | window: TMainWindow; 21 | 22 | implementation 23 | 24 | uses 25 | OverloadTest, FunctionTest; 26 | 27 | {$R *.dfm} 28 | 29 | procedure TMainWindow.foo1; 30 | begin 31 | foo2; 32 | end; 33 | 34 | procedure TMainWindow.foo2; 35 | begin 36 | foo1; 37 | globalProcedure(); 38 | x := globalFunction; 39 | TOverloadTest.over1(5); 40 | fooZZ; 41 | bar; 42 | getField; 43 | fooXX; 44 | getPrivateField(); 45 | TFunctionTest.setField(5); 46 | TFunctionTest.getField; 47 | TFunctionTest.foo; 48 | TFunctionTest.bar(); 49 | rfcFunction(123); 50 | end; 51 | 52 | end. -------------------------------------------------------------------------------- /samples/sonar-scanner/sonar-project.properties: -------------------------------------------------------------------------------- 1 | # must be unique in a given SonarQube instance 2 | sonar.projectKey=org.codehaus.sonar:delphi-sonar-runner 3 | 4 | # this is the name displayed in the SonarQube UI 5 | sonar.projectName=Delphi project analyzed with the SonarQube Runner 6 | sonar.projectVersion=1.0 7 | 8 | # Comma-separated paths to directories with sources (required) 9 | # Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows. 10 | # Since SonarQube 4.2, this property is optional if sonar.modules is set. 11 | # If not set, SonarQube starts looking for source code from the directory containing 12 | # the sonar-project.properties file. 13 | sonar.sources=src 14 | 15 | # Language is optional because of multi-language support. 16 | # Setting this property will force the language to Delphi. 17 | sonar.language=delph 18 | 19 | # Encoding of the source code. Default is default system encoding 20 | sonar.sourceEncoding=UTF-8 -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/SimpleDelphiProject/MainWindow.pas: -------------------------------------------------------------------------------- 1 | unit MainWindow; 2 | 3 | interface 4 | 5 | {** documented class **} 6 | type 7 | TMainWindow = class(TForm) 8 | public 9 | procedure foo1; 10 | procedure foo2; 11 | private 12 | field1: integer; 13 | protected 14 | { comment line } 15 | //another comment 16 | (* block comment *) 17 | end; 18 | 19 | var 20 | window: TMainWindow; 21 | 22 | implementation 23 | 24 | uses 25 | OverloadTest, FunctionTest; 26 | 27 | {$R *.dfm} 28 | 29 | procedure TMainWindow.foo1; 30 | begin 31 | foo2; 32 | end; 33 | 34 | procedure TMainWindow.foo2; 35 | begin 36 | foo1; 37 | globalProcedure(); 38 | x := globalFunction; 39 | TOverloadTest.over1(5); 40 | fooZZ; 41 | bar; 42 | getField; 43 | fooXX; 44 | getPrivateField(); 45 | TFunctionTest.setField(5); 46 | TFunctionTest.getField; 47 | TFunctionTest.foo; 48 | TFunctionTest.bar(); 49 | rfcFunction(123); 50 | end; 51 | 52 | end. -------------------------------------------------------------------------------- /samples/sonar-scanner/src/OverloadTest.pas: -------------------------------------------------------------------------------- 1 | unit OverloadTest; 2 | 3 | interface 4 | 5 | type 6 | TOverloadTest = class 7 | public 8 | function over1(x,y: integer): integer; 9 | function over1(x: integer; y: real): integer; overload; 10 | function over1(x: real): integer; overload; 11 | function over1(x: float): integer; overload; 12 | function notOver(): real; 13 | private 14 | field: integer; 15 | end; 16 | 17 | implementation 18 | 19 | {$R *.dfm} 20 | 21 | function TOverloadTest.notOver(): real; 22 | begin 23 | result := 5.0; 24 | end; 25 | 26 | function TOverloadTest.over1(x,y: integer): integer; 27 | begin 28 | field := x+y; 29 | over1(2.5); 30 | end; 31 | 32 | function TOverloadTest.over1(x: integer; y: real): integer; 33 | begin 34 | field := 0; 35 | end; 36 | 37 | function TOverloadTest.over1(x: real): integer; 38 | begin 39 | result := 3; 40 | end; 41 | 42 | function TOverloadTest.over1(x: float): integer; 43 | begin 44 | result := 4; 45 | end; 46 | 47 | end. -------------------------------------------------------------------------------- /samples/ant/SampleProject/src/OverloadTest.pas: -------------------------------------------------------------------------------- 1 | unit OverloadTest; 2 | 3 | interface 4 | 5 | type 6 | TOverloadTest = class 7 | public 8 | function over1(x,y: integer): integer; 9 | function over1(x: integer; y: real): integer; overload; 10 | function over1(x: real): integer; overload; 11 | function over1(x: float): integer; overload; 12 | function notOver(): real; 13 | private 14 | field: integer; 15 | end; 16 | 17 | implementation 18 | 19 | {$R *.dfm} 20 | 21 | function TOverloadTest.notOver(): real; 22 | begin 23 | result := 5.0; 24 | end; 25 | 26 | function TOverloadTest.over1(x,y: integer): integer; 27 | begin 28 | field := x+y; 29 | over1(2.5); 30 | end; 31 | 32 | function TOverloadTest.over1(x: integer; y: real): integer; 33 | begin 34 | field := 0; 35 | end; 36 | 37 | function TOverloadTest.over1(x: real): integer; 38 | begin 39 | result := 3; 40 | end; 41 | 42 | function TOverloadTest.over1(x: float): integer; 43 | begin 44 | result := 4; 45 | end; 46 | 47 | end. -------------------------------------------------------------------------------- /src/SABRE_AIRLINE_LICENSE.txt: -------------------------------------------------------------------------------- 1 | Sonar Delphi Plugin 2 | Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 3 | Author(s): 4 | Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 5 | Michal Wojcik (michal.wojcik@sabre.com) 6 | Fabricio Colombo (fabricio.colombo.mva@gmail.com) 7 | 8 | This program is free software; you can redistribute it and/or 9 | modify it under the terms of the GNU Lesser General Public 10 | License as published by the Free Software Foundation; either 11 | version 3 of the License, or (at your option) any later version. 12 | 13 | This program is distributed in the hope that it will be useful, 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 | Lesser General Public License for more details. 17 | 18 | You should have received a copy of the GNU Lesser General Public 19 | License along with this program; if not, write to the Free Software 20 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 -------------------------------------------------------------------------------- /samples/ant/SampleProject/reports/TEST-dunit.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 13 | 14 | 15 | 16 | 20 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/SimpleDelphiProject/branch/OverloadTest.pas: -------------------------------------------------------------------------------- 1 | unit OverloadTest; 2 | 3 | interface 4 | 5 | type 6 | TOverloadTest = class 7 | public 8 | function over1(x,y: integer): integer; 9 | function over1(x: integer; y: real): integer; overload; 10 | function over1(x: real): integer; overload; 11 | function over1(x: float): integer; overload; 12 | function notOver(): real; 13 | private 14 | field: integer; 15 | end; 16 | 17 | implementation 18 | 19 | {$R *.dfm} 20 | 21 | function TOverloadTest.notOver(): real; 22 | begin 23 | result := 5.0; 24 | end; 25 | 26 | function TOverloadTest.over1(x,y: integer): integer; 27 | begin 28 | field := x+y; 29 | over1(2.5); 30 | end; 31 | 32 | function TOverloadTest.over1(x: integer; y: real): integer; 33 | begin 34 | field := 0; 35 | end; 36 | 37 | function TOverloadTest.over1(x: real): integer; 38 | begin 39 | result := 3; 40 | end; 41 | 42 | function TOverloadTest.over1(x: float): integer; 43 | begin 44 | result := 4; 45 | end; 46 | 47 | end. -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/cpd/CpdTest.pas: -------------------------------------------------------------------------------- 1 | unit DemoForm; 2 | 3 | interface 4 | 5 | //test 6 | { test } 7 | (* test *) 8 | 9 | type 10 | TfDemo = class(TForm) 11 | bShowTracker: TButton; 12 | procedure bShowTrackerClick(Sender: TObject); 13 | private 14 | //test 15 | public 16 | {test} 17 | end; 18 | 19 | type 20 | TfDemoSecond = class (TForm) 21 | bShowTracker: TButton; 22 | procedure bShowTrackerClick(Sender: TObject); 23 | private 24 | 25 | public 26 | 27 | end; 28 | 29 | var 30 | fDemo: TfDemo; 31 | 32 | implementation 33 | 34 | procedure TfDemo.bShowTrackerClick(Sender: TObject); 35 | begin 36 | (* long 37 | multiline 38 | comment 39 | *) 40 | 41 | string = "sample string"; 42 | 43 | for i:=0 to 100 do 44 | begin 45 | a := b; 46 | b := c; 47 | c := d; 48 | a := b; 49 | if a < b then 50 | c := b; 51 | end; 52 | 53 | for i:=0 to 100 do 54 | begin 55 | a := b; 56 | b := c; 57 | c := d; 58 | a := b; 59 | if a < b then 60 | c := b; 61 | end; 62 | 63 | end; 64 | 65 | end. -------------------------------------------------------------------------------- /samples/ant/SampleProject/src/FunctionTest.pas: -------------------------------------------------------------------------------- 1 | unit FunctionTest; 2 | 3 | interface 4 | 5 | uses 6 | StatementTest; 7 | 8 | type 9 | TFunctionTest = class(TStatementTest, TForm) 10 | public 11 | function getField: integer; 12 | procedure setField(x: integer); 13 | procedure foo; 14 | private 15 | procedure bar; 16 | classField: integer; 17 | end; 18 | 19 | var 20 | window: TFunctionTest; 21 | 22 | implementation 23 | 24 | procedure TFunctionTest.setField(x: integer); 25 | var 26 | placeHolder, z: real; 27 | begin 28 | classField := placeHolder + z + x; 29 | if(x < 5) then x := 5; 30 | if(x > 5) then z := 0.0; 31 | end; 32 | 33 | function TFunctionTest.getField: integer; 34 | var 35 | placeHolder: real; 36 | y: integer; 37 | begin 38 | result := placeHolder + y; 39 | y := 0; 40 | while(true) do 41 | begin 42 | break; 43 | end; 44 | end; 45 | 46 | procedure TFunctionTest.foo; 47 | var 48 | placeHolder, y: real; 49 | begin 50 | setField(0); 51 | end; 52 | 53 | procedure TFunctionTest.bar; 54 | var 55 | placeHolder, x: real; 56 | begin 57 | classField := 0; 58 | end; 59 | 60 | end. -------------------------------------------------------------------------------- /samples/sonar-scanner/src/FunctionTest.pas: -------------------------------------------------------------------------------- 1 | unit FunctionTest; 2 | 3 | interface 4 | 5 | uses 6 | StatementTest; 7 | 8 | type 9 | TFunctionTest = class(TStatementTest, TForm) 10 | public 11 | function getField: integer; 12 | procedure setField(x: integer); 13 | procedure foo; 14 | private 15 | procedure bar; 16 | classField: integer; 17 | end; 18 | 19 | var 20 | window: TFunctionTest; 21 | 22 | implementation 23 | 24 | procedure TFunctionTest.setField(x: integer); 25 | var 26 | placeHolder, z: real; 27 | begin 28 | classField := placeHolder + z + x; 29 | if(x < 5) then x := 5; 30 | if(x > 5) then z := 0.0; 31 | end; 32 | 33 | function TFunctionTest.getField: integer; 34 | var 35 | placeHolder: real; 36 | y: integer; 37 | begin 38 | result := placeHolder + y; 39 | y := 0; 40 | while(true) do 41 | begin 42 | break; 43 | end; 44 | end; 45 | 46 | procedure TFunctionTest.foo; 47 | var 48 | placeHolder, y: real; 49 | begin 50 | setField(0); 51 | end; 52 | 53 | procedure TFunctionTest.bar; 54 | var 55 | placeHolder, x: real; 56 | begin 57 | classField := 0; 58 | end; 59 | 60 | end. -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/SimpleDelphiProject/tools/FunctionTest.pas: -------------------------------------------------------------------------------- 1 | unit FunctionTest; 2 | 3 | interface 4 | 5 | uses 6 | StatementTest; 7 | 8 | type 9 | TFunctionTest = class(TStatementTest, TForm) 10 | public 11 | function getField: integer; 12 | procedure setField(x: integer); 13 | procedure foo; 14 | private 15 | procedure bar; 16 | classField: integer; 17 | end; 18 | 19 | var 20 | window: TFunctionTest; 21 | 22 | implementation 23 | 24 | procedure TFunctionTest.setField(x: integer); 25 | var 26 | placeHolder, z: real; 27 | begin 28 | classField := placeHolder + z + x; 29 | if(x < 5) then x := 5; 30 | if(x > 5) then z := 0.0; 31 | end; 32 | 33 | function TFunctionTest.getField: integer; 34 | var 35 | placeHolder: real; 36 | y: integer; 37 | begin 38 | result := placeHolder + y; 39 | y := 0; 40 | while(true) do 41 | begin 42 | break; 43 | end; 44 | end; 45 | 46 | procedure TFunctionTest.foo; 47 | var 48 | placeHolder, y: real; 49 | begin 50 | setField(0); 51 | end; 52 | 53 | procedure TFunctionTest.bar; 54 | var 55 | placeHolder, x: real; 56 | begin 57 | classField := 0; 58 | end; 59 | 60 | end. -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/pmd/rules/EmptyFinallyBlockRule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2015 Fabricio Colombo 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Lesser General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 3 of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public 16 | * License along with this program; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 18 | */ 19 | package org.sonar.plugins.delphi.pmd.rules; 20 | 21 | public class EmptyFinallyBlockRule extends NodeSequenceRule { 22 | 23 | @Override 24 | protected void init() { 25 | super.init(); 26 | setSequence(new String[] {"finally", "end"}); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/pmd/rules/DestructorWithoutInheritedStatementRule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2015 Fabricio Colombo 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Lesser General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 3 of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public 16 | * License along with this program; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 18 | */ 19 | package org.sonar.plugins.delphi.pmd.rules; 20 | 21 | public class DestructorWithoutInheritedStatementRule extends NoInheritedStatementRule { 22 | @Override 23 | protected void init() { 24 | super.init(); 25 | setLookFor("destructor"); 26 | } 27 | 28 | } 29 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/codecoverage/CodeCoverageTool.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.codecoverage; 24 | 25 | public enum CodeCoverageTool 26 | { 27 | None, 28 | DelphiCodeCoverage, 29 | AQTime 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/codecoverage/DelphiCodeCoverageParser.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.codecoverage; 24 | 25 | import org.sonar.api.batch.SensorContext; 26 | 27 | public interface DelphiCodeCoverageParser 28 | { 29 | void parse(SensorContext context); 30 | } 31 | -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/grammar/DUnitX.pas: -------------------------------------------------------------------------------- 1 | unit DUnitX.AutoDetect.Console; 2 | 3 | { Copyright Unicode Support} 4 | 5 | interface 6 | 7 | (** Code snippets from https://github.com/VSoftTechnologies/DUnitX **) 8 | 9 | type 10 | TValueHelper = record helper for TValue 11 | private 12 | end; 13 | 14 | type 15 | TTestLocalMethod = TProc; 16 | 17 | TTestMethod = procedure of object; 18 | 19 | Assert = class 20 | //Implements could be used as identifier 21 | class function Implements(value : IInterface; const message : string = '' ) : T; 22 | end; 23 | 24 | IMyInterface = interface 25 | ['{9B59FF6D-7812-46A6-AFBD-89560AA639DB}'] 26 | end; 27 | 28 | TDUnitXEnumerable = class(TInterfacedObject, IEnumerable) 29 | protected 30 | //function IEnumerable.GetEnumerator = GetNonGenEnumerator; 31 | //function GetNonGenEnumerator : IEnumerator; virtual; abstract; 32 | end; 33 | 34 | TDUnitXTestFixture = class(TWeakReferencedObject, ITestFixture,ITestFixtureInfo) 35 | // function ITestFixtureInfo.GetTests = ITestFixtureInfo_GetTests; 36 | end; 37 | 38 | implementation 39 | 40 | procedure testUnicode; 41 | var 42 | vString: string; 43 | vTest: string; 44 | begin 45 | //vString := vTest; 46 | vString := #1234; 47 | vString := 'aaaa'; 48 | vString := '©'; 49 | end; 50 | 51 | end. 52 | -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/metrics/DeadCodeMetricsTest.pas: -------------------------------------------------------------------------------- 1 | unit DeadCodeUnit; 2 | 3 | interface 4 | 5 | type 6 | myInterface = Interface(IInterface) 7 | procedure foo; 8 | function bar: integer; 9 | end; 10 | 11 | myClass = class(myInterface) 12 | public 13 | procedure foo; //no violation, interface implementation 14 | procedure UnusedProcedure; //violation 15 | procedure UsedProcedure; //no violation, procedure used 16 | function bar: integer; //no violation, interface implementation 17 | procedure vii; virtual; //no violation, virtual procedure 18 | procedure viiii; override; //no violation, virtual procedure 19 | procedure readFunc; //no violation, used in property 20 | procedure writeFunc; //no violation, used in property 21 | procedure UnusedToo; //violation 22 | procedure WMPaint(var Message : TWMPaint); message WM_PAINT; //no violation, because message 23 | published 24 | property isSth : Boolean read readFunc write writeFunc; 25 | end; 26 | 27 | implementation 28 | 29 | procedure myClass.foo; 30 | begin 31 | end; 32 | 33 | function myClass.bar: integer; 34 | begin 35 | end; 36 | 37 | procedure myClass.UnusedProcedure; 38 | begin 39 | UsedProcedure; 40 | end; 41 | 42 | procedure myClass.UsedProcedure; 43 | begin 44 | end; 45 | 46 | procedure myClass.WMPaint(var Message : TWMPaint); 47 | begin 48 | 49 | end; 50 | 51 | end. -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/metrics/MetricsTest.pas: -------------------------------------------------------------------------------- 1 | unit DemoForm; 2 | 3 | interface 4 | 5 | //comment line 1 6 | //comment line 2 7 | 8 | type 9 | TfDemo = class(TForm, TMultiCheckbox) 10 | bShowTracker: TButton; 11 | procedure bShowTrackerClick(Sender, Sender2: TObject; x: Integer); 12 | function fClassMethod(var x,z: real): integer; 13 | private 14 | //comment line 3 15 | private1var: string; 16 | public 17 | public1var, pub2var: integer; 18 | protected 19 | protected1var: real; 20 | end; 21 | 22 | var 23 | fDemo1, fDemo2: TfDemo; 24 | bigInt: Integer; 25 | 26 | implementation 27 | 28 | { comment line 29 | 30 | 6 } //causing trouble //trouble //trouble 31 | 32 | (* comment 33 | line 8 *) { ddd } {ffff } // fff 34 | 35 | procedure StandAloneProcedure(x, y: real); 36 | begin 37 | x := 5; 38 | y := 7; 39 | while x < 5 do 40 | begin 41 | if(x > 7) then y := 2 42 | else y := 0; 43 | end; 44 | 45 | end; 46 | 47 | {** doc **} 48 | function StandAloneFunction(var x: TdDemo): integer; 49 | begin 50 | 51 | end; 52 | 53 | {* 54 | Assumes that all rows have the same number of columns 55 | *} 56 | function TfDemo.FunctionName1(SenderXXX: TObject): integer; 57 | begin 58 | 59 | end; 60 | 61 | {** function 62 | documentation 63 | 2 *******} 64 | 65 | procedure TfDemo.ProcedureName1(SenderYYY: TObject); 66 | var 67 | str: string; 68 | begin 69 | str := 'sample string'; 70 | end; 71 | 72 | end. -------------------------------------------------------------------------------- /src/test/java/org/sonar/plugins/delphi/colorizer/DelphiKeywordsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.colorizer; 24 | 25 | import org.junit.Test; 26 | 27 | import static org.junit.Assert.assertEquals; 28 | 29 | public class DelphiKeywordsTest { 30 | 31 | @Test 32 | public void delphiKeywordsTest() { 33 | assertEquals(183, DelphiKeywords.get().size()); 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/pmd/DelphiPmdConstants.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.pmd; 24 | 25 | /** 26 | * Constants for Delphi pmd 27 | */ 28 | public final class DelphiPmdConstants { 29 | 30 | public static final String REPOSITORY_KEY = "delph"; 31 | public static final String REPOSITORY_NAME = "Delphi PMD"; 32 | 33 | private DelphiPmdConstants() { 34 | } 35 | 36 | } 37 | -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/metrics/ComplexityMetricsTest.pas: -------------------------------------------------------------------------------- 1 | unit DemoForm; 2 | 3 | interface 4 | 5 | type 6 | TDemo = class(myClass, TMultiCheckbox, TMyAncestor, TAbstractSomething) 7 | private 8 | public 9 | procedure publicProcedure(x, y: real; z: integer); 10 | function getFunction(): integer; 11 | end; 12 | 13 | myClass = class(TMyAncestor) 14 | procedure myProcedure(); 15 | procedure myProcedure(x: real); 16 | procedure setSomething(); 17 | end; 18 | 19 | implementation 20 | 21 | function TDemo.getFunction(): integer; 22 | begin 23 | result := 7; 24 | end; 25 | 26 | procedure TDemo.publicProcedure(x, y: real; z: integer); 27 | var 28 | str: string; 29 | begin 30 | str := 'sample string'; 31 | end; 32 | 33 | procedure myClass.myProcedure(); 34 | var 35 | i: integer; 36 | begin 37 | for i := 1 to 5 do 38 | begin 39 | writeln(i); 40 | i := i + i; 41 | end; 42 | end; 43 | 44 | procedure myClass.myProcedure(x: real); 45 | begin 46 | if x < 5.0 then x := 5.0; 47 | if x < 6.0 then x := 6.0; 48 | if x < 7.0 then x := 7.0; 49 | end; 50 | 51 | procedure StandAloneProcedure(x, y: real); 52 | var 53 | x,y: integer; 54 | begin 55 | x := 5; 56 | y := 7; 57 | while x < 5 do 58 | begin 59 | if(x > 7) then y := 2 60 | else y := 0; 61 | end; 62 | end; 63 | 64 | procedure myClass.setSomething(); 65 | begin 66 | if(x) then y := 1; 67 | while true do 68 | begin 69 | 70 | end; 71 | end; 72 | 73 | end. 74 | -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/UnitTest/reports/TEST-dunit-report.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 9 | 13 | 14 | 15 | 16 | 20 | 24 | 25 | 26 | 27 | 31 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/core/DelphiRecognizer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.core; 24 | 25 | import org.sonar.squidbridge.recognizer.CodeRecognizer; 26 | 27 | 28 | /** 29 | * Delphi source code recognizer 30 | */ 31 | public class DelphiRecognizer extends CodeRecognizer { 32 | 33 | /** 34 | * ctor 35 | */ 36 | public DelphiRecognizer() { 37 | super(1.0, new DelphiFootPrint()); 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /src/test/java/org/sonar/plugins/delphi/pmd/xml/PropertyTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.pmd.xml; 24 | 25 | import org.junit.Test; 26 | 27 | import static org.junit.Assert.assertEquals; 28 | 29 | public class PropertyTest { 30 | 31 | @Test 32 | public void propertyTest() { 33 | Property p = new Property("name", "value"); 34 | assertEquals("name", p.getName()); 35 | assertEquals("value", p.getValue()); 36 | } 37 | 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/antlr/ast/exceptions/NodeNameForCodeDoesNotExistException.java: -------------------------------------------------------------------------------- 1 | package org.sonar.plugins.delphi.antlr.ast.exceptions; 2 | 3 | /* 4 | * Sonar Delphi Plugin 5 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 6 | * Author(s): 7 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 8 | * Michal Wojcik (michal.wojcik@sabre.com) 9 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 10 | * 11 | * This program is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 3 of the License, or (at your option) any later version. 15 | * 16 | * This program is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public 22 | * License along with this program; if not, write to the Free Software 23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 24 | */ 25 | 26 | public class NodeNameForCodeDoesNotExistException extends RuntimeException { 27 | 28 | /** 29 | * 30 | */ 31 | private static final long serialVersionUID = 1L; 32 | 33 | public NodeNameForCodeDoesNotExistException(String message) { 34 | super(message); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/test/java/org/sonar/plugins/delphi/colorizer/DelphiColorizerFormatTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.colorizer; 24 | 25 | import org.junit.Test; 26 | 27 | import static org.junit.Assert.assertTrue; 28 | 29 | public class DelphiColorizerFormatTest { 30 | 31 | @Test 32 | public void getTokenizersTest() { 33 | DelphiColorizerFormat format = new DelphiColorizerFormat(); 34 | assertTrue(format.getTokenizers().size() >= 5); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/antlr/analyzer/ASTAnalyzer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.antlr.analyzer; 24 | 25 | import org.sonar.plugins.delphi.antlr.ast.ASTTree; 26 | 27 | /** 28 | * AST tree analyzer interface 29 | */ 30 | public interface ASTAnalyzer { 31 | 32 | /** 33 | * Analyse an AST tree 34 | * 35 | * @param ast AST Tree 36 | * @return Result the result of analysis. 37 | */ 38 | CodeAnalysisResults analyze(ASTTree ast); 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/antlr/sanitizer/subranges/SubRangeFirstOccurenceComparator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.antlr.sanitizer.subranges; 24 | 25 | /** 26 | * comparator class for SubRange, returns which subrange is defined first 27 | */ 28 | public class SubRangeFirstOccurenceComparator implements java.util.Comparator { 29 | 30 | @Override 31 | public int compare(SubRange o1, SubRange o2) { 32 | return o1.getBegin() - o2.getBegin(); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /src/test/java/org/sonar/plugins/delphi/utils/StringSubRangeTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.utils; 24 | 25 | import org.junit.Test; 26 | import org.sonar.plugins.delphi.antlr.sanitizer.subranges.impl.StringSubRange; 27 | 28 | import static org.junit.Assert.assertEquals; 29 | 30 | public class StringSubRangeTest { 31 | 32 | @Test 33 | public void toStringTest() { 34 | StringSubRange subRange = new StringSubRange(3, 6, "my sub range"); 35 | assertEquals("[3, 6] sub", subRange.toString()); 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/antlr/analyzer/impl/operations/NodeOperation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.antlr.analyzer.impl.operations; 24 | 25 | import org.antlr.runtime.tree.Tree; 26 | import org.sonar.plugins.delphi.antlr.analyzer.CodeNode; 27 | 28 | /** 29 | * Node operation interface 30 | */ 31 | public interface NodeOperation { 32 | 33 | /** 34 | * Executes operation 35 | * 36 | * @param node Node to operate on 37 | * @return resulting code node 38 | */ 39 | CodeNode execute(Tree node); 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/antlr/sanitizer/resolvers/exceptions/DefineResolverException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.antlr.sanitizer.resolvers.exceptions; 24 | 25 | /** 26 | * DefineResolver exception 27 | */ 28 | public class DefineResolverException extends Exception { 29 | 30 | private static final long serialVersionUID = 5306303004220560085L; 31 | 32 | /** 33 | * ctor 34 | * 35 | * @param msg Error message 36 | */ 37 | public DefineResolverException(String msg) { 38 | super(msg); 39 | } 40 | 41 | } 42 | -------------------------------------------------------------------------------- /samples/pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | br.com.fabricio 5 | ProjectName 6 | 1.0 7 | pom 8 | ProjectName 9 | 10 | src 11 | unittest 12 | 13 | 14 | delph 15 | target\surefire-reports 16 | target\surefire-reports 17 | delphi code coverage 18 | target\CoverageReports\CodeCoverage_Summary.xml 19 | true 20 | 21 | 22 | 23 | scm:svn:https://repository/project/trunk 24 | scm:svn:https://repository/project/trunk 25 | https://repository/project/trunk 26 | 27 | 28 | 29 | 30 | sonar 31 | 32 | true 33 | 34 | 35 | http://localhost:9000 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/pmd/rules/DprVariableRule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.pmd.rules; 24 | 25 | import org.sonar.plugins.delphi.antlr.DelphiLexer; 26 | import org.sonar.plugins.delphi.antlr.ast.DelphiPMDNode; 27 | 28 | /** 29 | * Rule class searching for variables in a .dpr file 30 | */ 31 | public class DprVariableRule extends DprFunctionRule { 32 | 33 | @Override 34 | protected boolean isViolationNode(DelphiPMDNode node) { 35 | int type = node.getType(); 36 | return type == DelphiLexer.TkVariableIdents; 37 | } 38 | 39 | } 40 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/pmd/rules/RecordNameRule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2015 Fabricio Colombo 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Lesser General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 3 of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public 16 | * License along with this program; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 18 | */ 19 | package org.sonar.plugins.delphi.pmd.rules; 20 | 21 | import net.sourceforge.pmd.RuleContext; 22 | import org.sonar.plugins.delphi.antlr.DelphiLexer; 23 | import org.sonar.plugins.delphi.antlr.ast.DelphiPMDNode; 24 | 25 | public class RecordNameRule extends DelphiRule { 26 | 27 | @Override 28 | public void visit(DelphiPMDNode node, RuleContext ctx) { 29 | 30 | if (node.getType() == DelphiLexer.TkRecord) { 31 | String name = node.getParent().getText(); 32 | 33 | char firstCharAfterPrefix = name.charAt(1); 34 | 35 | if (!name.startsWith("T") || firstCharAfterPrefix != Character.toUpperCase(firstCharAfterPrefix)) { 36 | addViolation(ctx, node); 37 | } 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | .classpath 3 | .project 4 | .settings 5 | /.README.md.html 6 | /.TODO.md.html 7 | /hot-deploy.bat 8 | .sonar 9 | 10 | 11 | # Created by https://www.gitignore.io/api/git 12 | 13 | ### Git ### 14 | *.orig 15 | 16 | # Created by https://www.gitignore.io/api/intellij 17 | 18 | ### Intellij ### 19 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm 20 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 21 | 22 | # User-specific stuff: 23 | .idea/workspace.xml 24 | .idea/tasks.xml 25 | .idea/dictionaries 26 | .idea/vcs.xml 27 | .idea/jsLibraryMappings.xml 28 | .idea 29 | .idea/* 30 | #hiho 31 | 32 | # Sensitive or high-churn files: 33 | .idea/dataSources.ids 34 | .idea/dataSources.xml 35 | .idea/dataSources.local.xml 36 | .idea/sqlDataSources.xml 37 | .idea/dynamic.xml 38 | .idea/uiDesigner.xml 39 | 40 | # Gradle: 41 | .idea/gradle.xml 42 | .idea/libraries 43 | 44 | # Mongo Explorer plugin: 45 | .idea/mongoSettings.xml 46 | 47 | ## File-based project format: 48 | *.iws 49 | 50 | ## Plugin-specific files: 51 | 52 | # IntelliJ 53 | /out/ 54 | idea/* 55 | 56 | # mpeltonen/sbt-idea plugin 57 | .idea_modules/ 58 | 59 | # JIRA plugin 60 | atlassian-ide-plugin.xml 61 | 62 | # Crashlytics plugin (for Android Studio and IntelliJ) 63 | com_crashlytics_export_strings.xml 64 | crashlytics.properties 65 | crashlytics-build.properties 66 | fabric.properties 67 | 68 | ### Intellij Patch ### 69 | # Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 70 | 71 | # *.iml 72 | # modules.xml 73 | # .idea/misc.xml 74 | # *.ipr -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/core/DelphiFootPrint.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.core; 24 | 25 | import org.sonar.squidbridge.recognizer.Detector; 26 | import org.sonar.squidbridge.recognizer.LanguageFootprint; 27 | 28 | import java.util.Collections; 29 | import java.util.Set; 30 | 31 | /** 32 | * DelphiLanguage Language Footprint, used to recognize language 33 | * 34 | */ 35 | public class DelphiFootPrint implements LanguageFootprint { 36 | 37 | @Override 38 | public Set getDetectors() { 39 | return Collections.emptySet(); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/pmd/rules/ClassNameRule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2015 Fabricio Colombo 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Lesser General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 3 of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public 16 | * License along with this program; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 18 | */ 19 | package org.sonar.plugins.delphi.pmd.rules; 20 | 21 | import net.sourceforge.pmd.RuleContext; 22 | import org.sonar.plugins.delphi.antlr.DelphiLexer; 23 | import org.sonar.plugins.delphi.antlr.ast.DelphiPMDNode; 24 | 25 | public class ClassNameRule extends DelphiRule { 26 | 27 | @Override 28 | public void visit(DelphiPMDNode node, RuleContext ctx) { 29 | 30 | if (node.getType() == DelphiLexer.TkClass) { 31 | String name = node.getParent().getText(); 32 | 33 | char firstCharAfterPrefix = name.charAt(1); 34 | 35 | if ((!name.startsWith("T") && !name.startsWith("E")) || firstCharAfterPrefix != Character.toUpperCase(firstCharAfterPrefix)) { 36 | addViolation(ctx, node); 37 | } 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/core/language/verifiers/StatementVerifierException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.core.language.verifiers; 24 | 25 | /** 26 | * Exception for Statementverifier, triggered when we cannot create a statement 27 | * from provided node. 28 | */ 29 | public class StatementVerifierException extends Exception { 30 | 31 | private static final long serialVersionUID = 6052651134635021666L; 32 | 33 | /** 34 | * ctor 35 | * 36 | * @param msg error message 37 | */ 38 | public StatementVerifierException(String msg) { 39 | super(msg); 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/pmd/DelphiParserVisitor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.pmd; 24 | 25 | import net.sourceforge.pmd.ast.JavaParserVisitor; 26 | import org.sonar.plugins.delphi.antlr.ast.DelphiPMDNode; 27 | 28 | /** 29 | * visits delphi pmd node 30 | * 31 | */ 32 | public interface DelphiParserVisitor extends JavaParserVisitor { 33 | 34 | /** 35 | * preform visit to a specific node with given data 36 | * 37 | * @param node node to visit 38 | * @param data data 39 | * @return node data 40 | */ 41 | Object visit(DelphiPMDNode node, Object data); 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/pmd/rules/InterfaceNameRule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2015 Fabricio Colombo 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Lesser General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 3 of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public 16 | * License along with this program; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 18 | */ 19 | package org.sonar.plugins.delphi.pmd.rules; 20 | 21 | import net.sourceforge.pmd.RuleContext; 22 | import org.sonar.plugins.delphi.antlr.DelphiLexer; 23 | import org.sonar.plugins.delphi.antlr.ast.DelphiPMDNode; 24 | 25 | public class InterfaceNameRule extends DelphiRule { 26 | 27 | @Override 28 | public void visit(DelphiPMDNode node, RuleContext ctx) { 29 | 30 | if (node.getType() == DelphiLexer.TkInterface) { 31 | String name = node.getParent().getText(); 32 | 33 | char firstCharAfterPrefix = name.charAt(1); 34 | 35 | if (!name.startsWith("I") || firstCharAfterPrefix != Character.toUpperCase(firstCharAfterPrefix)) { 36 | addViolation(ctx, (DelphiPMDNode) node.getChild(0)); 37 | } 38 | } 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /src/test/java/org/sonar/plugins/delphi/DelphiPluginTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi; 24 | 25 | import org.junit.Before; 26 | import org.junit.Test; 27 | 28 | import static org.hamcrest.MatcherAssert.assertThat; 29 | import static org.hamcrest.Matchers.greaterThan; 30 | 31 | /** 32 | * @author Evgeny Mandrikov 33 | */ 34 | public class DelphiPluginTest { 35 | 36 | private DelphiPlugin plugin; 37 | 38 | @Before 39 | public void setUp() { 40 | plugin = new DelphiPlugin(); 41 | } 42 | 43 | @Test 44 | public void testExtensions() { 45 | assertThat(plugin.getExtensions().size(), greaterThan(0)); 46 | } 47 | 48 | } 49 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/pmd/rules/PointerNameRule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2015 Fabricio Colombo 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Lesser General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 3 of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public 16 | * License along with this program; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 18 | */ 19 | package org.sonar.plugins.delphi.pmd.rules; 20 | 21 | import net.sourceforge.pmd.RuleContext; 22 | import org.sonar.plugins.delphi.antlr.DelphiLexer; 23 | import org.sonar.plugins.delphi.antlr.ast.DelphiPMDNode; 24 | 25 | public class PointerNameRule extends DelphiRule { 26 | 27 | @Override 28 | public void visit(DelphiPMDNode node, RuleContext ctx) { 29 | if (isImplementationSection()) { 30 | return; 31 | } 32 | 33 | if (node.getType() == DelphiLexer.POINTER2) { 34 | String name = node.getParent().getText(); 35 | 36 | char firstCharAfterPrefix = name.charAt(1); 37 | 38 | if (!name.startsWith("P") || firstCharAfterPrefix != Character.toUpperCase(firstCharAfterPrefix)) { 39 | addViolation(ctx, node); 40 | } 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /src/test/java/org/sonar/plugins/delphi/IssueMatchers.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2015 Fabricio Colombo 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Lesser General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 3 of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public 16 | * License along with this program; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 18 | */ 19 | package org.sonar.plugins.delphi; 20 | 21 | import org.hamcrest.Matcher; 22 | import org.hamcrest.Matchers; 23 | import org.sonar.api.issue.Issue; 24 | 25 | public class IssueMatchers { 26 | 27 | public static Matcher hasRuleKey(String key) { 28 | return HasRuleKey.hasRuleKey(key); 29 | } 30 | 31 | public static Matcher hasRuleMessage(String message) { 32 | return HasRuleMessage.hasRuleMessage(message); 33 | } 34 | 35 | public static Matcher hasRuleLine(int line) { 36 | return HasRuleLineNumber.hasRuleLine(line); 37 | } 38 | 39 | public static Matcher hasRuleKeyAtLine(String key, int line) { 40 | return Matchers.allOf(HasRuleKey.hasRuleKey(key), hasRuleLine(line)); 41 | } 42 | 43 | } 44 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/antlr/sanitizer/resolvers/exceptions/IncludeResolverException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.antlr.sanitizer.resolvers.exceptions; 24 | 25 | /** 26 | * Include resolver exception 27 | */ 28 | public class IncludeResolverException extends Exception { 29 | 30 | private static final long serialVersionUID = 8547740813940921496L; 31 | 32 | /** 33 | * default ctor with no message 34 | */ 35 | public IncludeResolverException() { 36 | super(); 37 | } 38 | 39 | /** 40 | * ctor 41 | * 42 | * @param msg Error message 43 | */ 44 | public IncludeResolverException(String msg) { 45 | super(msg); 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/antlr/directives/exceptions/CompilerDirectiveFactorySyntaxException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.antlr.directives.exceptions; 24 | 25 | /** 26 | * Compiler directive factory exception 27 | */ 28 | public class CompilerDirectiveFactorySyntaxException extends Exception { 29 | 30 | private static final long serialVersionUID = -1053134005528284924L; 31 | 32 | /** 33 | * {@inheritDoc} 34 | */ 35 | public CompilerDirectiveFactorySyntaxException(String msg) { 36 | super(msg); 37 | } 38 | 39 | /** 40 | * {@inheritDoc} 41 | */ 42 | public CompilerDirectiveFactorySyntaxException() { 43 | super(); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/antlr/directives/exceptions/CompilerDirectiveFactoryUnsupportedDirectiveException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.antlr.directives.exceptions; 24 | 25 | /** 26 | * Is thrown when we could not generate compiler directive that was supported 27 | * (implemented) 28 | */ 29 | public class CompilerDirectiveFactoryUnsupportedDirectiveException extends Exception { 30 | 31 | private static final long serialVersionUID = -5165381856939059460L; 32 | 33 | /** 34 | * ctor 35 | * 36 | * @param msg Error message 37 | */ 38 | public CompilerDirectiveFactoryUnsupportedDirectiveException(String msg) { 39 | super(msg); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/metrics/FunctionMetricsTest.pas: -------------------------------------------------------------------------------- 1 | unit DemoForm; 2 | 3 | interface 4 | 5 | type 6 | TDemo = class(TMyClass, TMyElder, TMyAncestor) 7 | private 8 | bShowTracker: TButton; 9 | public 10 | procedure bShowTrackerClick(); 11 | function getFunction(): integer; 12 | field1,field2: integer; 13 | protected 14 | bShowTracker: TButton; 15 | end; 16 | 17 | type 18 | TMyClass = class 19 | procedure myProcedure(); 20 | procedure setSomething(); 21 | protected 22 | protectedField: real; 23 | end; 24 | 25 | type 26 | TMyAncestor = class(TMyElder) 27 | end; 28 | 29 | type 30 | TMyElder = class 31 | end; 32 | 33 | var 34 | fDemo1, fDemo2: TfDemo; 35 | bigInt: Integer; 36 | 37 | implementation 38 | 39 | {*** 40 | **** TDEMO 41 | ***} 42 | function TDemo.getFunction(): integer; 43 | begin 44 | result := 7; 45 | x := 5; 46 | end; 47 | 48 | procedure TDemo.bShowTrackerClick(); 49 | var 50 | str: string; 51 | begin 52 | str := 'sample string'; 53 | end; 54 | 55 | {*** 56 | **** TMYCLASS 57 | ***} 58 | procedure TMyClass.myProcedure(); 59 | var 60 | i: integer; 61 | begin 62 | for i := 1 to 5 do 63 | begin 64 | writeln(i); 65 | i := fDemo1.getFunction(); 66 | end; 67 | end; 68 | 69 | procedure TMyClass.setSomething(); 70 | begin 71 | setSomething; 72 | end; 73 | 74 | {******* STAND ALONE GLOBAL FUNCTIONS ****} 75 | procedure StandAloneProcedure(x, y: real; z: boolean; var ppp); 76 | var 77 | x,y: integer; 78 | begin 79 | x := 5; 80 | y := 7; 81 | while x < 500 do 82 | begin 83 | if(x > 7) then y := 2 84 | else y := 0; 85 | end; 86 | 87 | end; 88 | 89 | function StandAloneFunction(var x: TdDemo): integer; 90 | begin 91 | StandAloneProcedure(1,1); 92 | end; 93 | 94 | end. 95 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/pmd/DelphiRuleChain.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.pmd; 24 | 25 | import net.sourceforge.pmd.Language; 26 | import net.sourceforge.pmd.RuleChain; 27 | import net.sourceforge.pmd.RuleContext; 28 | import net.sourceforge.pmd.ast.CompilationUnit; 29 | 30 | import java.util.List; 31 | 32 | /** 33 | * Delphi pmd rule chain 34 | */ 35 | public class DelphiRuleChain extends RuleChain { 36 | 37 | private DelphiRuleChainVisitor visitor = new DelphiRuleChainVisitor(); 38 | 39 | @Override 40 | public void apply(List astCompilationUnits, RuleContext ctx, Language language) { 41 | visitor.visitAll(astCompilationUnits, ctx); 42 | } 43 | 44 | } 45 | -------------------------------------------------------------------------------- /src/test/java/org/sonar/plugins/delphi/antlr/analyzer/CodeAnalisysResultsTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.antlr.analyzer; 24 | 25 | import org.junit.Before; 26 | import org.junit.Test; 27 | import org.sonar.plugins.delphi.core.language.impl.DelphiUnit; 28 | 29 | import static org.junit.Assert.assertEquals; 30 | 31 | public class CodeAnalisysResultsTest { 32 | 33 | private CodeAnalysisResults result; 34 | 35 | @Before 36 | public void init() { 37 | result = new CodeAnalysisResults(); 38 | } 39 | 40 | @Test 41 | public void getActiveUnitTest() { 42 | result.setActiveUnit(new DelphiUnit("test")); 43 | assertEquals(new DelphiUnit("test"), result.getActiveUnit()); 44 | } 45 | 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/core/language/ArgumentInterface.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.core.language; 24 | 25 | /** 26 | * Interface used for function arguments 27 | */ 28 | public interface ArgumentInterface { 29 | 30 | /** 31 | * Sets name 32 | * 33 | * @param newName New name 34 | */ 35 | void setName(String newName); 36 | 37 | /** 38 | * Sets type 39 | * 40 | * @param newType New type 41 | */ 42 | void setType(String newType); 43 | 44 | /** 45 | * Gets name 46 | * 47 | * @return Argument name 48 | */ 49 | String getName(); 50 | 51 | /** 52 | * Gets type 53 | * 54 | * @return Argument type 55 | */ 56 | String getType(); 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/pmd/rules/UnitNameRule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2015 Fabricio Colombo 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Lesser General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 3 of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public 16 | * License along with this program; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 18 | */ 19 | package org.sonar.plugins.delphi.pmd.rules; 20 | 21 | import net.sourceforge.pmd.RuleContext; 22 | import org.antlr.runtime.tree.Tree; 23 | import org.sonar.plugins.delphi.antlr.DelphiLexer; 24 | import org.sonar.plugins.delphi.antlr.ast.DelphiPMDNode; 25 | 26 | public class UnitNameRule extends DelphiRule { 27 | 28 | @Override 29 | public void visit(DelphiPMDNode node, RuleContext ctx) { 30 | 31 | if (node.getType() == DelphiLexer.UNIT) { 32 | for (int i = 0; i < node.getChildCount(); i++) { 33 | Tree child = node.getChild(i); 34 | 35 | if (child.getType() == DelphiLexer.DOT) { 36 | continue; 37 | } 38 | 39 | char firstChar = child.getText().charAt(0); 40 | if (firstChar != Character.toUpperCase(firstChar)) { 41 | addViolation(ctx, node); 42 | break; 43 | } 44 | } 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/pmd/rules/IfNotFalseRule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.pmd.rules; 24 | 25 | import org.sonar.plugins.delphi.antlr.ast.DelphiPMDNode; 26 | 27 | /** 28 | * Checks for rule violation: if not (x = false) then (redundant) 29 | */ 30 | public class IfNotFalseRule extends IfTrueRule { 31 | 32 | protected boolean wasNot; 33 | 34 | @Override 35 | protected void init() { 36 | super.init(); 37 | wasNot = false; 38 | setStringToSearch("false"); 39 | } 40 | 41 | @Override 42 | protected boolean accept(DelphiPMDNode node) { 43 | if (!wasNot && "not".equals(node.getText())) { 44 | wasNot = true; 45 | } else { 46 | return super.accept(node); 47 | } 48 | return false; 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/antlr/sanitizer/subranges/impl/ReplacementSubRange.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.antlr.sanitizer.subranges.impl; 24 | 25 | /** 26 | * Used to replace a specific range with a specific string 27 | */ 28 | public class ReplacementSubRange extends IntegerSubRange { 29 | 30 | private String replacementString = null; 31 | 32 | /** 33 | * ctor 34 | * 35 | * @param rangeStart start index 36 | * @param rangeEnd end index 37 | * @param str string, which we will replace with 38 | */ 39 | public ReplacementSubRange(int rangeStart, int rangeEnd, String str) { 40 | super(rangeStart, rangeEnd); 41 | replacementString = str; 42 | } 43 | 44 | @Override 45 | public String toString() { 46 | return replacementString; 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/pmd/rules/WithAfterDoThenRule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.pmd.rules; 24 | 25 | import org.antlr.runtime.tree.Tree; 26 | import org.sonar.plugins.delphi.antlr.analyzer.LexerMetrics; 27 | 28 | /** 29 | * Checks, if we DIDN'T put 'with' directly after 'do' statement 30 | */ 31 | public class WithAfterDoThenRule extends NoBeginAfterDoRule { 32 | 33 | @Override 34 | protected boolean isWrongNode(Tree node) { 35 | return node != null 36 | && (node.getType() == LexerMetrics.WITH.toMetrics() || node.getType() == LexerMetrics.THEN.toMetrics()); 37 | } 38 | 39 | @Override 40 | protected boolean shouldCheck(Tree node) { 41 | return node != null 42 | && (node.getType() == LexerMetrics.DO.toMetrics() || node.getType() == LexerMetrics.THEN.toMetrics()); 43 | } 44 | 45 | } 46 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/utils/ProgressReporterLogger.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.utils; 24 | 25 | import org.slf4j.Logger; 26 | 27 | /** 28 | * Logger used to report progress from ProgressReporter 29 | */ 30 | public class ProgressReporterLogger 31 | { 32 | private Logger log = null; 33 | 34 | /** 35 | * Default ctor, no logging 36 | */ 37 | public ProgressReporterLogger() { 38 | } 39 | 40 | /** 41 | * Ctor, specify your own logger to write to 42 | * 43 | * @param log logger to write to 44 | */ 45 | public ProgressReporterLogger(Logger log) { 46 | this.log = log; 47 | } 48 | 49 | /** 50 | * log a message 51 | * 52 | * @param msg message 53 | */ 54 | public void log(String msg) { 55 | if (log != null) { 56 | log.info(msg); 57 | } 58 | } 59 | 60 | } 61 | -------------------------------------------------------------------------------- /src/test/java/org/sonar/plugins/delphi/HasRuleKey.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2015 Fabricio Colombo 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Lesser General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 3 of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public 16 | * License along with this program; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 18 | */ 19 | package org.sonar.plugins.delphi; 20 | 21 | import org.hamcrest.Description; 22 | import org.hamcrest.Matcher; 23 | import org.hamcrest.TypeSafeMatcher; 24 | import org.sonar.api.issue.Issue; 25 | 26 | public class HasRuleKey extends TypeSafeMatcher { 27 | 28 | private final String key; 29 | 30 | public HasRuleKey(String key) { 31 | this.key = key; 32 | } 33 | 34 | @Override 35 | protected boolean matchesSafely(T item) { 36 | return key.equals(item.ruleKey().rule()); 37 | } 38 | 39 | @Override 40 | public void describeTo(Description description) { 41 | description.appendText("ruleKey ").appendValue(key); 42 | } 43 | 44 | @Override 45 | protected void describeMismatchSafely(T item, Description mismatchDescription) { 46 | mismatchDescription.appendText("was ").appendValue(item.ruleKey().rule()); 47 | } 48 | 49 | public static Matcher hasRuleKey(String key) { 50 | return new HasRuleKey(key); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/antlr/analyzer/CodeNode.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.antlr.analyzer; 24 | 25 | /** 26 | * Template class holding specific node 27 | */ 28 | public class CodeNode { 29 | 30 | private T node; 31 | 32 | /** 33 | * ctor 34 | * 35 | * @param n node to hold 36 | */ 37 | public CodeNode(T n) { 38 | node = n; 39 | } 40 | 41 | /** 42 | * @return node 43 | */ 44 | public T getNode() { 45 | return node; 46 | } 47 | 48 | /** 49 | * is node valid 50 | * 51 | * @return Returns true if the CodeNode has a node. 52 | */ 53 | public boolean isValid() { 54 | return node != null; 55 | } 56 | 57 | @Override 58 | public String toString() { 59 | if (node == null) { 60 | return "null"; 61 | } 62 | return node.toString(); 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/core/language/ClassFieldInterface.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.core.language; 24 | 25 | /** 26 | * Interface used for class fields (variables). 27 | */ 28 | public interface ClassFieldInterface extends ArgumentInterface { 29 | 30 | /** 31 | * Sets field parent (class it resides in) 32 | * 33 | * @param cl Parent class 34 | */ 35 | void setParent(ClassInterface cl); 36 | 37 | /** 38 | * Get parent (class it resides in) 39 | * 40 | * @return Parent class 41 | */ 42 | ClassInterface getParent(); 43 | 44 | /** 45 | * @return Fields visibility (, protected or private) 46 | */ 47 | int getVisibility(); 48 | 49 | /** 50 | * Sets class field visibility 51 | * 52 | * @param value , protected of private 53 | */ 54 | void setVisibility(int value); 55 | } 56 | -------------------------------------------------------------------------------- /src/test/java/org/sonar/plugins/delphi/HasRuleLineNumber.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2015 Fabricio Colombo 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Lesser General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 3 of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public 16 | * License along with this program; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 18 | */ 19 | package org.sonar.plugins.delphi; 20 | 21 | import org.hamcrest.Description; 22 | import org.hamcrest.Matcher; 23 | import org.hamcrest.TypeSafeMatcher; 24 | import org.sonar.api.issue.Issue; 25 | 26 | public class HasRuleLineNumber extends TypeSafeMatcher { 27 | 28 | private final int line; 29 | 30 | public HasRuleLineNumber(int line) { 31 | this.line = line; 32 | } 33 | 34 | @Override 35 | protected boolean matchesSafely(T item) { 36 | return line == item.line(); 37 | } 38 | 39 | @Override 40 | public void describeTo(Description description) { 41 | description.appendText("rule line ").appendValue(line); 42 | } 43 | 44 | @Override 45 | protected void describeMismatchSafely(T item, Description mismatchDescription) { 46 | mismatchDescription.appendText("was ").appendValue(item.line()); 47 | } 48 | 49 | public static Matcher hasRuleLine(int line) { 50 | return new HasRuleLineNumber(line); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/test/java/org/sonar/plugins/delphi/debug/FileTestsCommon.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.debug; 24 | 25 | import org.sonar.plugins.delphi.utils.DelphiUtils; 26 | 27 | import java.io.*; 28 | 29 | public class FileTestsCommon { 30 | 31 | protected static File testFile = null; 32 | protected static StringBuilder testFileString = null; 33 | 34 | protected static void loadFile(String fileName) throws IOException { 35 | testFile = DelphiUtils.getResource(fileName); 36 | testFileString = new StringBuilder(); 37 | 38 | String line; 39 | BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(testFile), "UTF-8")); 40 | while ((line = reader.readLine()) != null) { 41 | testFileString.append(line); 42 | testFileString.append('\n'); 43 | } 44 | reader.close(); 45 | } 46 | 47 | } 48 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/antlr/directives/impl/IfDirective.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.antlr.directives.impl; 24 | 25 | import org.sonar.plugins.delphi.antlr.directives.CompilerDirectiveType; 26 | 27 | /** 28 | * {$if ...} directive 29 | */ 30 | public class IfDirective extends CommonCompilerDirective { 31 | 32 | private static final String DIRECTIVE_NAME = "if"; 33 | 34 | /** 35 | * ctor 36 | * 37 | * @param item directive item 38 | * @param firstCharPos first character position 39 | * @param lastCharPos last character position 40 | */ 41 | public IfDirective(String item, int firstCharPos, int lastCharPos) { 42 | super(DIRECTIVE_NAME, item, firstCharPos, lastCharPos); 43 | } 44 | 45 | @Override 46 | public CompilerDirectiveType getType() { 47 | return CompilerDirectiveType.IF; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/antlr/directives/impl/ElseDirective.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.antlr.directives.impl; 24 | 25 | import org.sonar.plugins.delphi.antlr.directives.CompilerDirectiveType; 26 | 27 | /** 28 | * {$else} or {$elseif ...} directive 29 | */ 30 | public class ElseDirective extends CommonCompilerDirective { 31 | 32 | private static final String DIRECTIVE_NAME = "else"; 33 | 34 | /** 35 | * ctor 36 | * 37 | * @param item item 38 | * @param firstCharPos first character position 39 | * @param lastCharPos last character position 40 | */ 41 | public ElseDirective(String item, int firstCharPos, int lastCharPos) { 42 | super(DIRECTIVE_NAME, item, firstCharPos, lastCharPos); 43 | } 44 | 45 | @Override 46 | public CompilerDirectiveType getType() { 47 | return CompilerDirectiveType.ELSE; 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/antlr/directives/impl/EndIfDirective.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.antlr.directives.impl; 24 | 25 | import org.sonar.plugins.delphi.antlr.directives.CompilerDirectiveType; 26 | 27 | /** 28 | * {$endif} directive 29 | */ 30 | public class EndIfDirective extends CommonCompilerDirective { 31 | 32 | private static final String DIRECTIVE_NAME = "endif"; 33 | 34 | /** 35 | * ctor 36 | * 37 | * @param item directive item 38 | * @param firstCharPos first character position 39 | * @param lastCharPos last character position 40 | */ 41 | public EndIfDirective(String item, int firstCharPos, int lastCharPos) { 42 | super(DIRECTIVE_NAME, item, firstCharPos, lastCharPos); 43 | } 44 | 45 | @Override 46 | public CompilerDirectiveType getType() { 47 | return CompilerDirectiveType.ENDIF; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/antlr/directives/impl/IfEndDirective.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.antlr.directives.impl; 24 | 25 | import org.sonar.plugins.delphi.antlr.directives.CompilerDirectiveType; 26 | 27 | /** 28 | * {$ifend} directive 29 | */ 30 | public class IfEndDirective extends CommonCompilerDirective { 31 | 32 | private static final String DIRECTIVE_NAME = "ifend"; 33 | 34 | /** 35 | * ctor 36 | * 37 | * @param item directive item 38 | * @param firstCharPos first character position 39 | * @param lastCharPos last character position 40 | */ 41 | public IfEndDirective(String item, int firstCharPos, int lastCharPos) { 42 | super(DIRECTIVE_NAME, item, firstCharPos, lastCharPos); 43 | } 44 | 45 | @Override 46 | public CompilerDirectiveType getType() { 47 | return CompilerDirectiveType.IFEND; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/test/java/org/sonar/plugins/delphi/pmd/RuleData.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | 24 | package org.sonar.plugins.delphi.pmd; 25 | 26 | import java.util.Comparator; 27 | 28 | class RuleData { 29 | 30 | private String name; 31 | private int line; 32 | 33 | public RuleData(String _name, int _line) { 34 | name = _name; 35 | line = _line; 36 | } 37 | 38 | public String getName() { 39 | return name; 40 | } 41 | 42 | public int getLine() { 43 | return line; 44 | } 45 | 46 | @Override 47 | public String toString() { 48 | return "RuleData [name=" + name + ", line=" + line + "]"; 49 | } 50 | 51 | public static Comparator getComparator() { 52 | return new Comparator() { 53 | 54 | @Override 55 | public int compare(RuleData o1, RuleData o2) { 56 | return o1.getLine() - o2.getLine(); 57 | } 58 | }; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/pmd/DelphiRuleChainVisitor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.pmd; 24 | 25 | import net.sourceforge.pmd.AbstractRuleChainVisitor; 26 | import net.sourceforge.pmd.Rule; 27 | import net.sourceforge.pmd.RuleContext; 28 | import net.sourceforge.pmd.ast.CompilationUnit; 29 | import net.sourceforge.pmd.ast.JavaNode; 30 | import net.sourceforge.pmd.ast.SimpleNode; 31 | 32 | import java.util.List; 33 | 34 | /** 35 | * Delphi pmd rule chain visitor 36 | */ 37 | public class DelphiRuleChainVisitor extends AbstractRuleChainVisitor { 38 | 39 | @Override 40 | protected void visit(Rule rule, SimpleNode node, RuleContext ctx) { 41 | ((JavaNode) node).jjtAccept((DelphiParserVisitor) rule, ctx); 42 | } 43 | 44 | @Override 45 | protected void indexNodes(List astCompilationUnits, RuleContext ctx) { 46 | 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /src/test/java/org/sonar/plugins/delphi/HasRuleMessage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2015 Fabricio Colombo 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Lesser General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 3 of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public 16 | * License along with this program; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 18 | */ 19 | package org.sonar.plugins.delphi; 20 | 21 | import org.hamcrest.Description; 22 | import org.hamcrest.Matcher; 23 | import org.hamcrest.TypeSafeMatcher; 24 | import org.sonar.api.issue.Issue; 25 | 26 | public class HasRuleMessage extends TypeSafeMatcher { 27 | 28 | private final String message; 29 | 30 | public HasRuleMessage(String message) { 31 | this.message = message; 32 | } 33 | 34 | @Override 35 | protected boolean matchesSafely(T item) { 36 | return message.equals(item.message()); 37 | } 38 | 39 | @Override 40 | public void describeTo(Description description) { 41 | description.appendText("message ").appendValue(message); 42 | } 43 | 44 | @Override 45 | protected void describeMismatchSafely(T item, Description mismatchDescription) { 46 | mismatchDescription.appendText("was ").appendValue(item.message()); 47 | } 48 | 49 | public static Matcher hasRuleMessage(String message) { 50 | return new HasRuleMessage(message); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/antlr/directives/impl/DefineDirective.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.antlr.directives.impl; 24 | 25 | import org.sonar.plugins.delphi.antlr.directives.CompilerDirectiveType; 26 | 27 | /** 28 | * {$define XXX} directive 29 | */ 30 | public class DefineDirective extends CommonCompilerDirective { 31 | 32 | private static final String DIRECTIVE_NAME = "define"; 33 | 34 | /** 35 | * ctor 36 | * 37 | * @param item directive item 38 | * @param firstCharPos first character position 39 | * @param lastCharPos last character position 40 | */ 41 | public DefineDirective(String item, int firstCharPos, int lastCharPos) { 42 | super(DIRECTIVE_NAME, item, firstCharPos, lastCharPos); 43 | } 44 | 45 | @Override 46 | public CompilerDirectiveType getType() { 47 | return CompilerDirectiveType.DEFINE; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/core/language/HasNameInterface.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.core.language; 24 | 25 | /** 26 | * For each class that consists of name and realName (real name is not converted 27 | * to lowercase) 28 | */ 29 | public interface HasNameInterface { 30 | 31 | /** 32 | * Gets function name (lowercase) 33 | * 34 | * @return Function name 35 | */ 36 | String getName(); 37 | 38 | /** 39 | * Sets function name 40 | * 41 | * @param name New function name 42 | */ 43 | void setName(String name); 44 | 45 | /** 46 | * Gets function real name (not converted to lowercase) 47 | * 48 | * @return Function real name 49 | */ 50 | String getRealName(); 51 | 52 | /** 53 | * Sets function real name 54 | * 55 | * @param name New function real name 56 | */ 57 | void setRealName(String name); 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/antlr/directives/impl/UndefineDirective.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.antlr.directives.impl; 24 | 25 | import org.sonar.plugins.delphi.antlr.directives.CompilerDirectiveType; 26 | 27 | /** 28 | * {$UNDEF ...} directive 29 | */ 30 | public class UndefineDirective extends CommonCompilerDirective { 31 | 32 | private static final String DIRECTIVE_NAME = "undefine"; 33 | 34 | /** 35 | * ctor 36 | * 37 | * @param item directive item 38 | * @param firstCharPos first character position 39 | * @param lastCharPos last character position 40 | */ 41 | public UndefineDirective(String item, int firstCharPos, int lastCharPos) { 42 | super(DIRECTIVE_NAME, item, firstCharPos, lastCharPos); 43 | } 44 | 45 | @Override 46 | public CompilerDirectiveType getType() { 47 | return CompilerDirectiveType.UNDEFINE; 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/antlr/analyzer/impl/operations/AdvanceNodeOperation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.antlr.analyzer.impl.operations; 24 | 25 | import org.antlr.runtime.tree.Tree; 26 | import org.sonar.plugins.delphi.antlr.analyzer.CodeNode; 27 | 28 | /** 29 | * Created by IntelliJ IDEA. User: sg0501095 Date: 11/30/11 Time: 10:32 AM To 30 | * change this template use File | Settings | File Templates. 31 | */ 32 | public class AdvanceNodeOperation implements NodeOperation { 33 | 34 | @Override 35 | public CodeNode execute(Tree node) { 36 | if (node == null) { 37 | return new CodeNode(null); 38 | } 39 | 40 | if (node.getChildCount() == 0) { 41 | // no child, traceback to parent 42 | return (new TraceBackNodeOperation().execute(node)); 43 | } else { 44 | // get first child 45 | return new CodeNode(node.getChild(0)); 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/test/java/org/sonar/plugins/delphi/antlr/analyzer/impl/operations/OperationsTestsCommon.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.antlr.analyzer.impl.operations; 24 | 25 | import org.antlr.runtime.CommonToken; 26 | import org.antlr.runtime.tree.CommonTree; 27 | import org.antlr.runtime.tree.Tree; 28 | 29 | class OperationsTestsCommon // private class 30 | { 31 | 32 | protected final static int LAYER_NODES = 3; 33 | 34 | protected NodeOperation operation; 35 | protected Tree parent; 36 | 37 | public OperationsTestsCommon(NodeOperation op) { 38 | operation = op; 39 | } 40 | 41 | public void init() { 42 | parent = new CommonTree(new CommonToken(256, "parent")); 43 | for (int i = 0; i < LAYER_NODES; ++i) { 44 | CommonTree child = new CommonTree(new CommonToken(i + 1, "layer1")); 45 | child.addChild(new CommonTree(new CommonToken(i + 100, "layer2"))); 46 | parent.addChild(child); 47 | } 48 | } 49 | 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/pmd/rules/FieldNameRule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2015 Fabricio Colombo 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Lesser General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 3 of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public 16 | * License along with this program; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 18 | */ 19 | package org.sonar.plugins.delphi.pmd.rules; 20 | 21 | import net.sourceforge.pmd.RuleContext; 22 | import org.antlr.runtime.tree.Tree; 23 | import org.sonar.plugins.delphi.antlr.DelphiLexer; 24 | import org.sonar.plugins.delphi.antlr.ast.DelphiPMDNode; 25 | 26 | public class FieldNameRule extends DelphiRule { 27 | 28 | @Override 29 | protected void init() { 30 | super.init(); 31 | } 32 | 33 | @Override 34 | public void visit(DelphiPMDNode node, RuleContext ctx) { 35 | if (node.getType() == DelphiLexer.TkClassField) { 36 | 37 | if (!isPublished()) { 38 | Tree variableIdentsNode = node.getChild(0); 39 | String name = variableIdentsNode.getChild(0).getText(); 40 | if (name.length() > 1) { 41 | char firstCharAfterPrefix = name.charAt(1); 42 | 43 | if (!name.startsWith("F") || firstCharAfterPrefix != Character.toUpperCase(firstCharAfterPrefix)) { 44 | addViolation(ctx, node); 45 | } 46 | } else { 47 | // a single letter name has no prefix 48 | addViolation(ctx, node); 49 | } 50 | } 51 | } 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/pmd/rules/IfTrueRule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.pmd.rules; 24 | 25 | import org.sonar.plugins.delphi.antlr.ast.DelphiPMDNode; 26 | 27 | /** 28 | * Checks for rule violation: if x = true then (redundant) 29 | */ 30 | public class IfTrueRule extends BlockCounterRule { 31 | 32 | private boolean wasEquals; 33 | 34 | @Override 35 | protected void init() { 36 | super.init(); 37 | wasEquals = false; 38 | setStringToSearch("true"); 39 | } 40 | 41 | @Override 42 | protected boolean accept(DelphiPMDNode node) { 43 | if (!wasEquals && "=".equals(node.getText())) { 44 | wasEquals = true; 45 | } else if (wasEquals && node.getText().equals(getStringToSearch())) { 46 | // save this node as violation 47 | firstNode = node; 48 | return true; 49 | } else { 50 | // reset if 'true' is not directly after '=' 51 | wasEquals = false; 52 | } 53 | 54 | return false; 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /samples/ant/SampleProject/build.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/pmd/rules/MethodNameRule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2015 Fabricio Colombo 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Lesser General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 3 of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public 16 | * License along with this program; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 18 | */ 19 | package org.sonar.plugins.delphi.pmd.rules; 20 | 21 | import net.sourceforge.pmd.RuleContext; 22 | import org.antlr.runtime.tree.Tree; 23 | import org.sonar.plugins.delphi.antlr.DelphiLexer; 24 | import org.sonar.plugins.delphi.antlr.ast.DelphiPMDNode; 25 | 26 | import java.util.List; 27 | 28 | public class MethodNameRule extends DelphiRule { 29 | 30 | @Override 31 | public void visit(DelphiPMDNode node, RuleContext ctx) { 32 | 33 | if (node.getType() == DelphiLexer.TkNewType) { 34 | if (isInterface(node) || !isPublished()) { 35 | List methodNodes = node.findAllChildren(DelphiLexer.TkFunctionName); 36 | 37 | for (Tree method : methodNodes) { 38 | String name = method.getChild(0).getText(); 39 | char firstChar = name.charAt(0); 40 | 41 | if (firstChar != Character.toUpperCase(firstChar)) { 42 | addViolation(ctx, (DelphiPMDNode) method); 43 | } 44 | } 45 | } 46 | } 47 | } 48 | 49 | private boolean isInterface(DelphiPMDNode node) { 50 | return node.getChild(0).getChild(0).getType() == DelphiLexer.TkInterface; 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/test/java/org/sonar/plugins/delphi/antlr/sanitizer/subranges/impl/StringSubRangeTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.antlr.sanitizer.subranges.impl; 24 | 25 | import org.junit.Before; 26 | import org.junit.Test; 27 | import org.sonar.plugins.delphi.antlr.sanitizer.subranges.SubRange; 28 | 29 | import static org.junit.Assert.assertEquals; 30 | 31 | public class StringSubRangeTest { 32 | 33 | private SubRange range; 34 | private String str; 35 | 36 | @Before 37 | public void setup() { 38 | str = "!This is a test string!"; 39 | range = new StringSubRange(0, str.length(), str); 40 | } 41 | 42 | @Test 43 | public void toStringTest() { 44 | assertEquals("[0, 23] !This is a test string!", range.toString()); 45 | 46 | range.setBegin(0); 47 | range.setEnd(10); 48 | assertEquals("[0, 10] !This is a", range.toString()); 49 | 50 | range.setEnd(15); 51 | range.setBegin(11); 52 | assertEquals("[11, 15] test", range.toString()); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/pmd/rules/ThenTryRule.java: -------------------------------------------------------------------------------- 1 | // $ANTLR 3.5.2 org\\sonar\\plugins\\delphi\\antlr\\Delphi.g 2016-07-19 17:58:30 2 | 3 | /* 4 | * Sonar Delphi Plugin 5 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 6 | * Author(s): 7 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 8 | * Michal Wojcik (michal.wojcik@sabre.com) 9 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 10 | * 11 | * This program is free software; you can redistribute it and/or 12 | * modify it under the terms of the GNU Lesser General Public 13 | * License as published by the Free Software Foundation; either 14 | * version 3 of the License, or (at your option) any later version. 15 | * 16 | * This program is distributed in the hope that it will be useful, 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 19 | * Lesser General Public License for more details. 20 | * 21 | * You should have received a copy of the GNU Lesser General Public 22 | * License along with this program; if not, write to the Free Software 23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 24 | */ 25 | package org.sonar.plugins.delphi.pmd.rules; 26 | 27 | import net.sourceforge.pmd.RuleContext; 28 | import org.sonar.plugins.delphi.antlr.DelphiLexer; 29 | import org.sonar.plugins.delphi.antlr.ast.DelphiNode; 30 | import org.sonar.plugins.delphi.antlr.ast.DelphiPMDNode; 31 | 32 | /** 33 | * "Try" should always be preceeded with "Begin" after "Then" 34 | * 35 | */ 36 | public class ThenTryRule extends DelphiRule { 37 | 38 | @Override 39 | public void visit(DelphiPMDNode node, RuleContext ctx) { 40 | if (node.getType() != DelphiLexer.THEN) { 41 | return; 42 | } 43 | 44 | DelphiNode parent = (DelphiNode) node.getParent(); 45 | 46 | // get next node ident 47 | int nextNode = parent.getChildType(node.getChildIndex() + 1); 48 | 49 | if (nextNode == DelphiLexer.TRY) { 50 | addViolation(ctx, node); 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/test/java/org/sonar/plugins/delphi/pmd/DelphiPMDTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.pmd; 24 | 25 | import net.sourceforge.pmd.ast.CompilationUnit; 26 | import org.antlr.runtime.RecognitionException; 27 | import org.junit.Test; 28 | import org.sonar.plugins.delphi.antlr.ast.DelphiAST; 29 | import org.sonar.plugins.delphi.utils.DelphiUtils; 30 | 31 | import java.io.File; 32 | import java.io.IOException; 33 | import java.util.List; 34 | 35 | import static org.junit.Assert.assertEquals; 36 | 37 | public class DelphiPMDTest { 38 | 39 | private static final String TEST_FILE = "/org/sonar/plugins/delphi/PMDTest/smallpmd.pas"; 40 | 41 | @Test 42 | public void getNodesFromASTTest() throws IOException, RecognitionException { 43 | File testFile = DelphiUtils.getResource(TEST_FILE); 44 | DelphiPMD pmd = new DelphiPMD(); 45 | DelphiAST ast = new DelphiAST(testFile); 46 | List nodes = pmd.getNodesFromAST(ast); 47 | 48 | assertEquals(7, nodes.size()); 49 | } 50 | 51 | } 52 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/antlr/sanitizer/SourceResolver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.antlr.sanitizer; 24 | 25 | import org.sonar.plugins.delphi.antlr.sanitizer.resolvers.SourceResolverResults; 26 | 27 | /** 28 | * Abstract class for source resolvers, applies chain-of-command design pattern 29 | */ 30 | public abstract class SourceResolver { 31 | 32 | SourceResolver next = null; 33 | 34 | /** 35 | * chain next resolver 36 | * 37 | * @param successor resolver to chain 38 | * @return chained resolver 39 | */ 40 | public SourceResolver chain(SourceResolver successor) { 41 | next = successor; 42 | return next; 43 | } 44 | 45 | /** 46 | * resolves 47 | * 48 | * @param results Class to holding results for resolvers 49 | */ 50 | public void resolve(SourceResolverResults results) { 51 | doResolve(results); 52 | if (next != null) { 53 | next.resolve(results); 54 | } 55 | } 56 | 57 | protected abstract void doResolve(SourceResolverResults results); 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/antlr/directives/impl/UnusedDirective.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.antlr.directives.impl; 24 | 25 | import org.sonar.plugins.delphi.antlr.directives.CompilerDirectiveType; 26 | 27 | /** 28 | * Class for unused compiler directives, such as: {$I+-} {$WARN} and so on. They 29 | * do not affect parsing and processing a certain file. This class will prevent 30 | * from writing CompilerDirectiveFactoryUnsupportedDirectiveException to log 31 | * file. 32 | */ 33 | public class UnusedDirective extends CommonCompilerDirective { 34 | 35 | /** 36 | * ctor 37 | * 38 | * @param firstCharPos first character position 39 | * @param lastCharPos last character position 40 | */ 41 | public UnusedDirective(int firstCharPos, int lastCharPos) { 42 | super("unused", "", firstCharPos, lastCharPos); 43 | } 44 | 45 | /** 46 | * returns UNUSED 47 | */ 48 | 49 | @Override 50 | public CompilerDirectiveType getType() { 51 | return CompilerDirectiveType.UNUSED; 52 | } 53 | 54 | } 55 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/antlr/analyzer/impl/operations/TraceBackNodeOperation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.antlr.analyzer.impl.operations; 24 | 25 | import org.antlr.runtime.tree.Tree; 26 | import org.sonar.plugins.delphi.antlr.analyzer.CodeNode; 27 | 28 | /** 29 | * Created by IntelliJ IDEA. User: sg0501095 Date: 11/30/11 Time: 10:31 AM To 30 | * change this template use File | Settings | File Templates. 31 | */ 32 | public class TraceBackNodeOperation implements NodeOperation { 33 | 34 | @Override 35 | public CodeNode execute(Tree node) { 36 | if (node == null) { 37 | return new CodeNode(null); 38 | } 39 | 40 | Tree parent = node.getParent(); 41 | if (parent == null) { 42 | return new CodeNode(null); 43 | } 44 | 45 | Tree nextNode = parent.getChild(node.getChildIndex() + 1); 46 | if (nextNode != null) { 47 | return new CodeNode(nextNode); 48 | } 49 | 50 | // No child found, trace back again 51 | return execute(parent); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/pmd/rules/ConstructorWithoutInheritedStatementRule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2015 Fabricio Colombo 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Lesser General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 3 of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public 16 | * License along with this program; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 18 | */ 19 | package org.sonar.plugins.delphi.pmd.rules; 20 | 21 | import net.sourceforge.pmd.RuleContext; 22 | import org.sonar.plugins.delphi.antlr.DelphiLexer; 23 | import org.sonar.plugins.delphi.antlr.ast.DelphiPMDNode; 24 | 25 | import java.util.ArrayList; 26 | import java.util.List; 27 | 28 | public class ConstructorWithoutInheritedStatementRule extends NoInheritedStatementRule { 29 | 30 | private List knewRecords = new ArrayList<>(); 31 | 32 | @Override 33 | protected void init() { 34 | super.init(); 35 | knewRecords.clear(); 36 | setLookFor("constructor"); 37 | } 38 | 39 | @Override 40 | public void visit(DelphiPMDNode node, RuleContext ctx) { 41 | if (node.getType() == DelphiLexer.TkRecord) { 42 | knewRecords.add(node.getParent().getText()); 43 | } 44 | super.visit(node, ctx); 45 | } 46 | 47 | @Override 48 | protected boolean shouldAddRule(DelphiPMDNode node) { 49 | if (node.getChild(0).getType() == DelphiLexer.TkFunctionName) { 50 | String typeName = node.getChild(0).getChild(0).getText(); 51 | return !knewRecords.contains(typeName); 52 | } 53 | return super.shouldAddRule(node); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/pmd/xml/Property.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.pmd.xml; 24 | 25 | import com.thoughtworks.xstream.annotations.XStreamAlias; 26 | import com.thoughtworks.xstream.annotations.XStreamAsAttribute; 27 | 28 | /** 29 | * Delphi pmd property class 30 | * 31 | * @author SG0214809 32 | * 33 | */ 34 | @XStreamAlias("property") 35 | public class Property { 36 | 37 | @XStreamAsAttribute 38 | private String name; 39 | 40 | private String value; 41 | 42 | /** 43 | * Default ctor 44 | */ 45 | public Property() { 46 | } 47 | 48 | /** 49 | * ctor 50 | * 51 | * @param name property name 52 | * @param value property value 53 | */ 54 | public Property(String name, String value) { 55 | this.name = name; 56 | this.value = value; 57 | } 58 | 59 | /** 60 | * @return property name 61 | */ 62 | public String getName() { 63 | return name; 64 | } 65 | 66 | /** 67 | * @return property value 68 | */ 69 | public String getValue() { 70 | return value; 71 | } 72 | } 73 | -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/grammar/ListUtils.pas: -------------------------------------------------------------------------------- 1 | unit ListUtils; 2 | 3 | interface 4 | 5 | {$I Elotech.inc} 6 | 7 | {$IFDEF Delphi7} 8 | uses Classes; 9 | {$ELSE} 10 | {$ENDIF} 11 | type 12 | TListUtils = class 13 | public 14 | class procedure Normal(); 15 | class procedure AddAll1(AList: TList; const AValue: string; ADelimiter: Char = ';');overload; 16 | class procedure AddAll2(AList: TList; const AValue: string; ADelimiter: Char = ';');overload; 17 | class procedure AddAll3(AList: TList; const AValue: string; AConverter: TFunc; ADelimiter: Char = ';');overload; 18 | end; 19 | 20 | implementation 21 | 22 | { TListUtils } 23 | 24 | class procedure TListUtils.AddAll1(AList: TList; const AValue: string; ADelimiter: Char); 25 | begin 26 | AddAll3(AList, AValue, function (Value: String): Integer 27 | begin 28 | Result := StrToInt(Value); 29 | end, 30 | ADelimiter); 31 | end; 32 | 33 | class procedure TListUtils.AddAll2(AList: TList; const AValue: string; ADelimiter: Char); 34 | begin 35 | AddAll3(AList, AValue, function (Value: String): string 36 | begin 37 | Result := Value; 38 | end, 39 | ADelimiter); 40 | end; 41 | 42 | class procedure TListUtils.AddAll3(AList: TList; const AValue: string; AConverter: TFunc; ADelimiter: Char); 43 | var 44 | vList: TStringList; 45 | vItem: String; 46 | begin 47 | Assert(Assigned(AConverter), 'Converter n�o definido'); 48 | 49 | vList := TStringList.Create; 50 | try 51 | vList.Delimiter := ADelimiter; 52 | vList.DelimitedText := AValue; 53 | 54 | for vItem in vList do 55 | begin 56 | AList.Add(AConverter(vItem)) 57 | end; 58 | finally 59 | vList.Free; 60 | end; 61 | end; 62 | 63 | class procedure TListUtils.Normal; 64 | begin 65 | //Comment 66 | end; 67 | 68 | end. 69 | -------------------------------------------------------------------------------- /src/test/java/org/sonar/plugins/delphi/pmd/TooLongMethodNameRuleTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2015 Fabricio Colombo 4 | * 5 | * This program is free software; you can redistribute it and/or 6 | * modify it under the terms of the GNU Lesser General Public 7 | * License as published by the Free Software Foundation; either 8 | * version 3 of the License, or (at your option) any later version. 9 | * 10 | * This program is distributed in the hope that it will be useful, 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 | * Lesser General Public License for more details. 14 | * 15 | * You should have received a copy of the GNU Lesser General Public 16 | * License along with this program; if not, write to the Free Software 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 18 | */ 19 | package org.sonar.plugins.delphi.pmd; 20 | 21 | import org.junit.Test; 22 | import org.sonar.plugins.delphi.IssueMatchers; 23 | 24 | import static org.hamcrest.MatcherAssert.assertThat; 25 | import static org.hamcrest.Matchers.*; 26 | 27 | public class TooLongMethodNameRuleTest extends BasePmdRuleTest { 28 | 29 | @Test 30 | public void testValidRule() { 31 | DelphiUnitBuilderTest builder = new DelphiUnitBuilderTest(); 32 | builder.appendImpl("function Foo: Integer;"); 33 | builder.appendImpl("begin"); 34 | builder.appendImpl(" Result := 1;"); 35 | builder.appendImpl("end;"); 36 | 37 | analyse(builder); 38 | 39 | assertThat(issues, is(empty())); 40 | } 41 | 42 | /* @Test 43 | public void testTooLongMethod() { 44 | DelphiUnitBuilderTest builder = new DelphiUnitBuilderTest(); 45 | builder.appendImpl("function Foo: Integer;"); 46 | builder.appendImpl("begin"); 47 | for (int i = 1; i <= 101; i++) { 48 | builder.appendImpl(" Result := Result + 1;"); 49 | } 50 | builder.appendImpl("end;"); 51 | 52 | analyse(builder); 53 | 54 | assertThat(issues, hasItem(IssueMatchers.hasRuleKeyAtLine("TooLongMethodRule", builder.getOffSet() + 1))); 55 | }*/ 56 | } 57 | -------------------------------------------------------------------------------- /src/test/resources/org/sonar/plugins/delphi/metrics/ListUtils.pas: -------------------------------------------------------------------------------- 1 | unit ListUtils; 2 | 3 | interface 4 | 5 | {$I Elotech.inc} 6 | 7 | {$IFDEF Delphi7} 8 | uses Classes; 9 | {$ELSE} 10 | {$ENDIF} 11 | type 12 | TListUtils = class 13 | public 14 | class procedure SingleStatement(); 15 | class procedure AddAll1(AList: TList; const AValue: string; ADelimiter: Char = ';');overload; 16 | class procedure AddAll2(AList: TList; const AValue: string; ADelimiter: Char = ';');overload; 17 | class procedure AddAll3(AList: TList; const AValue: string; AConverter: TFunc; ADelimiter: Char = ';');overload; 18 | end; 19 | 20 | implementation 21 | 22 | { TListUtils } 23 | 24 | class procedure TListUtils.AddAll1(AList: TList; const AValue: string; ADelimiter: Char); 25 | begin 26 | AddAll3(AList, AValue, function (Value: String): Integer 27 | begin 28 | Result := StrToInt(Value); 29 | end, 30 | ADelimiter); 31 | end; 32 | 33 | class procedure TListUtils.AddAll2(AList: TList; const AValue: string; ADelimiter: Char); 34 | begin 35 | AddAll3(AList, AValue, function (Value: String): string 36 | begin 37 | Result := Value; 38 | end, 39 | ADelimiter); 40 | end; 41 | 42 | class procedure TListUtils.AddAll3(AList: TList; const AValue: string; AConverter: TFunc; ADelimiter: Char); 43 | var 44 | vList: TStringList; 45 | vItem: String; 46 | begin 47 | Assert(Assigned(AConverter), 'Converter n�o definido'); 48 | 49 | vList := TStringList.Create; 50 | try 51 | vList.Delimiter := ADelimiter; 52 | vList.DelimitedText := AValue; 53 | 54 | for vItem in vList do 55 | begin 56 | AList.Add(AConverter(vItem)) 57 | end; 58 | finally 59 | vList.Free; 60 | end; 61 | end; 62 | 63 | class procedure TListUtils.SingleStatement; 64 | begin 65 | WriteLn('Test'); 66 | end; 67 | 68 | end. 69 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/antlr/ast/NodeName.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.antlr.ast; 24 | 25 | import org.sonar.plugins.delphi.antlr.ast.exceptions.NodeNameForCodeDoesNotExistException; 26 | 27 | public enum NodeName { 28 | UNKNOWN("unknown", ""), 29 | SEMI("semi", ";"), 30 | DOT("dot", "\\."), 31 | COLON("colon", "[:()]"), 32 | GUID_IDENT("guid_ident", "^'\\{.*"), 33 | DASH("dash", ","), 34 | DASH_POINTER("dash_pointer", "\\^"); 35 | 36 | private String name; 37 | private String codeRegExpression; 38 | 39 | NodeName(String name, String regex) { 40 | this.name = name; 41 | this.codeRegExpression = regex; 42 | } 43 | 44 | public boolean matchesCode(String code) { 45 | return code.matches(codeRegExpression); 46 | } 47 | 48 | static NodeName findByCode(String code) { 49 | for (NodeName nodeName : NodeName.values()) { 50 | if (nodeName.matchesCode(code)) { 51 | return nodeName; 52 | } 53 | } 54 | throw new NodeNameForCodeDoesNotExistException(code); 55 | } 56 | 57 | public String getName() { 58 | return name; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/antlr/directives/CompilerDirective.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.antlr.directives; 24 | 25 | /** 26 | * Compiler directive interface 27 | */ 28 | public interface CompilerDirective { 29 | 30 | /** 31 | * example: #include <iostream> will return "include" 32 | * 33 | * @return definition name 34 | */ 35 | String getName(); 36 | 37 | /** 38 | * example: #include <iostream> will return "iostream" 39 | * 40 | * @return definition item 41 | */ 42 | String getItem(); 43 | 44 | /** 45 | * @return first definition char position in whole file 46 | */ 47 | int getFirstCharPosition(); 48 | 49 | /** 50 | * @return last definition char position 51 | */ 52 | int getLastCharPosition(); 53 | 54 | /** 55 | * @return preprocessor definition type 56 | */ 57 | CompilerDirectiveType getType(); 58 | 59 | /** 60 | * Get length of the directive, that is: the span from first and last char 61 | * position 62 | * 63 | * @return directive length 64 | */ 65 | int getLength(); 66 | 67 | } 68 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/core/DelphiLanguage.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.core; 24 | 25 | import org.sonar.api.resources.AbstractLanguage; 26 | 27 | /** 28 | * Class representing Delphi language 29 | */ 30 | public class DelphiLanguage extends AbstractLanguage { 31 | 32 | public static final String KEY = "delph"; 33 | public static DelphiLanguage instance = new DelphiLanguage(); 34 | 35 | public static final String FILE_SOURCE_CODE_SUFFIX = "pas"; 36 | public static final String FILE_PROJECT_SUFFIX = "dpr"; 37 | public static final String FILE_PACKAGE_SUFFIX = "dpk"; 38 | 39 | private static final String LANGUAGE_NAME = "Delphi"; 40 | 41 | private static final String[] FILE_SUFFIXES = {FILE_SOURCE_CODE_SUFFIX, FILE_PROJECT_SUFFIX, FILE_PACKAGE_SUFFIX}; 42 | 43 | /** 44 | * Default ctor 45 | */ 46 | public DelphiLanguage() { 47 | super(KEY, LANGUAGE_NAME); 48 | instance = this; 49 | } 50 | 51 | /** 52 | * @return Delphi source code file suffixes 53 | */ 54 | 55 | @Override 56 | public String[] getFileSuffixes() { 57 | return FILE_SUFFIXES; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/pmd/rules/NoBeginAfterDoRule.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.pmd.rules; 24 | 25 | import net.sourceforge.pmd.RuleContext; 26 | import org.antlr.runtime.tree.Tree; 27 | import org.sonar.plugins.delphi.antlr.analyzer.LexerMetrics; 28 | import org.sonar.plugins.delphi.antlr.ast.DelphiPMDNode; 29 | 30 | /** 31 | * Checks, if we put 'begin' after 'do' statement 32 | */ 33 | public class NoBeginAfterDoRule extends DelphiRule { 34 | 35 | @Override 36 | public void visit(DelphiPMDNode node, RuleContext ctx) { 37 | if (shouldCheck(node)) { 38 | Tree nextNode = node.getParent().getChild(node.getChildIndex() + 1); 39 | 40 | if (isWrongNode(nextNode)) { 41 | addViolation(ctx, node); 42 | } 43 | 44 | } 45 | } 46 | 47 | protected boolean isWrongNode(Tree node) { 48 | return node == null 49 | || (node.getType() != LexerMetrics.BEGIN.toMetrics() && node.getType() != LexerMetrics.WITH.toMetrics()); 50 | } 51 | 52 | protected boolean shouldCheck(Tree node) { 53 | return node != null && node.getType() == LexerMetrics.DO.toMetrics(); 54 | } 55 | 56 | } 57 | -------------------------------------------------------------------------------- /src/test/java/org/sonar/plugins/delphi/utils/ProgressReporterTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.utils; 24 | 25 | import org.junit.Test; 26 | 27 | import static org.junit.Assert.assertEquals; 28 | 29 | public class ProgressReporterTest 30 | { 31 | @Test 32 | public void testDefaultReporter() { 33 | ProgressReporter reporter = new ProgressReporter(); 34 | testReporter(reporter, 5); 35 | } 36 | 37 | @Test 38 | public void testCustomReporter() { 39 | ProgressReporter reporter = new ProgressReporter(7, 10, new ProgressReporterLogger()); 40 | testReporter(reporter, 11); 41 | 42 | reporter = new ProgressReporter(10, 2, new ProgressReporterLogger()); 43 | testReporter(reporter, 3); 44 | 45 | reporter = new ProgressReporter(75, 3, new ProgressReporterLogger()); 46 | testReporter(reporter, 4); 47 | } 48 | 49 | public void testReporter(ProgressReporter reporter, int expectedReports) { 50 | int numReports = 0; 51 | for (int i = 0; i < reporter.getTargetProgress(); i++) { 52 | numReports += reporter.progress(); 53 | } 54 | assertEquals(expectedReports, numReports); 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/antlr/sanitizer/resolvers/SourceFixerResolver.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.antlr.sanitizer.resolvers; 24 | 25 | import org.sonar.plugins.delphi.antlr.sanitizer.SourceResolver; 26 | 27 | /** 28 | * Fixex source file: appends '\n' to the end, adds whitespaces when needed. All 29 | * that in order to ANTLR parser could work correctly. 30 | */ 31 | public class SourceFixerResolver extends SourceResolver { 32 | 33 | @Override 34 | protected void doResolve(SourceResolverResults results) { 35 | results.setFileData(fixSource(results.getFileData())); 36 | } 37 | 38 | /** 39 | * Fixes source 40 | * 41 | * @param fileData file character data to fix 42 | * @return String containing fixed source code 43 | */ 44 | private StringBuilder fixSource(StringBuilder fileData) { 45 | String fixed = fileData.toString(); 46 | // replace ':' with ' :' 47 | fixed = fixed.replaceAll(":", " :"); 48 | // replace '..' with ' .. ' 49 | fixed = fixed.replaceAll("\\.\\.", " .. "); 50 | // adds '\n' before EOF 51 | fixed = fixed.concat("\n"); 52 | return new StringBuilder(fixed); 53 | } 54 | 55 | } 56 | -------------------------------------------------------------------------------- /src/test/java/org/sonar/plugins/delphi/project/DelphiWorkgroupTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.project; 24 | 25 | import org.junit.Before; 26 | import org.junit.Test; 27 | import org.sonar.plugins.delphi.utils.DelphiUtils; 28 | 29 | import java.io.IOException; 30 | 31 | import static org.junit.Assert.assertEquals; 32 | 33 | public class DelphiWorkgroupTest { 34 | 35 | private static String XML_FILE = "/org/sonar/plugins/delphi/SimpleDelphiProject/dproj/workgroup/All.groupproj"; 36 | private DelphiWorkgroup workGroup; 37 | 38 | @Before 39 | public void init() { 40 | workGroup = new DelphiWorkgroup(); 41 | } 42 | 43 | @Test 44 | public void simpleWorkgroupTest() { 45 | assertEquals(0, workGroup.getProjects().size()); 46 | workGroup.addProject(new DelphiProject("Sample")); 47 | assertEquals(1, workGroup.getProjects().size()); 48 | } 49 | 50 | @Test 51 | public void xmlWorkgroupTest() throws IOException { 52 | DelphiWorkgroup workGroup = new DelphiWorkgroup(DelphiUtils.getResource(XML_FILE)); 53 | assertEquals(1, workGroup.getProjects().size()); 54 | assertEquals("Simple Delphi Product", workGroup.getProjects().get(0).getName()); 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/antlr/analyzer/CodeTree.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.antlr.analyzer; 24 | 25 | import org.antlr.runtime.tree.Tree; 26 | import org.sonar.plugins.delphi.antlr.ast.ASTTree; 27 | 28 | /** 29 | * Code analyzed class provides us with information about code being currently 30 | * analysed. 31 | */ 32 | public class CodeTree { 33 | 34 | private CodeNode rootNode; 35 | private CodeNode currentNode; 36 | 37 | /** 38 | * ctor 39 | * 40 | * @param root root element 41 | * @param current first element we will parse 42 | */ 43 | public CodeTree(CodeNode root, CodeNode current) { 44 | this.rootNode = root; 45 | this.currentNode = current; 46 | } 47 | 48 | /** 49 | * @return current node 50 | */ 51 | public CodeNode getCurrentCodeNode() { 52 | return currentNode; 53 | } 54 | 55 | /** 56 | * @return root node 57 | */ 58 | public CodeNode getRootCodeNode() { 59 | return rootNode; 60 | } 61 | 62 | /** 63 | * sets the current node 64 | * 65 | * @param n node 66 | */ 67 | public void setCurrentNode(CodeNode n) { 68 | currentNode = n; 69 | } 70 | 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/antlr/directives/impl/IncludeDirective.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.antlr.directives.impl; 24 | 25 | import org.sonar.plugins.delphi.antlr.directives.CompilerDirectiveType; 26 | 27 | /** 28 | * {$include ...} or {$i ...} directive 29 | */ 30 | public class IncludeDirective extends CommonCompilerDirective { 31 | 32 | private static final String DIRECTIVE_NAME = "include"; 33 | 34 | /** 35 | * ctor 36 | * 37 | * @param item directive item 38 | * @param firstCharPos first character position 39 | * @param lastCharPos last character position 40 | */ 41 | public IncludeDirective(String item, int firstCharPos, int lastCharPos) { 42 | super(DIRECTIVE_NAME, item, firstCharPos, lastCharPos); 43 | } 44 | 45 | @Override 46 | public CompilerDirectiveType getType() { 47 | return CompilerDirectiveType.INCLUDE; 48 | } 49 | 50 | @Override 51 | public String getItem() { 52 | String result = String.copyValueOf(item.toCharArray()); 53 | // get rid of ' ', \ -> / 54 | result = result.replaceAll(" ", "").replaceAll("\\\\", "/"); 55 | // get rid of '../' 56 | result = result.replaceAll("\\.\\./", ""); 57 | return result; 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/antlr/sanitizer/subranges/SubRange.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.antlr.sanitizer.subranges; 24 | 25 | /** 26 | * Sub range interface 27 | */ 28 | public interface SubRange { 29 | 30 | /** 31 | * Checks if value is in range 32 | * 33 | * @param value Value to check 34 | * @return True if value >= begin    value <= end, false otherwise 35 | */ 36 | boolean inRange(int value); 37 | 38 | /** 39 | * Checks if range is in scope of current range 40 | * 41 | * @param range Range to check 42 | * @return True if range includes itself in current range, false otherwise 43 | */ 44 | boolean inRange(SubRange range); 45 | 46 | /** 47 | * Get beginning of the range 48 | * 49 | * @return Beggining of the range 50 | */ 51 | int getBegin(); 52 | 53 | /** 54 | * Get the end of the range 55 | * 56 | * @return End of the range 57 | */ 58 | int getEnd(); 59 | 60 | /** 61 | * sets end 62 | * 63 | * @param value new end value 64 | */ 65 | void setEnd(int value); 66 | 67 | /** 68 | * sets begin 69 | * 70 | * @param value new begin value 71 | */ 72 | void setBegin(int value); 73 | 74 | } 75 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/antlr/ast/DelphiTreeAdaptor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.antlr.ast; 24 | 25 | import org.antlr.runtime.CommonToken; 26 | import org.antlr.runtime.Token; 27 | import org.antlr.runtime.tree.CommonTreeAdaptor; 28 | 29 | /** 30 | * DelphiLanguage Tree Adaptor class, used for creating DelphiPMDNodes in ANTLR 31 | */ 32 | class DelphiTreeAdaptor extends CommonTreeAdaptor { 33 | 34 | private ASTTree astTree; 35 | private int lastLine = 0; 36 | 37 | /** 38 | * Adaptor ctor 39 | * 40 | * @param tree Tree 41 | */ 42 | public DelphiTreeAdaptor(ASTTree tree) { 43 | astTree = tree; 44 | } 45 | 46 | @Override 47 | public Object create(Token payload) { 48 | if (payload != null) { 49 | lastLine = payload.getLine(); 50 | } 51 | return new DelphiPMDNode(payload, astTree); 52 | } 53 | 54 | /** 55 | * When creating imaginary Tokens (such as TkReturnType etc) we need to set 56 | * its parameters manually. 57 | */ 58 | 59 | @Override 60 | public Token createToken(int tokenType, String text) { 61 | CommonToken imaginaryToken = new CommonToken(tokenType, text); 62 | imaginaryToken.setLine(lastLine); 63 | return imaginaryToken; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/metrics/DefaultMetrics.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.metrics; 24 | 25 | import java.util.HashMap; 26 | import java.util.Map; 27 | 28 | /** 29 | * Metric class providing default behavior 30 | */ 31 | public abstract class DefaultMetrics implements MetricsInterface { 32 | 33 | protected Map metrics = new HashMap(); 34 | 35 | /** 36 | * {@inheritDoc} 37 | */ 38 | 39 | @Override 40 | public String[] getMetricKeys() { 41 | return metrics.keySet().toArray(new String[metrics.keySet().size()]); 42 | } 43 | 44 | /** 45 | * {@inheritDoc} 46 | */ 47 | 48 | @Override 49 | public double getMetric(String metric) { 50 | if (!metrics.containsKey(metric)) { 51 | throw new IllegalStateException("No metric (" + metric + ") for " + this); 52 | } 53 | return metrics.get(metric); 54 | } 55 | 56 | /** 57 | * {@inheritDoc} 58 | */ 59 | public void setMetric(String metric, double value) { 60 | metrics.put(metric, value); 61 | } 62 | 63 | protected void clearMetrics() { 64 | metrics.clear(); 65 | } 66 | 67 | @Override 68 | public boolean hasMetric(String metric) { 69 | return metrics.containsKey(metric); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/antlr/analyzer/impl/VisibilityAnalyzer.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.antlr.analyzer.impl; 24 | 25 | import org.antlr.runtime.tree.Tree; 26 | import org.sonar.plugins.delphi.antlr.DelphiParser; 27 | import org.sonar.plugins.delphi.antlr.analyzer.CodeAnalysisResults; 28 | import org.sonar.plugins.delphi.antlr.analyzer.CodeAnalyzer; 29 | import org.sonar.plugins.delphi.antlr.analyzer.CodeTree; 30 | import org.sonar.plugins.delphi.antlr.analyzer.LexerMetrics; 31 | 32 | /** 33 | * Analyzes current visibility scope and changes it 34 | */ 35 | public class VisibilityAnalyzer extends CodeAnalyzer { 36 | 37 | @Override 38 | protected void doAnalyze(CodeTree codeTree, CodeAnalysisResults results) { 39 | Tree currentNode = codeTree.getCurrentCodeNode().getNode(); 40 | results.setParseVisibility(LexerMetrics.getLexerMetricsForType(currentNode.getType())); 41 | } 42 | 43 | @Override 44 | public boolean canAnalyze(CodeTree codeTree) { 45 | Tree currentNode = codeTree.getCurrentCodeNode().getNode(); 46 | int type = currentNode.getType(); 47 | return type == DelphiParser.PUBLIC || type == DelphiParser.PRIVATE || type == DelphiParser.PROTECTED 48 | || type == DelphiParser.PUBLISHED; 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/test/java/org/sonar/plugins/delphi/antlr/analyzer/impl/operations/TraceBackNodeOperationTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.antlr.analyzer.impl.operations; 24 | 25 | import org.junit.Before; 26 | import org.junit.Test; 27 | 28 | import static org.junit.Assert.*; 29 | 30 | public class TraceBackNodeOperationTest extends OperationsTestsCommon { 31 | 32 | public TraceBackNodeOperationTest() { 33 | super(new TraceBackNodeOperation()); 34 | } 35 | 36 | @Override 37 | @Before 38 | public void init() { 39 | super.init(); 40 | } 41 | 42 | @Test 43 | public void traceback() { 44 | assertFalse(operation.execute(parent).isValid()); 45 | for (int i = 0; i < LAYER_NODES - 1; ++i) { 46 | 47 | assertNotNull(operation.execute(parent.getChild(i))); 48 | assertEquals(i + 2, operation.execute(parent.getChild(i)).getNode().getType()); 49 | 50 | assertNotNull(operation.execute(parent.getChild(i).getChild(0))); 51 | assertEquals(i + 2, operation.execute(parent.getChild(i).getChild(0)).getNode().getType()); 52 | } 53 | 54 | assertFalse(operation.execute(parent.getChild(LAYER_NODES - 1).getChild(0)).isValid()); 55 | assertFalse(operation.execute(parent.getChild(LAYER_NODES - 1)).isValid()); 56 | } 57 | 58 | } 59 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/squid/text/delphi/LiteralValueHandler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.squid.text.delphi; 24 | 25 | public class LiteralValueHandler extends LineContextHandler { 26 | 27 | private final char delimiter; 28 | 29 | public LiteralValueHandler(char delimiter) { 30 | this.delimiter = delimiter; 31 | } 32 | 33 | @Override 34 | boolean matchToEnd(Line line, StringBuilder pendingLine) { 35 | return matchEndOfString(pendingLine, delimiter) && evenNumberOfBackSlashBeforeDelimiter(pendingLine); 36 | } 37 | 38 | private boolean evenNumberOfBackSlashBeforeDelimiter(StringBuilder pendingLine) { 39 | int numberOfBackSlashChar = 0; 40 | for (int index = pendingLine.length() - 2; index >= 0; index--) { 41 | if (pendingLine.charAt(index) == '\\') { 42 | numberOfBackSlashChar++; 43 | } else { 44 | break; 45 | } 46 | } 47 | return numberOfBackSlashChar % 2 == 0; 48 | } 49 | 50 | @Override 51 | boolean matchToBegin(Line line, StringBuilder pendingLine) { 52 | return matchEndOfString(pendingLine, delimiter); 53 | } 54 | 55 | @Override 56 | boolean matchWithEndOfLine(Line line, StringBuilder pendingLine) { 57 | // see http://jira.codehaus.org/browse/SONAR-1555 58 | return true; 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /src/main/java/org/sonar/plugins/delphi/pmd/rules/VariableCounter.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.pmd.rules; 24 | 25 | import net.sourceforge.pmd.RuleContext; 26 | import org.antlr.runtime.tree.Tree; 27 | import org.sonar.plugins.delphi.antlr.DelphiLexer; 28 | import org.sonar.plugins.delphi.antlr.ast.DelphiPMDNode; 29 | 30 | /** 31 | * Rule that is checking how many arguments a function has - if too many, it 32 | * triggers a violation 33 | */ 34 | public class VariableCounter extends DelphiRule { 35 | 36 | @Override 37 | public void visit(DelphiPMDNode node, RuleContext ctx) { 38 | // if function arguments node 39 | if (node.getText().equals(getStringProperty(START))) { 40 | int count = 0; 41 | 42 | // count num of arguments 43 | for (int i = 0; i < node.getChildCount(); ++i) { 44 | Tree child = node.getChild(i); 45 | if (child.getType() == DelphiLexer.TkVariableIdents) { 46 | count += child.getChildCount(); 47 | } 48 | } 49 | 50 | int limit = getIntProperty(LIMIT); 51 | if (count > limit) { 52 | String msg = "Too many " + getStringProperty(LOOK_FOR) + ": " + count + " (max " 53 | + limit + ")"; 54 | addViolation(ctx, node, msg); 55 | } 56 | } 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /src/test/java/org/sonar/plugins/delphi/antlr/sanitizer/resolvers/IncludeResolverTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Sonar Delphi Plugin 3 | * Copyright (C) 2011 Sabre Airline Solutions and Fabricio Colombo 4 | * Author(s): 5 | * Przemyslaw Kociolek (przemyslaw.kociolek@sabre.com) 6 | * Michal Wojcik (michal.wojcik@sabre.com) 7 | * Fabricio Colombo (fabricio.colombo.mva@gmail.com) 8 | * 9 | * This program is free software; you can redistribute it and/or 10 | * modify it under the terms of the GNU Lesser General Public 11 | * License as published by the Free Software Foundation; either 12 | * version 3 of the License, or (at your option) any later version. 13 | * 14 | * This program is distributed in the hope that it will be useful, 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 17 | * Lesser General Public License for more details. 18 | * 19 | * You should have received a copy of the GNU Lesser General Public 20 | * License along with this program; if not, write to the Free Software 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 22 | */ 23 | package org.sonar.plugins.delphi.antlr.sanitizer.resolvers; 24 | 25 | import org.junit.Before; 26 | import org.junit.BeforeClass; 27 | import org.junit.Test; 28 | import org.sonar.plugins.delphi.debug.FileTestsCommon; 29 | 30 | import java.io.File; 31 | import java.io.IOException; 32 | import java.util.ArrayList; 33 | 34 | import static org.junit.Assert.assertEquals; 35 | 36 | public class IncludeResolverTest extends FileTestsCommon { 37 | 38 | private static final String FILE_NAME = "/org/sonar/plugins/delphi/grammar/GrammarTest.pas"; 39 | private IncludeResolver resolver; 40 | 41 | @BeforeClass 42 | public static void init() throws IOException { 43 | loadFile(FILE_NAME); 44 | } 45 | 46 | @Before 47 | public void setup() { 48 | resolver = new IncludeResolver(true, new ArrayList()); 49 | } 50 | 51 | @Test 52 | public void resolveIncludesTest() { 53 | SourceResolverResults results = new SourceResolverResults(testFile.getAbsolutePath(), testFileString); 54 | ExcludeResolver excludeResolver = new ExcludeResolver(); 55 | excludeResolver.resolve(results); 56 | 57 | resolver.resolve(results); 58 | assertEquals(4, resolver.getIncludedFilesPath().size()); 59 | } 60 | 61 | } 62 | --------------------------------------------------------------------------------