source: trunk/t3q/src/org/etsi/t3q/visitor/QualityCheckerExtras.java @ 7

Last change on this file since 7 was 7, checked in by phdmakk, 14 years ago
  • Property svn:mime-type set to text/plain
File size: 9.5 KB
Line 
1package org.etsi.t3q.visitor;
2
3import java.util.ArrayList;
4import java.util.Iterator;
5import java.util.List;
6import java.util.Map;
7import java.util.Map.Entry;
8import java.util.regex.Matcher;
9import java.util.regex.Pattern;
10
11import org.etsi.common.MiscTools;
12import org.etsi.common.logging.LoggingInterface.MessageClass;
13import org.etsi.t3q.T3Q;
14
15import de.ugoe.cs.swe.trex.core.analyzer.astutil.ReferenceFinder;
16import de.ugoe.cs.swe.trex.core.analyzer.astutil.ReferenceWithContext;
17import de.ugoe.cs.swe.trex.core.analyzer.rfparser.LocationAST;
18import de.ugoe.cs.swe.trex.core.analyzer.rfparser.TTCN3ParserTokenTypes;
19import de.ugoe.cs.swe.trex.core.analyzer.rfparser.symboltable.Symbol;
20
21public class QualityCheckerExtras extends QualityChecker {
22
23        public QualityCheckerExtras(T3QVisitor visitor) {
24                super(visitor);
25                // TODO Auto-generated constructor stub
26        }
27
28
29        public void checkZeroReferencedTestCases(LocationAST node) {
30                if (node.getFirstChild().getType() != TTCN3ParserTokenTypes.TestcaseDef){
31                        return;
32                }
33                ArrayList<LocationAST> identifiersList = LocationAST.getModuleDefinitionIdentifiersList(node);
34                LocationAST identifierNode = null;
35                String identifierText = null;
36                for (int i = 0; i < identifiersList.size(); i++){
37                        identifierNode = identifiersList.get(i);
38                        identifierText = identifierNode.getFirstChild().getText();
39       
40                        Symbol s = identifierNode.getFirstChild().getSymbol();
41                        //TODO: add safety check here
42                        ReferenceFinder referenceFinder = new ReferenceFinder();
43                        if (referenceFinder.countReferences(s) == 0){
44                                //TODO: add stats counters
45                                //TODO: add type information
46                                //TODO: groups shall probably be excluded
47                                this.getLoggingInterface().logWarning(identifierNode.getLine(),
48                                                identifierNode.getEndLine(),
49                                                MessageClass.LOGGING,
50                                                "Testcase definition for \""+identifierText+"\" is never used!",
51                                                                MiscTools.getMethodName());
52                        }
53                }
54       
55        }
56
57        public void checkSetVerdictPrecededByLog(LocationAST node) {
58                        //TODO: duplicated and combined constraints (presence and content according to context)
59                        // Fixed: currently based on SetLocalVerdict node,
60                        // consider starting at the fail or inconc node instead, checking
61                        // whether it is
62                        // within a set verdict context and proceed from there
63                        // fix: it is specific to the set verdict and not to inconc or fail
64                        // alone
65                        boolean problemOccured = false;
66                        String warning = "";
67                        if (node.getFirstChild().getType() == TTCN3ParserTokenTypes.Fail){
68                                LocationAST functionStatementOrDefNode = LocationAST.resolveParentsUntilType(node, TTCN3ParserTokenTypes.FunctionStatement);
69                                LocationAST parentNode = functionStatementOrDefNode.getParent();
70                                LocationAST siblingNode = parentNode.getFirstChild();
71                                LocationAST prevSiblingNode = null;
72                                do {
73                                        if (siblingNode.getType()!=TTCN3ParserTokenTypes.SemiColon){
74                                                prevSiblingNode = siblingNode;
75                                        }
76                                        siblingNode = siblingNode.getNextSibling();
77                                } while ((siblingNode != functionStatementOrDefNode) && (siblingNode !=null));
78       
79                                if (prevSiblingNode!=null && prevSiblingNode.getType()==TTCN3ParserTokenTypes.FunctionStatement){
80                                        if (prevSiblingNode.getNthChild(3).getType()!=TTCN3ParserTokenTypes.LogStatement){
81                                                problemOccured = true;
82                                                warning = "Failing verdict not preceded by a log statement";
83                                        } else {
84                                                //check log statement format according to context
85                                                LocationAST moduleDefinitionNode = LocationAST.resolveParentsUntilType(node, TTCN3ParserTokenTypes.ModuleDefinition);
86                                                if (moduleDefinitionNode.getFirstChild().getType()==TTCN3ParserTokenTypes.AltstepDef){
87                                                        LocationAST altstepDefIdentifierNode = moduleDefinitionNode.getNthChild(3);
88                                               
89                                                        //CHECK REFERENCING CONTEXTS
90                                                        int activatedAsDefault = 0;
91                                                        boolean mixedContext = false;
92                                                        Symbol altstepDefSymbol = altstepDefIdentifierNode.getSymbol();
93       
94                                                        ReferenceFinder referenceFinder = new ReferenceFinder();
95                                                        Map<String, List<ReferenceWithContext>> referenceMap = referenceFinder
96                                                                        .findReferences(altstepDefSymbol);
97                                                        if (referenceMap.isEmpty()) {
98                                                                activatedAsDefault = -1;
99                                                        } else {
100                                                                Iterator<Entry<String, List<ReferenceWithContext>>> referenceMapIterator = referenceMap
101                                                                                .entrySet().iterator();
102       
103                                                                while (referenceMapIterator.hasNext()) {
104                                                                        Entry<String, List<ReferenceWithContext>> referenceEntry = referenceMapIterator
105                                                                                        .next();
106                                                                        Iterator<ReferenceWithContext> referenceIterator = referenceEntry
107                                                                                        .getValue().iterator();
108                                                                        while (referenceIterator.hasNext()) {
109                                                                                ReferenceWithContext referenceWithContext = referenceIterator
110                                                                                                .next();
111                                                                                LocationAST referenceNode = referenceWithContext
112                                                                                                .getReferenceNode();
113                                                                                //HERE
114                                                                                if (referenceNode.getNthParent(3).getType() != TTCN3ParserTokenTypes.ActivateOp) {
115                                                                                        if (activatedAsDefault==1){
116                                                                                                mixedContext = true;
117                                                                                        }
118                                                                                        activatedAsDefault = -1;
119                                                                                } else {
120                                                                                        if (activatedAsDefault==-1){
121                                                                                                mixedContext = true;
122                                                                                        }
123                                                                                        activatedAsDefault = 1;
124                                                                                }
125                                                                        }
126                                                                }
127                                                        }
128                                                        if (mixedContext){
129                                                                this.getLoggingInterface().logInformation(node.getLine(),
130                                                                                node.getEndLine(),
131                                                                                MessageClass.LOGGING,
132                                                                                "Alstep \""+altstepDefIdentifierNode.getText()+"\" is used in mixed contexts (both activated as default and not), therefore the results for the correct log statement format may be unreliable!",
133                                                                                                MiscTools.getMethodName());
134                                                        }
135                                                        if (activatedAsDefault > 0){
136                                                                this.checkLogStatementFormat(prevSiblingNode.getNthChild(3),false);
137                                                        } else {
138                                                                this.checkLogStatementFormat(prevSiblingNode.getNthChild(3),true);
139                                                        }
140                                                }
141                                        }
142                                } else {
143                                        problemOccured = true;
144                                        warning = "Failing verdict not preceded by a log statement!";
145                                }
146                        }
147                        if (node.getFirstChild().getType() == TTCN3ParserTokenTypes.None){
148                                //TODO: DUPLICATE BLOCK
149                                LocationAST functionStatementOrDefNode = LocationAST.resolveParentsUntilType(node, TTCN3ParserTokenTypes.FunctionStatement);
150                                LocationAST parentNode = functionStatementOrDefNode.getParent();
151                                LocationAST siblingNode = parentNode.getFirstChild();
152                                LocationAST prevSiblingNode = null;
153                                do {
154                                        if (siblingNode.getType()!=TTCN3ParserTokenTypes.SemiColon){
155                                                prevSiblingNode = siblingNode;
156                                        }
157                                        siblingNode = siblingNode.getNextSibling();
158                                } while ((siblingNode != functionStatementOrDefNode) && (siblingNode !=null));
159       
160                                if (prevSiblingNode!=null && prevSiblingNode.getType()==TTCN3ParserTokenTypes.FunctionStatement){
161                                        if (prevSiblingNode.getNthChild(3).getType()!=TTCN3ParserTokenTypes.LogStatement){
162                                                problemOccured = true;
163                                                warning = "Verdict \"none\" not preceded by a log statement!";
164                                        } else {
165                                                this.checkLogStatementFormat(prevSiblingNode.getNthChild(3),false);     
166                                        }
167                                }
168                        }
169                       
170                       
171                        if (problemOccured) {
172                                this.getLoggingInterface().logWarning(node.getLine(),
173                                                node.getEndLine(),
174                                                MessageClass.LOGGING,
175                                                warning,
176                                                                MiscTools.getMethodName());
177                        }
178                       
179        //              boolean problemOccured = false;
180        //
181        //              if (ASTUtil.findChild(node, TTCN3ParserTokenTypes.Fail) != null
182        //                              || ASTUtil.findChild(node, TTCN3ParserTokenTypes.Inconc) != null) {
183        //
184        //                      LocationAST parentNode = LocationAST.resolveParentsUntilType(node,
185        //                                      TTCN3ParserTokenTypes.FunctionStatementOrDef);
186        //
187        //                      int functionStatementOrDefsVisited = visitor
188        //                                      .getFunctionStatementOrDefNodes().size();
189        //                      if (parentNode == visitor.getFunctionStatementOrDefNodes().get(
190        //                                      functionStatementOrDefsVisited - 1)) {
191        //                              int i = functionStatementOrDefsVisited - 2;
192        //                              if (i > 0) {
193        //                                      LocationAST next = null;
194        //                                      // skip semicolons
195        //                                      next = visitor.getFunctionStatementOrDefNodes().get(i)
196        //                                                      .getNextSibling();
197        //                                      while ((next != null)
198        //                                                      && (next.getType() == TTCN3ParserTokenTypes.SemiColon)) {
199        //                                              next = next.getNextSibling();
200        //                                      }
201        //
202        //                                      if (next == parentNode) {
203        //                                              LocationAST resultNode = (LocationAST) ASTUtil
204        //                                                              .findChild(visitor
205        //                                                                              .getFunctionStatementOrDefNodes()
206        //                                                                              .get(i),
207        //                                                                              TTCN3ParserTokenTypes.LogStatement);
208        //                                              if (resultNode == null) {
209        //                                                      problemOccured = true;
210        //                                              }
211        //                                      } else {
212        //                                              problemOccured = true;
213        //                                      }
214        //                              } else {
215        //                                      problemOccured = true;
216        //                              }
217        //                      }
218        //              }
219                }
220
221        public void checkLogStatementFormat(LocationAST node, boolean withVerificationPoint) {
222                boolean problemOccured = false;
223                String warning = "";
224                String logItemText = "";
225               
226                LocationAST logItem = node.getFirstChild();
227               
228                do {
229                        if (logItem.getFirstChild().getType()!=TTCN3ParserTokenTypes.TemplateInstance){
230                                logItemText += logItem.getFirstChild().getText();
231                        } else {
232                                //TODO: attempt to perform resolution
233                                logItemText += "<<VAR>>";
234                        }
235                        logItem = logItem.getNextSibling();
236                } while (logItem!=null && logItem.getType()==TTCN3ParserTokenTypes.LogItem);
237               
238       
239                Pattern logPattern = Pattern.compile(T3Q.activeProfile
240                                .getLogFormatRegExp());
241                Matcher logMatcher = logPattern.matcher(logItemText);
242                if (!logMatcher.matches()) {
243                        problemOccured = true;
244                        warning = "Invalid log format (\"" + logItemText + "\")!";
245                } else if (logMatcher.group(1).equals("") && withVerificationPoint) {
246                        problemOccured = true;
247                        warning = "Invalid log format (\"" + logItemText + "\") - missing verification point!";
248                }
249                if (problemOccured) {
250                        this.getLoggingInterface().logWarning(node.getLine(),
251                                        node.getEndLine(),
252                                        MessageClass.LOGGING,
253                                        warning,
254                                                        MiscTools.getMethodName());
255                }
256       
257        }
258
259}
Note: See TracBrowser for help on using the repository browser.