Changeset 196
- Timestamp:
- 10/23/09 15:15:59 (5 weeks ago)
- Location:
- trunk/cpp_analysis/src/xtc/lang/c4
- Files:
-
- 1 added
- 8 modified
-
C4Core.rats (modified) (4 diffs)
-
C4Extractor.java (modified) (1 diff)
-
C4MacrosManager.java (modified) (9 diffs)
-
C4ParseTreePrinter.java (modified) (2 diffs)
-
C4ParserState.java (modified) (26 diffs)
-
C4SignatureUtils.java (added)
-
CPPAnalyzer.java (modified) (27 diffs)
-
DBHandler.java (modified) (4 diffs)
-
TreeLogger.java (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/cpp_analysis/src/xtc/lang/c4/C4Core.rats
r193 r196 53 53 54 54 generic Declaration := 55 <MacroBased2> ("__extension__":Keyword)? DeclarationSpecifiersSimpleDeclarator id:Identifier &{yyState.isFunctionMacro(toText(id))}56 void:"(":Symbol FunctionMacroCallParametersList? void:")":Symbol55 <MacroBased2> k:("__extension__":Keyword)? d:DeclarationSpecifiers sId:SimpleDeclarator id:Identifier &{yyState.isFunctionMacro(toText(id))} 56 void:"(":Symbol f:FunctionMacroCallParametersList? void:")":Symbol 57 57 l:InitializedDeclaratorList? &{ yyState.isValid(l) } void:";":Symbol 58 @MacroBased2 59 / <MacroBased3> ("__extension__":Keyword)? DeclarationSpecifiers id:Identifier &{yyState.isFunctionMacro(toText(id))} 60 void:"(":Symbol FunctionMacroCallParametersList? void:")":Symbol l:InitializedDeclaratorList? 58 { yyValue = GNode.create("MacroBased2",k, d, sId, id, f, yyState.getMacro(toText(id))); } 59 //@MacroBased2 60 / <MacroBased3> mb3_k:("__extension__":Keyword)? mb3_ds:DeclarationSpecifiers id:Identifier &{yyState.isFunctionMacro(toText(id))} 61 void:"(":Symbol f:FunctionMacroCallParametersList? void:")":Symbol l:InitializedDeclaratorList? 61 62 &{ yyState.isValid(l) } void:";":Symbol 62 @MacroBased3 63 / <MacroBased4> ("__extension__":Keyword)? id:Identifier &{yyState.isFunctionMacro(toText(id))} 64 void:"(":Symbol FunctionMacroCallParametersList? void:")":Symbol DeclarationSpecifiers? 65 (void:"=":Symbol Initializer)? void:";":Symbol 66 @MacroBased4 67 / <MacroBased5> ("__extension__":Keyword)? DeclarationSpecifier id:Identifier &{yyState.isFunctionMacro(toText(id))} 68 void:"(":Symbol FunctionMacroCallParametersList? void:")":Symbol void:"[":Symbol as:ArraySizeExpression? void:"]":Symbol 69 DeclarationSpecifiers? (void:"=":Symbol Initializer)? void:";":Symbol 70 @MacroBased5 71 / <MacroBased6> ("__extension__":Keyword)? DeclarationSpecifier id:Identifier &{yyState.isFunctionMacro(toText(id))} 72 void:"(":Symbol FunctionMacroCallParametersList? void:")":Symbol DeclarationSpecifiers? 73 (void:"=":Symbol Initializer)? void:";":Symbol 74 @MacroBased6 75 / <MacroBased7> ("__extension__":Keyword)? DeclarationSpecifier DeclarationSpecifier id:Identifier &{yyState.isFunctionMacro(toText(id))} 76 void:"(":Symbol FunctionMacroCallParametersList? void:")":Symbol 77 DeclarationSpecifiers? (void:"=":Symbol Initializer)? void:";":Symbol 78 @MacroBased7 79 / <MacroBased8> ("__extension__":Keyword)? DeclarationSpecifier DeclarationSpecifier id:Identifier &{yyState.isFunctionMacro(toText(id))} 80 DeclarationSpecifiers? (void:"=":Symbol Initializer)? void:";":Symbol 81 @MacroBased8 63 { yyValue = yyState.annotate(GNode.create("MacroBased3", mb3_k, mb3_ds, id, f, l, yyState.getMacro(toText(id)))); } 64 //@MacroBased3 65 / <MacroBased4> k:("__extension__":Keyword)? id:Identifier &{yyState.isFunctionMacro(toText(id))} 66 void:"(":Symbol f:FunctionMacroCallParametersList? void:")":Symbol d:DeclarationSpecifiers? 67 init:(void:"=":Symbol Initializer)? void:";":Symbol 68 { yyValue = yyState.annotate(GNode.create("MacroBased4",k, id, f, d, init, yyState.getMacro(toText(id)))); } 69 //@MacroBased4 {System.out.println("TEST");} 70 / <MacroBased5> mb5_k:("__extension__":Keyword)? mb5_ds:DeclarationSpecifier id:Identifier &{yyState.isFunctionMacro(toText(id))} 71 void:"(":Symbol f:FunctionMacroCallParametersList? void:")":Symbol void:"[":Symbol as:ArraySizeExpression? void:"]":Symbol 72 mb5_ds2:DeclarationSpecifiers? init:(void:"=":Symbol Initializer)? void:";":Symbol 73 { yyValue = yyState.annotate(GNode.create("MacroBased5", mb5_ds, id, f, as, mb5_ds2, init, yyState.getMacro(toText(id)))); } 74 //@MacroBased5 75 / <MacroBased6> mb6_k:("__extension__":Keyword)? d:DeclarationSpecifier id:Identifier &{yyState.isFunctionMacro(toText(id))} 76 void:"(":Symbol f:FunctionMacroCallParametersList? void:")":Symbol ds:DeclarationSpecifiers? 77 init:(void:"=":Symbol Initializer)? void:";":Symbol 78 { yyValue = GNode.create("MacroBased6",mb6_k, d, id, f, ds, init, yyState.getMacro(toText(id))); } 79 //@MacroBased6 80 / <MacroBased7> mb7_k:("__extension__":Keyword)? ds1:DeclarationSpecifier ds2:DeclarationSpecifier id:Identifier &{yyState.isFunctionMacro(toText(id))} 81 void:"(":Symbol f:FunctionMacroCallParametersList? void:")":Symbol 82 ds3:DeclarationSpecifiers? init:(void:"=":Symbol Initializer)? void:";":Symbol 83 { yyValue = yyState.annotate(GNode.create("MacroBased7",mb7_k, ds1, ds2, id, f, ds3, init, yyState.getMacro(toText(id)))); } 84 //@MacroBased7 85 / <MacroBased8> mb8_k:("__extension__":Keyword)? mb8_ds1:DeclarationSpecifier mb8_ds2:DeclarationSpecifier id:Identifier &{yyState.isFunctionMacro(toText(id))} 86 ds3:DeclarationSpecifiers? init:(void:"=":Symbol Initializer)? void:";":Symbol 87 { yyValue = yyState.annotate(GNode.create("MacroBased8",mb8_k, mb8_ds1, mb8_ds2, id, ds3, init, yyState.getMacro(toText(id)))); } 88 //@MacroBased8 82 89 / <MacroBased9> ("__extension__":Keyword)? DeclarationSpecifier MacroFunctionExpression void:";":Symbol 83 90 @MacroBased9 84 / <MacroBased10> ("__extension__":Keyword)? DeclarationSpecifier void:"*":Symbol id:Identifier &{yyState.isFunctionMacro(toText(id))} 85 void:"(":Symbol FunctionMacroCallParametersList? void:")":Symbol DeclarationSpecifiers? (void:"=":Symbol Initializer)? void:";":Symbol 86 @MacroBased10 91 / <MacroBased10> k:("__extension__":Keyword)? ds1:DeclarationSpecifier void:"*":Symbol id:Identifier &{yyState.isFunctionMacro(toText(id))} 92 void:"(":Symbol f:FunctionMacroCallParametersList? void:")":Symbol ds2:DeclarationSpecifiers? init:(void:"=":Symbol Initializer)? void:";":Symbol 93 { yyValue = yyState.annotate(GNode.create("MacroBased10",k, ds1, id, f, ds2, init, yyState.getMacro(toText(id)))); } 94 //@MacroBased10 87 95 / <RegularWithIfSection> ("__extension__":Keyword)? DeclarationSpecifiers AttributeSpecifierList? 88 96 Declarator SimpleAssemblyExpression? AttributeSpecifierList? void:"=":Symbol IfSection … … 384 392 generic FunctionMacroCallStatement = 385 393 id:Identifier &{yyState.isFunctionMacro(toText(id))} void:"(":Symbol f:FunctionMacroCallParametersList? 386 void:")":Symbol CompoundStatement? 394 void:")":Symbol c:CompoundStatement? 395 { yyValue = GNode.create("FunctionMacroCallStatement", id, f, c, yyState.getMacro(toText(id))); } 387 396 ; 388 397 … … 410 419 411 420 generic MacroFunctionExpression = 412 id:Identifier &{yyState.isFunctionMacro(toText(id))} void:"(":Symbol FunctionMacroCallParametersList?413 void:")":Symbol 421 id:Identifier &{yyState.isFunctionMacro(toText(id))} void:"(":Symbol f:FunctionMacroCallParametersList? 422 void:")":Symbol { yyValue = GNode.create("MacroFunctionExpression", id, f, yyState.getMacro(toText(id))); } 414 423 ; 415 424 … … 962 971 { 963 972 Node tmpExp = exp.strip(); 964 if (tmpExp.getName() == "IfGroupAdditiveExpression"||965 tmpExp.getName() == "IfGroupMultiplicativeExpression"||966 tmpExp.getName() == "IfGroupBinaryExpression"||967 tmpExp.getName() == "IntegerConstant"||968 tmpExp.getName() == "FunctionMacroCallExpression") {973 if (tmpExp.getName().equals("IfGroupAdditiveExpression") || 974 tmpExp.getName().equals("IfGroupMultiplicativeExpression") || 975 tmpExp.getName().equals("IfGroupBinaryExpression") || 976 tmpExp.getName().equals("IntegerConstant") || 977 tmpExp.getName().equals("FunctionMacroCallExpression") ) { 969 978 yyState.compute(tmpExp); 970 979 yyState.setDefined(yyState.isTrue(yyState.getTmpCondValue())); -
trunk/cpp_analysis/src/xtc/lang/c4/C4Extractor.java
r193 r196 30 30 31 31 32 /** The Debug extraction Flag **/ 33 private boolean DBG_EXTRACT = false; 34 /** The feature to create **/ 35 private String featureName; 36 37 /** The Configuration option to extract **/ 38 private String configOption; 39 /** The list of nodes marked as to be extracted by the CPP Analyzer **/ 40 private ArrayList<C4Extractable> toExtract; 41 42 /** The original Tree before any extraction **/ 43 private Node tree; 44 /** The new Tree with everything extracted **/ 45 private Node newTree; 46 /** The name of the top level file to be extracted **/ 47 /** We do not extract anything from the included header files **/ 48 private String topLevel; 49 50 /** The collection of functionDefinitions extracted from the analysis 51 */ 52 private Map<String, GNode> functionDefinitions; 53 54 /** The collection of functionDefinitions after all the modification 55 * populated during the extraction process 56 */ 57 private Map<String, GNode> modifiedFunctionDefinitions; 58 59 private Node father; 60 61 public C4Extractor(String featureName, String optExtract, ArrayList<C4Extractable> toExtract, Node unit, String fileName, Map<String, GNode> functionDefinitions ) { 62 this.featureName = featureName; 63 configOption = optExtract; 64 this.toExtract = toExtract; 65 tree = unit; 66 topLevel = fileName; 67 this.functionDefinitions = functionDefinitions; 68 modifiedFunctionDefinitions = new HashMap<String, GNode> (); 69 } 70 71 72 static Node build(Formatting n, HashMap<Integer, Object> newChildren) { 73 Formatting result = null ; 74 try { 75 if(n.size() != newChildren.size()) { 76 return GNode.create("toPurge", n); 77 } 78 if(n.getName().contains("After1")) { 79 result = Formatting.after1((Node) newChildren.get(new Integer(0)), newChildren.get(new Integer(1))); 80 } else if(n.getName().contains("Before1")) { 81 result = Formatting.before1(newChildren.get(new Integer(0)), (Node) newChildren.get(new Integer(1))); 82 } else if(n.getName().contains("Round1")) { 83 result = Formatting.round1(newChildren.get(new Integer(0)), (Node) newChildren.get(new Integer(1)), newChildren.get(new Integer(2))); 84 } else if(n.getName().contains("RoundN")) { 85 result = Formatting.variable(); 86 87 for(int index = 0; index < newChildren.size(); index++) { 88 result.add(index, newChildren.get(new Integer(index))); 89 } 90 91 } 92 else { 93 result = null; 94 } 95 } catch (Exception ex) { 96 System.out.println("\n\n######EXCEPTION \n\n" + ex.getMessage()); 97 System.out.println("FORMATTING " + n ); 98 System.out.println("NEW CHILDREN " + newChildren); 99 100 System.exit(-1); 101 result = null; 102 } 103 104 return result; 105 } 106 107 private Node extractVisitor(Node n) { 108 //Node modified = GNode.create((GNode)n); 109 110 //When we remove some children, we have to change the index. 111 //the modifiedIndex is aware of the updated size. 112 int modifiedIndex = 0; 113 ArrayList<Integer> toRemove = new ArrayList<Integer>(); 114 HashMap<Integer, Object> newChildren = new HashMap<Integer, Object>(); 115 String name = n.getName(); 116 117 if(0 == n.size()) { 118 if(DBG_EXTRACT) 119 System.out.println("SIZE ZERO... We return the same node " + n); 120 return n; 121 } 122 123 for(int i = 0; i< n.size(); i++) { 124 if(n.get(i) instanceof Node) { 125 Integer index = new Integer(i); 126 Node child = (Node) n.get(i); 127 128 if(isToBeExtracted(child)) { 129 if(DBG_EXTRACT) 130 System.out.println("We are going to remove child number : " + index); 131 132 father = n; 133 toRemove.add(index); 134 135 } else { 136 137 Node dispatched = (Node) dispatch(child); 138 139 if(DBG_EXTRACT) 140 System.out.println("DISPATCHED " + dispatched + "\nCHILD " + child); 141 142 if(dispatched != null) { 143 144 if(!dispatched.getName().equals("toPurge")) { 145 newChildren.put(new Integer(modifiedIndex), dispatched); 146 modifiedIndex++; 147 } else { 148 if(DBG_EXTRACT) 149 System.out.println("DISPATCHED " + dispatched); 150 } 151 } else { 152 if(DBG_EXTRACT) 153 System.out.println("========= DISPATCHED IS NULL ===========>>>>>> " + dispatched); 154 } 155 156 } 157 158 159 } else if(n.get(i) instanceof String) { 160 newChildren.put(new Integer(modifiedIndex), n.getString(i)); 161 162 modifiedIndex++; 163 164 } else if(null == n.get(i)){ 165 newChildren.put(new Integer(modifiedIndex), null); 166 modifiedIndex++; 167 } else { 168 newChildren.put(new Integer(modifiedIndex), n.get(i)); 169 modifiedIndex++; 170 } 171 } 172 173 if(newChildren.size() == 0) { 174 return GNode.create("toPurge", n); 175 } 176 177 //The new node which is going to be returned 178 Node modified = null; 179 180 if(n instanceof GNode) { 181 modified = GNode.create(name, newChildren.size()); 182 183 //Creation of the new Node 184 for (int k = 0; k < newChildren.size(); k++) { 185 Integer bigK = new Integer(k); 186 modified.add(k, newChildren.get(bigK)); 187 } 188 189 if (name == "FunctionDefinition") { 190 //We look for the name of this function 191 if (functionDefinitions.containsValue(n)) { 192 for (String funcName: functionDefinitions.keySet()) { 193 if(functionDefinitions.get(funcName) == n) { 194 modifiedFunctionDefinitions.put(funcName, (GNode)modified); 195 } 196 } 197 } 198 } 199 200 } 201 else if( (n instanceof Formatting)) { 202 // 203 Node f = build((Formatting) n, newChildren); 204 return f; 205 206 } else if(n instanceof Token) { 207 return n; 208 } else { 209 if(DBG_EXTRACT) 210 System.out.println("N KIND " + n.getName()); 211 } 212 213 if(modified == null) { 214 System.exit(-1); 215 } 216 217 return modified; 218 219 } 220 221 public Object visit(Annotation a) { 222 return extractVisitor(a); 223 } 224 225 public Node visit(Node n) { 226 return extractVisitor(n); 227 } 228 229 public void extract() { 230 newTree = (Node) dispatch(tree); 231 232 if(DBG_EXTRACT) { 233 //console Printer 234 Printer console = new Printer(new BufferedWriter(new OutputStreamWriter(System.out))); 235 System.out.println("\n\nPARSE TREE BEFORE EXTRACTION"); 236 console.format(tree, false).pln().flush(); 237 System.out.println("\n\nAFTER EXTRACTION"); 238 console.format(newTree, false).pln().flush(); 239 240 System.out.println("\n\n###EXTRACTION RESULT#####\n"); 241 System.out.println("PARSETREE PRINTER\n\n"); 242 new ParseTreePrinter(console).dispatch(tree); 243 console.flush(); 244 System.out.println("PARSENEWTREE PRINTER\n\n"); 245 new C4ParseTreePrinter(console).dispatch(newTree); 246 console.flush(); 247 System.out.println("\n#######\n"); 248 } 249 try { 250 251 Printer result_file = new Printer(new BufferedWriter(new OutputStreamWriter(new FileOutputStream("__extract_"+topLevel.substring(topLevel.lastIndexOf("/") + 1 ))))); 252 new C4ParseTreePrinter(result_file).dispatch(newTree); 253 result_file.flush(); 254 255 } catch(FileNotFoundException fne) { 256 System.out.println( "FILE: " + "__extract_"+topLevel.substring(topLevel.lastIndexOf("/") + 1 ) + " not found..."); 257 fne.printStackTrace(); 258 } 259 260 //Weaving 261 weave(newTree); 262 263 264 } 265 266 267 268 private ArrayList<C4Extractable> lookupToWeave(String funcName) { 269 ArrayList<C4Extractable> toWeave = new ArrayList<C4Extractable>(); 270 271 for(C4Extractable item : toExtract) { 272 if(item.getFuncName().equals(funcName)) { 273 toWeave.add(item); 274 } 275 } 276 return toWeave; 277 } 278 279 280 private void weave(Node newTree) { 281 Printer console = new Printer(new BufferedWriter(new OutputStreamWriter(System.out))); 282 new ParseTreePrinter(console).dispatch(tree); 283 System.out.println("\n\nABeforeEXTRACTION"); 284 console.format(newTree, false).pln().flush(); 285 286 287 new Visitor() { 288 public void visitFunctionDefinition(GNode n ) { 289 if (modifiedFunctionDefinitions.containsValue(n)) { 290 for (String funcName: modifiedFunctionDefinitions.keySet()) { 291 if(modifiedFunctionDefinitions.get(funcName) == n) { 292 ArrayList<C4Extractable> toWeave = lookupToWeave(funcName); 293 n.getGeneric(5).add(0,blockWrapping(toWeave)); 294 } 295 } 296 } 297 298 } 299 300 public void visit(Node n) { 301 for(int i=0; i < n.size(); i++) { 302 if(n.get(i) instanceof Node) { 303 dispatch(n.getNode(i)); 304 } 305 } 306 } 307 308 }.dispatch(newTree); 309 310 311 312 System.out.println("AFTER WEAVING PRINTER\n\n"); 313 new C4ParseTreePrinter(console).dispatch(newTree); 314 console.flush(); 315 System.out.println("\n#######\n"); 316 317 318 } 319 320 private boolean isToBeExtracted(Node n) { 321 for(C4Extractable toBeExtracted : toExtract) { 322 if(toBeExtracted.getGNodeToExtract() == n) { 323 return true; 324 } 325 } 326 327 return false; 328 } 329 330 private Node blockWrapping(ArrayList<C4Extractable> toWeave) { 331 GNode tmpGNodeBefore = GNode.create("RawFeatureBefore"); 332 GNode tmpGNodeAfter = GNode.create("RawFeatureAfter"); 333 334 for(C4Extractable item: toWeave) { 335 336 if(item.getKind().equals("before")) 337 tmpGNodeBefore.add(item.getGNodeToExtract()); 338 else if(item.getKind().equals("after")) 339 tmpGNodeAfter.add(item.getGNodeToExtract()); 340 341 } 342 343 GNode featureBlock = GNode.create("FeatureBlockStatement"); 344 345 if(tmpGNodeBefore.size() > 0) { 346 Formatting roundBefore = Formatting.round1(new Token("_before_" + featureName + " { \n\t"), tmpGNodeBefore,new Token("\n }\n")); 347 featureBlock.add(roundBefore); 348 } 349 if(tmpGNodeAfter.size() > 0) { 350 Formatting roundAfter = Formatting.round1(new Token("_after_" + featureName + " { \n\t"), tmpGNodeAfter,new Token("\n }\n")); 351 featureBlock.add(roundAfter); 352 } 353 354 return featureBlock; 355 356 } 32 /** The Debug extraction Flag **/ 33 private boolean DBG_EXTRACT = false; 34 /** The feature to create **/ 35 private String featureName; 36 37 /** The Configuration option to extract **/ 38 private String configOption; 39 /** The list of nodes marked as to be extracted by the CPP Analyzer **/ 40 private ArrayList<C4Extractable> toExtract; 41 42 /** The original Tree before any extraction **/ 43 private Node tree; 44 /** The new Tree with everything extracted **/ 45 private Node newTree; 46 /** The name of the top level file to be extracted **/ 47 /** We do not extract anything from the included header files **/ 48 private String topLevel; 49 50 /** The collection of functionDefinitions extracted from the analysis 51 */ 52 private Map<String, GNode> functionDefinitions; 53 54 /** The collection of functionDefinitions after all the modification 55 * populated during the extraction process 56 */ 57 private Map<String, GNode> modifiedFunctionDefinitions; 58 59 private Node father; 60 61 public C4Extractor(String featureName, String optExtract, ArrayList<C4Extractable> toExtract, Node unit, String fileName, Map<String, GNode> functionDefinitions ) { 62 this.featureName = featureName; 63 configOption = optExtract; 64 this.toExtract = toExtract; 65 tree = unit; 66 topLevel = fileName; 67 this.functionDefinitions = functionDefinitions; 68 modifiedFunctionDefinitions = new HashMap<String, GNode> (); 69 } 70 71 72 static Node build(Formatting n, HashMap<Integer, Object> newChildren) { 73 Formatting result = null ; 74 try { 75 if(n.size() != newChildren.size()) { 76 return GNode.create("toPurge", n); 77 } 78 if(n.getName().contains("After1")) { 79 result = Formatting.after1((Node) newChildren.get(new Integer(0)), newChildren.get(new Integer(1))); 80 } else if(n.getName().contains("Before1")) { 81 result = Formatting.before1(newChildren.get(new Integer(0)), (Node) newChildren.get(new Integer(1))); 82 } else if(n.getName().contains("Round1")) { 83 result = Formatting.round1(newChildren.get(new Integer(0)), (Node) newChildren.get(new Integer(1)), newChildren.get(new Integer(2))); 84 } else if(n.getName().contains("RoundN")) { 85 result = Formatting.variable(); 86 87 for(int index = 0; index < newChildren.size(); index++) { 88 result.add(index, newChildren.get(new Integer(index))); 89 } 90 91 } 92 else { 93 result = null; 94 } 95 } catch (Exception ex) { 96 System.out.println("\n\n######EXCEPTION \n\n" + ex.getMessage()); 97 System.out.println("FORMATTING " + n ); 98 System.out.println("NEW CHILDREN " + newChildren); 99 100 System.exit(-1); 101 result = null; 102 } 103 104 return result; 105 } 106 107 private Node extractVisitor(Node n) { 108 //Node modified = GNode.create((GNode)n); 109 110 //When we remove some children, we have to change the index. 111 //the modifiedIndex is aware of the updated size. 112 113 int modifiedIndex = 0; 114 ArrayList<Integer> toRemove = new ArrayList<Integer>(); 115 HashMap<Integer, Object> newChildren = new HashMap<Integer, Object>(); 116 String name = n.getName(); 117 118 if(0 == n.size()) { 119 if(DBG_EXTRACT) 120 System.out.println("SIZE ZERO... We return the same node " + n); 121 return n; 122 } 123 124 for(int i = 0; i< n.size(); i++) { 125 if(n.get(i) instanceof Node) { 126 Integer index = new Integer(i); 127 Node child = (Node) n.get(i); 128 129 if(isToBeExtracted(child)) { 130 131 if(DBG_EXTRACT) 132 System.out.println("We are going to remove child number : " + index); 133 134 father = n; 135 toRemove.add(index); 136 137 } else { 138 139 Node dispatched = (Node) dispatch(child); 140 141 if(DBG_EXTRACT) 142 System.out.println("DISPATCHED " + dispatched + "\nCHILD " + child); 143 144 if(dispatched != null) { 145 146 if(!dispatched.getName().equals("toPurge")) { 147 newChildren.put(new Integer(modifiedIndex), dispatched); 148 modifiedIndex++; 149 } else { 150 if(DBG_EXTRACT) 151 System.out.println("DISPATCHED " + dispatched); 152 } 153 } else { 154 if(DBG_EXTRACT) 155 System.out.println("========= DISPATCHED IS NULL ===========>>>>>> " + dispatched); 156 } 157 158 } 159 160 161 } else if(n.get(i) instanceof String) { 162 newChildren.put(new Integer(modifiedIndex), n.getString(i)); 163 164 modifiedIndex++; 165 166 } else if(null == n.get(i)) { 167 newChildren.put(new Integer(modifiedIndex), null); 168 modifiedIndex++; 169 } else { 170 newChildren.put(new Integer(modifiedIndex), n.get(i)); 171 modifiedIndex++; 172 } 173 } 174 175 if(newChildren.size() == 0) { 176 return GNode.create("toPurge", n); 177 } 178 179 //The new node which is going to be returned 180 Node modified = null; 181 182 if(n instanceof GNode) { 183 modified = GNode.create(name, newChildren.size()); 184 185 //Creation of the new Node 186 for (int k = 0; k < newChildren.size(); k++) { 187 Integer bigK = new Integer(k); 188 modified.add(k, newChildren.get(bigK)); 189 } 190 191 if (name.equals("FunctionDefinition")) { 192 //We look for the name of this function 193 if (functionDefinitions.containsValue(n)) { 194 for (String funcName: functionDefinitions.keySet()) { 195 if(functionDefinitions.get(funcName) == n) { 196 modifiedFunctionDefinitions.put(funcName, (GNode)modified); 197 } 198 } 199 } 200 } 201 202 } 203 else if( (n instanceof Formatting)) { 204 // 205 Node f = build((Formatting) n, newChildren); 206 return f; 207 208 } else if(n instanceof Token) { 209 return n; 210 } else { 211 if(DBG_EXTRACT) 212 System.out.println("N KIND " + n.getName()); 213 } 214 215 if(modified == null) { 216 System.exit(-1); 217 } 218 219 return modified; 220 221 } 222 223 public Object visit(Annotation a) { 224 return extractVisitor(a); 225 } 226 227 public Node visit(Node n) { 228 return extractVisitor(n); 229 } 230 231 public void extract() { 232 newTree = (Node) dispatch(tree); 233 234 if(DBG_EXTRACT) { 235 //console Printer 236 Printer console = new Printer(new BufferedWriter(new OutputStreamWriter(System.out))); 237 System.out.println("\n\nPARSE TREE BEFORE EXTRACTION"); 238 console.format(tree, false).pln().flush(); 239 System.out.println("\n\nAFTER EXTRACTION"); 240 console.format(newTree, false).pln().flush(); 241 242 System.out.println("\n\n###EXTRACTION RESULT#####\n"); 243 System.out.println("PARSETREE PRINTER\n\n"); 244 new ParseTreePrinter(console).dispatch(tree); 245 console.flush(); 246 System.out.println("PARSENEWTREE PRINTER\n\n"); 247 new C4ParseTreePrinter(console).dispatch(newTree); 248 console.flush(); 249 System.out.println("\n#######\n"); 250 } 251 try { 252 253 Printer result_file = new Printer(new BufferedWriter(new OutputStreamWriter(new FileOutputStream("__extract_"+topLevel.substring(topLevel.lastIndexOf("/") + 1 ))))); 254 new C4ParseTreePrinter(result_file).dispatch(newTree); 255 result_file.flush(); 256 257 } catch(FileNotFoundException fne) { 258 System.out.println( "FILE: " + "__extract_"+topLevel.substring(topLevel.lastIndexOf("/") + 1 ) + " not found..."); 259 fne.printStackTrace(); 260 } 261 262 //Weaving 263 weave(newTree); 264 265 266 } 267 268 269 270 private ArrayList<C4Extractable> lookupToWeave(String funcName) { 271 ArrayList<C4Extractable> toWeave = new ArrayList<C4Extractable>(); 272 273 for(C4Extractable item : toExtract) { 274 if(item.getFuncName().equals(funcName)) { 275 toWeave.add(item); 276 } 277 } 278 279 return toWeave; 280 } 281 282 283 private void weave(Node newTree) { 284 Printer console = new Printer(new BufferedWriter(new OutputStreamWriter(System.out))); 285 new ParseTreePrinter(console).dispatch(tree); 286 //System.out.println("\n\nBEFORE WEAVING"); 287 //console.format(newTree, false).pln().flush(); 288 289 290 new Visitor() { 291 public void visitFunctionDefinition(GNode n ) { 292 if (modifiedFunctionDefinitions.containsValue(n)) { 293 for (String funcName: modifiedFunctionDefinitions.keySet()) { 294 if(modifiedFunctionDefinitions.get(funcName) == n) { 295 ArrayList<C4Extractable> toWeave = lookupToWeave(funcName); 296 n.getGeneric(5).add(0,blockWrapping(toWeave)); 297 } 298 } 299 } 300 301 } 302 303 public void visit(Node n) { 304 for(int i=0; i < n.size(); i++) { 305 if(n.get(i) instanceof Node) { 306 dispatch(n.getNode(i)); 307 } 308 } 309 } 310 311 }.dispatch(newTree); 312 313 314 315 System.out.println("\nAFTER WEAVING\n\n"); 316 console.format(newTree, false).pln().flush(); 317 System.out.println("\nAfterC4ParseTree\n"); 318 new C4ParseTreePrinter(console).dispatch(newTree); 319 console.flush(); 320 System.out.println("\n#######\n"); 321 322 323 } 324 325 private boolean isToBeExtracted(Node n) { 326 for(C4Extractable toBeExtracted : toExtract) { 327 if(toBeExtracted.getGNodeToExtract() == n) { 328 return true; 329 } 330 } 331 332 return false; 333 } 334 335 private Node blockWrapping(ArrayList<C4Extractable> toWeave) { 336 GNode tmpGNodeBefore = GNode.create("RawFeatureBefore"); 337 GNode tmpGNodeAfter = GNode.create("RawFeatureAfter"); 338 339 for(C4Extractable item: toWeave) { 340 341 if(item.getKind().equals("before")) 342 tmpGNodeBefore.add(item.getGNodeToExtract()); 343 else if(item.getKind().equals("after")) 344 tmpGNodeAfter.add(item.getGNodeToExtract()); 345 346 } 347 348 GNode featureBlock = GNode.create("FeatureBlockStatement"); 349 350 if(tmpGNodeBefore.size() > 0) { 351 Formatting roundBefore = Formatting.round1(new Token("_before_" + featureName + " { \n\t"), tmpGNodeBefore,new Token("\n }\n\t\n")); 352 featureBlock.add(roundBefore); 353 } 354 355 if(tmpGNodeAfter.size() > 0) { 356 Formatting roundAfter = Formatting.round1(new Token("_after_" + featureName + " { \n\t"), tmpGNodeAfter,new Token("\n }\n")); 357 featureBlock.add(roundAfter); 358 } 359 360 return featureBlock; 361 362 } 357 363 } -
trunk/cpp_analysis/src/xtc/lang/c4/C4MacrosManager.java
r193 r196 25 25 private Map<String, ArrayList<Node>> oldDefinitionsMacros; 26 26 27 /** A map of the current definition of each macro defined **/28 private Map<String, Node> currentDefinitionMacros;29 private Map<String, Integer> currentDefinitionIndex;30 31 27 /** To remove all the macros defined in the previous branch...*/ 32 28 private ArrayList<ArrayList <String>> branchMacros; … … 57 53 this.macro_keyword_parents = new ArrayList<ArrayList<String>>(); 58 54 this.keywords_toFlush = new ArrayList<ArrayList<String>>(); 59 this.branchMacros.add(lastBranchMacros); 55 56 this.branchMacros.add(lastBranchMacros); 60 57 } 61 58 … … 87 84 name = objMacro.getGeneric(0).getString(0); 88 85 } 89 90 86 91 87 if(!oldDefinitionsMacros.containsKey(name)) { 92 88 oldDefinitionsMacros.put(name, new ArrayList<Node>()); … … 115 111 String name = funcMacro.getString(0); 116 112 Node value = funcMacro; 117 113 118 114 if(!oldDefinitionsMacros.containsKey(name)) { 119 115 oldDefinitionsMacros.put(name, new ArrayList<Node>()); … … 457 453 458 454 public Object getFunctionMacro(String macroName) { 459 return functionMacros.get(macroName); 455 if(isFunctionMacro(macroName)) { 456 if (lastBranchMacrosFunction != null) { 457 if (lastBranchMacrosFunction.containsKey(macroName)) { 458 return lastBranchMacrosFunction.get(macroName); 459 } 460 } 461 462 return functionMacros.get(macroName); 463 } 464 return null; //TODO throws an exception 465 460 466 } 461 467 … … 468 474 public Node retrieveMacro(String macroName) { 469 475 Node macroNode = null; 470 if( lastBranchMacrosFunction != null ) { 471 if(lastBranchMacrosFunction.containsKey(macroName)) { 472 macroNode = getMacro(lastBranchMacrosFunction, macroName); 473 } 474 } else if(functionMacros.containsKey(macroName)) { 475 macroNode = getMacro(functionMacros, macroName); 476 } 477 478 if( lastBranchMacrosObject != null ) { 479 if( lastBranchMacrosObject.containsKey(macroName)) { 480 macroNode = getMacro(lastBranchMacrosObject, macroName); 481 } 482 } else if(objectMacros.containsKey(macroName)) { 483 macroNode = getMacro(objectMacros, macroName); 484 } 485 476 if(isFunctionMacro(macroName)) { 477 if( lastBranchMacrosFunction != null ) { 478 if(lastBranchMacrosFunction.containsKey(macroName)) { 479 macroNode = getMacro(lastBranchMacrosFunction, macroName); 480 } 481 } 482 483 if(functionMacros.containsKey(macroName)) { 484 macroNode = getMacro(functionMacros, macroName); 485 } 486 487 } else if(isObjectMacro(macroName)) { 488 if( lastBranchMacrosObject != null ) { 489 if( lastBranchMacrosObject.containsKey(macroName)) { 490 macroNode = getMacro(lastBranchMacrosObject, macroName); 491 } 492 } 493 494 if(objectMacros.containsKey(macroName)) { 495 macroNode = getMacro(objectMacros, macroName); 496 } 497 } 486 498 return macroNode; 487 499 } … … 521 533 } 522 534 523 535 /* 524 536 public void updateDefine(String macroName) { 525 537 if(!currentDefinitionIndex.containsKey(macroName)){ … … 528 540 currentDefinitionIndex.put(macroName, currentDefinitionIndex.get(macroName) + 1); 529 541 } 530 542 531 543 currentDefinitionMacros.put(macroName, oldDefinitionsMacros.get(macroName).get(0)); 532 544 } … … 535 547 //The macro that we undef is not defined 536 548 currentDefinitionMacros.put(macroName, null); 537 538 } 549 } 550 551 public Node retrieveMacroAnalysis(String macroName) { 552 return currentDefinitionMacros.get(macroName); 553 }*/ 539 554 540 555 -
trunk/cpp_analysis/src/xtc/lang/c4/C4ParseTreePrinter.java
r185 r196 35 35 } 36 36 37 public void visitMacroBased4(GNode n) { 38 int i = 0; 39 for (Object o : n) { 40 //We do not want to consider the last child since this is the definition 41 if ((o instanceof Node) && (i < n.size() - 1)) 42 dispatch((Node)o); 43 i++; 44 } 45 } 46 37 47 public void visitUserHeader(GNode n) { 38 48 39 49 for (Object o : n) { 40 50 if (o instanceof Node) { 41 if(((Node)o).getName() == "Include")51 if(((Node)o).getName().equals("Include")) 42 52 dispatch((Node)o); 43 53 } … … 49 59 for (Object o : n) { 50 60 if (o instanceof Node) { 51 if(((Node)o).getName() == "Include")61 if(((Node)o).getName().equals("Include")) 52 62 dispatch((Node)o); 53 63 } -
trunk/cpp_analysis/src/xtc/lang/c4/C4ParserState.java
r192 r196 553 553 554 554 555 if(n.getString(1) == "<<")555 if(n.getString(1).equals("<<")) 556 556 result = tmp1 << tmp2; 557 557 … … 597 597 tmp2 = string2long(tmpS2); 598 598 599 if(n.get(1) == "<")599 if(n.get(1).equals("<")) 600 600 result = tmp1 < tmp2 ? 1 : 0; 601 else if (n.get(1) == ">")601 else if (n.get(1).equals(">")) 602 602 result = tmp1 > tmp2 ? 1 : 0; 603 else if (n.get(1) == ">=")603 else if (n.get(1).equals(">=")) 604 604 result = tmp1 >= tmp2 ? 1 : 0; 605 else if (n.get(1) == ">=")605 else if (n.get(1).equals(">=")) 606 606 result = tmp1 >= tmp2 ? 1 : 0; 607 607 … … 617 617 tmpS2 = dispatch((Node)n.get(2)).toString(); 618 618 tmp2 = string2long(tmpS2); 619 if(n.get(1) == "==")619 if(n.get(1).equals("==")) 620 620 result = tmp1 == tmp2 ? 1 : 0; 621 else if (n.get(1) == "!=")621 else if (n.get(1).equals("!=")) 622 622 result = tmp1 != tmp2 ? 1 : 0; 623 623 … … 635 635 tmp2 = string2long(tmpS2); 636 636 637 if(n.get(1) == "+")637 if(n.get(1).equals("+")) 638 638 result = tmp1+tmp2; 639 else if (n.get(1) == "-")639 else if (n.get(1).equals("-")) 640 640 result = tmp1-tmp2; 641 641 … … 653 653 654 654 655 if(n.get(1) == "*")655 if(n.get(1).equals("*")) 656 656 result = tmp1*tmp2; 657 else if (n.get(1) == "/")657 else if (n.get(1).equals("/")) 658 658 result = tmp1/tmp2; 659 else if (n.get(1) == "%")659 else if (n.get(1).equals("%")) 660 660 result = tmp1%tmp2; 661 661 return Long.toString(result); … … 818 818 819 819 820 if(n.getString(1) == "<<")820 if(n.getString(1).equals("<<")) 821 821 result = tmp1 << tmp2; 822 822 … … 865 865 tmp2 = string2long(tmpS2); 866 866 867 if(n.getString(1) == "<")867 if(n.getString(1).equals("<")) 868 868 result = tmp1 < tmp2 ? 1 : 0; 869 else if (n.getString(1) == ">")869 else if (n.getString(1).equals(">")) 870 870 result = tmp1 > tmp2 ? 1 : 0; 871 else if (n.getString(1) == ">=")871 else if (n.getString(1).equals(">=")) 872 872 result = tmp1 >= tmp2 ? 1 : 0; 873 else if (n.getString(1) == ">=")873 else if (n.getString(1).equals(">=")) 874 874 result = tmp1 >= tmp2 ? 1 : 0; 875 875 … … 885 885 tmpS2 = dispatch((Node)n.get(2)).toString(); 886 886 tmp2 = string2long(tmpS2); 887 if(n.getString(1) == "==")887 if(n.getString(1).equals("==")) 888 888 result = tmp1 == tmp2 ? 1 : 0; 889 else if (n.getString(1) == "!=")889 else if (n.getString(1).equals("!=")) 890 890 result = tmp1 != tmp2 ? 1 : 0; 891 891 … … 904 904 905 905 906 if(n.getString(1) == "+")906 if(n.getString(1).equals("+")) 907 907 result = tmp1+tmp2; 908 else if (n.getString(1) == "-")908 else if (n.getString(1).equals("-")) 909 909 result = tmp1-tmp2; 910 910 … … 922 922 923 923 924 if(n.getString(1) == "*")924 if(n.getString(1).equals("*")) 925 925 result = tmp1*tmp2; 926 else if (n.getString(1) == "/")926 else if (n.getString(1).equals("/")) 927 927 result = tmp1/tmp2; 928 else if (n.getString(1) == "%")928 else if (n.getString(1).equals("%")) 929 929 result = tmp1%tmp2; 930 930 … … 969 969 else 970 970 { 971 throw new MacroUndefException(macroName);971 throw new MacroUndefException(macroName); 972 972 } 973 973 return macroValue; … … 1725 1725 } 1726 1726 1727 if(op == ">")1727 if(op.equals(">")) 1728 1728 result = ( leftValueI > rightValueI); 1729 if(op == ">=")1729 if(op.equals(">=")) 1730 1730 result = ( leftValueI >= rightValueI); 1731 if(op == "<")1731 if(op.equals("<")) 1732 1732 result = ( leftValueI < rightValueI); 1733 if(op == "<="){1733 if(op.equals("<=")) { 1734 1734 result = ( leftValueI <= rightValueI); 1735 1735 } … … 1777 1777 Node tmpArithmeticExpression = arithmeticExpression.strip(); 1778 1778 Long tmpCompute; 1779 if (tmpArithmeticExpression.getName() == "IfGroupAdditiveExpression"||1780 tmpArithmeticExpression.getName() == "IfGroupMultiplicativeExpression"||1781 tmpArithmeticExpression.getName() == "IfGroupBinaryExpression"||1782 tmpArithmeticExpression.getName() == "FunctionMacroCallExpression") {1779 if (tmpArithmeticExpression.getName().equals("IfGroupAdditiveExpression") || 1780 tmpArithmeticExpression.getName().equals("IfGroupMultiplicativeExpression") || 1781 tmpArithmeticExpression.getName().equals("IfGroupBinaryExpression") || 1782 tmpArithmeticExpression.getName().equals("FunctionMacroCallExpression")) { 1783 1783 1784 1784 tmpCompute = computeVisitor(tmpArithmeticExpression); … … 1787 1787 1788 1788 } else { 1789 if (tmpArithmeticExpression.getName() == "IntegerConstant") {1789 if (tmpArithmeticExpression.getName().equals("IntegerConstant") ) { 1790 1790 try { 1791 1791 String tmpS = tmpArithmeticExpression.getString(0); … … 1803 1803 } 1804 1804 } else { 1805 if ( tmpArithmeticExpression.getName() == "DefinedExpressionID") {1805 if ( tmpArithmeticExpression.getName().equals("DefinedExpressionID")) { 1806 1806 //in the case the macro is not defined but still used... 1807 1807 //its value is 0 … … 1828 1828 tmp1 = (Long) dispatch((Node)n.get(0)); 1829 1829 tmp2 = (Long) dispatch((Node)n.get(2)); 1830 if(n.getString(1) == "+")1830 if(n.getString(1).equals("+")) 1831 1831 result = tmp1+tmp2; 1832 else if (n.getString(1) == "-")1832 else if (n.getString(1).equals("-")) 1833 1833 result = tmp1-tmp2; 1834 1834 … … 1842 1842 tmp1 = ((Long) dispatch((Node)n.get(0))).longValue(); 1843 1843 tmp2 = ((Long) dispatch((Node)n.get(2))).longValue(); 1844 if( tmp2 == 0 && ((n.getString(1) == "%") || (n.getString(1) =="/"))) {1844 if( tmp2 == 0 && ((n.getString(1).equals("%")) || (n.getString(1).equals("/")))) { 1845 1845 tmp2 =1; 1846 1846 } 1847 if(n.getString(1) == "*")1847 if(n.getString(1).equals("*")) 1848 1848 result = tmp1*tmp2; 1849 else if (n.getString(1) == "/")1849 else if (n.getString(1).equals("/")) 1850 1850 result = tmp1/tmp2; 1851 else if (n.getString(1) == "%")1851 else if (n.getString(1).equals("%")) 1852 1852 result = tmp1%tmp2; 1853 1853 … … 1861 1861 tmp1 = ((Long) dispatch((Node)n.get(0))).longValue(); 1862 1862 tmp2 = ((Long) dispatch((Node)n.get(2))).longValue(); 1863 if(n.getString(1) == "&")1863 if(n.getString(1).equals("&")) 1864 1864 result = tmp1&tmp2; 1865 else if (n.getString(1) == "|")1865 else if (n.getString(1).equals("|")) 1866 1866 result = tmp1|tmp2; 1867 1867 … … 1883 1883 tmp1 = ((Long) dispatch((Node)n.get(0))).longValue(); 1884 1884 tmp2 = ((Long) dispatch((Node)n.get(2))).longValue(); 1885 if(n.getString(1) == "<<")1885 if(n.getString(1).equals("<<")) 1886 1886 result = tmp1 << tmp2; 1887 1887 … … 1966 1966 1967 1967 1968 if(n.getString(1) == "<<")1968 if(n.getString(1).equals("<<")) 1969 1969 result = tmp1 << tmp2; 1970 1970 … … 2010 2010 tmp2 = string2long(tmpS2); 2011 2011 2012 if(n.getString(1) == "<")2012 if(n.getString(1).equals("<")) 2013 2013 result = tmp1 < tmp2 ? 1 : 0; 2014 else if (n.getString(1) == ">")2014 else if (n.getString(1).equals(">")) 2015 2015 result = tmp1 > tmp2 ? 1 : 0; 2016 else if (n.getString(1) == ">=")2016 else if (n.getString(1).equals(">=")) 2017 2017 result = tmp1 >= tmp2 ? 1 : 0; 2018 else if (n.getString(1) == ">=")2018 else if (n.getString(1).equals(">=")) 2019 2019 result = tmp1 >= tmp2 ? 1 : 0; 2020 2020 … … 2030 2030 tmpS2 = dispatch((Node)n.get(2)).toString(); 2031 2031 tmp2 = string2long(tmpS2); 2032 if(n.getString(1) == "==")2032 if(n.getString(1).equals("==")) 2033 2033 result = tmp1 == tmp2 ? 1 : 0; 2034 else if (n.getString(1) == "!=")2034 else if (n.getString(1).equals("!=")) 2035 2035 result = tmp1 != tmp2 ? 1 : 0; 2036 2036 … … 2047 2047 tmpS2 = dispatch((Node)n.get(2)).toString(); 2048 2048 tmp2 = string2long(tmpS2); 2049 if(n.getString(1) == "+")2049 if(n.getString(1).equals("+")) 2050 2050 result = tmp1+tmp2; 2051 else if (n.getString(1) == "-")2051 else if (n.getString(1).equals("-")) 2052 2052 result = tmp1-tmp2; 2053 2053 … … 2065 2065 2066 2066 2067 if(n.getString(1) == "*")2067 if(n.getString(1).equals("*")) 2068 2068 result = tmp1*tmp2; 2069 else if (n.getString(1) == "/")2069 else if (n.getString(1).equals("/")) 2070 2070 result = tmp1/tmp2; 2071 else if (n.getString(1) == "%")2071 else if (n.getString(1).equals("%")) 2072 2072 result = tmp1%tmp2; 2073 2073 return Long.valueOf(result); … … 2246 2246 public boolean isBlockBeginning(String macroIsBegin) { 2247 2247 boolean result = false; 2248 2248 2249 if(!isMacro(macroIsBegin)) 2249 2250 result = false; 2250 2251 else { 2251 result = visitForCurly("{", getMacro(macroIsBegin)); 2252 Node macroNode = getMacro(macroIsBegin); 2253 2254 if(macroNode == null) { 2255 result = false; 2256 } else { 2257 result = visitForCurly("{", macroNode); 2258 } 2252 2259 } 2253 2260 return result; … … 2264 2271 result = false; 2265 2272 else { 2273 Node macroNode = getMacro(macroIsEnd); 2274 if(macroNode == null){ 2275 result = false; 2276 } else { 2266 2277 result = visitForCurly("}", getMacro(macroIsEnd)); 2278 } 2267 2279 } 2268 2280 return result; -
trunk/cpp_analysis/src/xtc/lang/c4/CPPAnalyzer.java
r193 r196 1185 1185 for (int i = 0; i < size; i++) { 1186 1186 final GNode declaration = declarationList.getGeneric(i); 1187 1188 1187 if(declaration.size() < 2) { 1189 1188 … … 1194 1193 1195 1194 if(null != name) { 1196 if(declaration.getGeneric(0).getName() == "IfSection") { 1195 String declarationName = declaration.getGeneric(0).getName(); 1196 if(declarationName.equals("IfSection")) { 1197 1197 c4log.logStructCond(name, i== size - 1 , declaration.getLocation()); 1198 } 1198 } else if(declarationName.equals("Define")) { 1199 dispatch(declaration.getGeneric(0)); 1200 } 1199 1201 } 1200 1202 } else { … … 1261 1263 } 1262 1264 } 1265 1266 /** Visit the specified macro Definition. 1267 * Just to check if the macro is conditionally defined 1268 * Duplicate of the global visitor because of the specifiers subclass 1269 **/ 1270 1271 public void visitDefine(GNode define) { 1272 String name=""; 1273 String kind = define.getGeneric(0).getName(); 1274 if(kind.equals("FunctionMacro")) { 1275 name = define.getGeneric(0).getString(0); 1276 } else if(kind.equals("ObjectMacro")) { 1277 name = define.getGeneric(0).getGeneric(0).getString(0); 1278 } else if(kind.equals("Undef")) { 1279 name = define.getGeneric(0).getGeneric(0).getString(0); 1280 conditionalDefs.remove(name); 1281 return; 1282 } 1283 1284 if(isConditionallyDefined(define) && configOptions.size() > 0) { 1285 if(0 != conditionStack.size() && !name.equals("")) { 1286 if(markedConfigOptions.size() > 0) // We add this function only if the condition is based on a config option 1287 conditionalDefs.put(name, markedConfigOptions ); 1288 } else { 1289 try{ 1290 throw new NullPointerException(); 1291 }catch(NullPointerException npe) { 1292 System.err.println("Problem with the conditional definitions visitDefine " + define.getLocation() 1293 ); 1294 npe.printStackTrace(); 1295 conditionalDefs.put(name, null); 1296 } 1297 } 1298 1299 } 1300 } 1263 1301 1264 1302 /** Process the complex type specifier. */ … … 2296 2334 public static String getDeclaredIdName(GNode declarator) { 2297 2335 String functionName = "Fuction Name"; 2298 if (declarator.getName() == "IfSection") {2336 if (declarator.getName().equals("IfSection")) { 2299 2337 functionName = (String) new Visitor() { 2300 2338 String name; … … 2597 2635 2598 2636 StringWriter tmpString = c4log.init_logging(dbStore); 2599 2600 2637 if(cmdLineHeaders != null) { 2601 2638 //if header files were given on the command line, we dispatch them... … … 2610 2647 //Dispatching the top level 2611 2648 dispatch(unit); 2612 2613 2649 //Extraction 2614 2650 if(null != extract && atTopLevelFile()) { … … 2618 2654 2619 2655 c4log.dumpResults(tmpString, ifSectionNumber, macroNumber); 2620 2621 2656 return table; 2622 2657 } … … 3450 3485 + elsen.get(0)); 3451 3486 } 3487 3452 3488 if (elsen.getGeneric(0).getName() != "Unparsed") { 3453 3489 //This condition holds. … … 3514 3550 } 3515 3551 3516 if (ifDefn.getGeneric(1).getName() != "Unparsed") { //unnecessary test, if it's a config option, this is parsed3517 if(configOptions.contains(ifDefn.getString(0))) //If this is a config option3552 if (ifDefn.getGeneric(1).getName() != "Unparsed") { 3553 if(configOptions.contains(ifDefn.getString(0))) { //If this is a config option 3518 3554 tmpConfigOptions.add(ifDefn.getString(0)); 3519 } 3555 } 3556 } else { //We log the config options which are not defined anyway 3557 if (ifDefn.getString(0).startsWith("CONFIG_")) { 3558 tmpNDefConfigOptions.add("!" + ifDefn.getString(0)); 3559 } 3560 } 3520 3561 } 3521 3562 … … 3527 3568 3528 3569 //ConfigOptions which are not defined should not hold, so we only log them to build for the else 3529 if(configOptions.contains(ifNDefn.getString(0))) //If this is a config option 3530 tmpNDefConfigOptions.add(ifNDefn.getString(0)); 3570 if (ifNDefn.getGeneric(1).getName() != "Unparsed") { 3571 //the ifndef condition is true 3572 //we check if the condition is a potential config option 3573 if (ifNDefn.getString(0).startsWith("CONFIG_")) { 3574 //Config Option, we log it 3575 tmpConfigOptions.add("!" + ifNDefn.getString(0)); 3576 } 3577 } else { 3578 if(configOptions.contains(ifNDefn.getString(0))) { //If this is a config option 3579 tmpNDefConfigOptions.add(ifNDefn.getString(0)); 3580 } 3581 } 3531 3582 } 3532 3583 … … 3729 3780 3730 3781 3731 } else if (functionBody.getGeneric(index+1).getName() == "ReturnStatement") { //just before a return3782 } else if (functionBody.getGeneric(index+1).getName().equals("ReturnStatement")) { //just before a return 3732 3783 result = true; 3733 3784 } … … 5468 5519 if (macroDefinition.getGeneric(1).get(0) instanceof Node) { 5469 5520 if (macroDefinition.getGeneric(1).getNode(0).strip() != null) { 5470 //System.out.println("NULL AFTER STRIP " + macroDefinition.getGeneric(1).getNode(0));5471 //System.out.println("MACRODEF GET GEN 1" + macroDefinition.getGeneric(1));5472 //System.out.println("MACRODEF GET GEN 1STRIPPED " + macroDefinition.getGeneric(1).strip());5473 //System.out.println("MACRO GETGENERIC " + macroDefinition.getGeneric(1).size());5474 // System.exit(-1);5475 5476 // else5477 5521 if (macroDefinition.getGeneric(1).getNode(0).strip().isGeneric()) { 5478 5522 if (macroDefinition.getGeneric(1).getGeneric(0) != null) { … … 5726 5770 public void visitCPPIdentifierList(GNode n) { 5727 5771 for(int i=0; i < n.size(); i++) { 5728 //System.out.println("ADDING " + n.getString(i));5729 5772 idsInMacro.add(n.getString(i)); 5730 5773 } … … 7019 7062 /** Visit the specified macro Definition. 7020 7063 * Just to check if the macro is conditionally defined */ 7021 7022 7064 public void visitDefine(GNode define) { 7023 String name="";7065 String name=""; 7024 7066 String kind = define.getGeneric(0).getName(); 7025 7067 if(kind.equals("FunctionMacro")) { 7026 7068 name = define.getGeneric(0).getString(0); 7027 macrosManager.updateDefine(name);7028 7069 } else if(kind.equals("ObjectMacro")) { 7029 7070 name = define.getGeneric(0).getGeneric(0).getString(0); 7030 macrosManager.updateDefine(name);7031 7071 } else if(kind.equals("Undef")) { 7072 name = define.getGeneric(0).getGeneric(0).getString(0); 7073 conditionalDefs.remove(name); 7032 7074 return; 7033 7075 } … … 7947 7989 } 7948 7990 7991 //Logging 7992 //if(!alreadyConsideredFunctionCall() &&) { 7949 7993 // CONVERTIBLE BEFORE 7950 7994 convertibleBefore = isConvertibleBefore(n); … … 7967 8011 funcDefLoc = ((GNode)functionDefinitions.get(name)).getLocation(); 7968 8012 if (conditionalDefs.containsKey(name)) { 8013 8014 7969 8015 c4log.logWrapper("Function", name, funcDefLoc, loc, conditionStack, markedConfigOptionsStack, condDefs, movableBefore, movableAfter, convertibleBefore, convertibleAfter); 7970 8016 clogger.setConvertibleBeforeFuncCall(conditionalDefs.get(name), loc,convertibleBefore); … … 7973 8019 7974 8020 } 7975 // c4log.logBeforeInfo("Function", name, funcDefLoc, loc, movableBefore, conditionStack, condDefs); 7976 // c4log.logAfterInfo("Function", name, funcDefLoc, movableAfter, n 7977 // .getLocation(), conditionStack, condDefs); 7978 8021 //}// END OF LOGGING 7979 8022 //END OF ANALYSIS REPORT 7980 8023 } … … 8257 8300 if(null != extract && atTopLevelFile()) { 8258 8301 if(markedConfigOptions.contains(extract)) { 8259 toExtract.add(new C4Extractable(functionDefinitionName, funcDef,n, "before")); 8302 if(isInFunctionBody() && funcDef != null) { //To Be sure we are inside a function 8303 toExtract.add(new C4Extractable(functionDefinitionName, funcDef,n, "before")); 8304 } else { 8305 //toExtract.add(new C4Extractable(null, null, n, "before")); 8306 //System.out.println("NODE " + n); 8307 } 8260 8308 } 8261 8309 } … … 8366 8414 *@return The node associated with the macroName 8367 8415 * */ 8368 public Node retrieveMacroAnalysis(String macroName) { 8369 Node macro = macrosManager.retrieveMacro(macroName); 8370 8371 if(macro != null) 8372 return macro; 8373 8374 if(macrosManager.getRemovedMacros().containsKey(macroName)) 8375 { 8376 return (Node) macrosManager.getRemovedMacros().get(macroName).get(macrosManager.getRemovedMacros().get(macroName).size() - 1); 8377 } 8378 8379 return null; 8380 8381 } 8416 /*public Node retrieveMacroAnalysis(String macroName) { 8417 return macrosManager.retrieveMacroAnalysis(macroName); 8418 }*/ 8382 8419 8383 8420 /** Visit the specified if else statement. */ … … 8611 8648 Node parameters = n.getGeneric(parameterPosition) != null ? n.getGeneric(parameterPosition) : null; // FunctionMacroCallParametersList 8612 8649 String macroName = n.getString(macroNamePosition); 8613 Node macroDefinition = retrieveMacroAnalysis(macroName); 8614 Location macroDefLoc = macroDefinition.getLocation(); 8615 if (1 <= DEBUG) { 8650 8651 8652 //Node macroDefinition = retrieveMacroAnalysis(macroName); 8653 8654 Node macroDefinition = n.getNode(n.size()-1); 8655 8656 Location macroDefLoc = macroDefinition.getLocation(); 8657 8658 if (1 <= DEBUG) { 8616 8659 if(macroDefinition == null) { 8617 8660 System.out.println("MACRO " + macroName + " LOCATION " + n.getLocation()); … … 8687 8730 } 8688 8731 // 8689 8690 8732 //Extraction 8691 8733 if(null != extract) { … … 8699 8741 } 8700 8742 8743 8744 8745 // 8746 8701 8747 //BEFORE 8702 8748 8703 8749 //We've already seen a return statement we cannot move this macro 8704 8750 //Or this is in the right hand side of an assignement 8751 8752 8753 8705 8754 if((returnSeen || rightHandSide) || (ifsNode.size() > 0) || (loopsNode.size() > 0) || !inFuncBody) { 8706 8755 //if((returnSeen) || (rightHandSide)){ … … 8721 8770 ArrayList<String> defs = conditionalDefs.get(macroName); 8722 8771 movableAfter = isMovableAfterFunctionMacroRef(n, idsInMacro, inFuncBody); 8723 8772 8773 //SIGNATURE 8774 if(!atTopLevelFile()) {// We look at the signatures for header files 8775 if(logger.alreadyConsideredMacro(n.getGeneric(n.size()-1), macroName, loc.toString())) { 8776 return ; 8777 } 8778 } 8779 8780 8724 8781 c4log.logWrapper("MacroReference", macroName, macroDefLoc, loc, conditionStack, markedConfigOptionsStack, defs, movableBefore, movableAfter, convertibleBefore, convertibleAfter); 8725 8782 … … 8746 8803 public void visitMacroReference(GNode n) { 8747 8804 String macroName = n.getString(0); 8748 Node macroDefinition = retrieveMacroAnalysis(macroName); 8805 //Node macroDefinition = retrieveMacroAnalysis(macroName); 8806 Node macroDefinition = n.getNode(n.size()- 1); 8749 8807 Location macroDefLoc = macroDefinition.getLocation(); 8808 8750 8809 // We need to know what is the scope hierarchy 8751 8810 // We only want to analyze when the macro is in a function … … 9151 9210 scope = scope.getParent(); 9152 9211 if (scope != null) { 9153 if (scope.getName() == "") // Macro9212 if (scope.getName().equals("")) // Macro 9154 9213 return ErrorT.TYPE; 9155 9214 } else { … … 9472 9531 } 9473 9532 9474 if( reanalyze || !logger.alreadyConsidered (headerName, header.getGeneric(1)))9533 if( reanalyze || !logger.alreadyConsideredTree(headerName, header.getGeneric(1))) 9475 9534 dispatch(header.getGeneric(1)); 9476 9535 … … 9486 9545 } 9487 9546 9488 if( reanalyze || !logger.alreadyConsidered (headerName, header.getGeneric(1)))9547 if( reanalyze || !logger.alreadyConsideredTree(headerName, header.getGeneric(1))) 9489 9548 dispatch(header.getGeneric(1)); 9490 9549 -
trunk/cpp_analysis/src/xtc/lang/c4/DBHandler.java
r172 r196 59 59 /** A Prepared to select nodes with the same hash signature **/ 60 60 PreparedStatement query; 61 /** A PreparedStatement to insert new trees in the database **/ 62 PreparedStatement newMacro; 63 /** A Prepared to find whether the macrodefinition associated with a particular location remains the same 64 * with the same hash signature **/ 65 PreparedStatement queryMacro; 66 /** A PreparedStatement to insert new funcCall in the database **/ 67 PreparedStatement newFuncCall; 68 /** A Prepared to find whether the function definition associated with a particular location remains the same 69 * with the same hash signature **/ 70 PreparedStatement queryFuncCall; 61 71 /** A Prepared to update the value of the convertibility for IfSections defined with of a config option **/ 62 72 PreparedStatement convUpdateBeforeSection; … … 119 129 query = conn.prepareStatement("select node from treesMD5 where hash==? ;"); 120 130 131 //Table to avoid recounting the same macro twice when analyzing 132 //Keeps track of the macroDefinition signature associated with one refLocation 133 stmt.executeUpdate("create table if not exists macrosMD5 (id INTEGER PRIMARY KEY ASC, " + 134 "hashDef BINARY[16], macroName TEXT, refLocation TEXT); "); 135 newMacro = conn.prepareStatement( 136 "insert into macrosMD5(hashDef, macroName, refLocation) values(?, ?, ?);"); 137 queryMacro = conn.prepareStatement("select macroName from macrosMD5 where (hashDef==? AND refLocation==?) ;"); 138 139 //Table to avoid recounting the same funcCall twice when analyzing 140 //Keeps track of the macroDefinition signature associated with one refLocation 141 stmt.executeUpdate("create table if not exists funcCallMD5 (id INTEGER PRIMARY KEY ASC, " + 142 "hashDef BINARY[16], funcName TEXT, callLocation TEXT); "); 143 newFuncCall = conn.prepareStatement( 144 "insert into funcCallMD5 (hashDef, funcName, callLocation) values(?, ?, ?);"); 145 queryFuncCall= conn.prepareStatement("select funcName from funcCallMD5 where (hashDef==? AND callLocation==?) ;"); 146 147 121 148 //Table to collect convertible information per location with CONFIG_OPTION 122 149 stmt.executeUpdate("create table if not exists convertibleLocation (id INTEGER PRIMARY KEY ASC, configOption TEXT, kind TEXT " + … … 249 276 //release_db(dbLock); 250 277 251 query.setBytes(1, hash);278 //squery.setBytes(1, hash); 252 279 conn.commit(); 253 280 } catch (SQLException e) { … … 293 320 } 294 321 322 /** 323 * Adding a MacroReference with a new signature for a particular location 324 * @param headerRoot The node representing the root of the header file to add. 325 * @param hName The name of the header file to add. 326 * @param includingName The name of the file including this header file. 327 */ 328 public void addFuncCallTree(byte[] hash, String funcName, String callLocation) { 329 Pair<FileLock, RandomAccessFile> dbLock; 330 try { 331 //newTree.clearBatch(); 332 conn.setAutoCommit(false); 333 if(16 == hash.length ) { 334 newFuncCall.setBytes(1, hash); 335 336 } 337 else { 338 System.err.println("Problem with the MD5 We're exiting..." ); 339 System.exit(0); 340 } 341 342 newFuncCall.setString(2, funcName); 343 newFuncCall.setString(3, callLocation); 344 345 //System.out.println("NEW MACRO " + newMacro); 346 347 //dbLock = lock_db(); 348 newFuncCall.executeUpdate(); 349 //release_db(dbLock); 350 351 conn.commit(); 352 } catch (SQLException e) { 353 System.err.println("Updating error: " + funcName + " at "+ callLocation + " not inserted in the table"); 354 e.printStackTrace(); 355 } 356 } 357 358 /** 359 * Adding a MacroReference with a new signature for a particular location 360 * @param headerRoot The node representing the root of the header file to add. 361 * @param hName The name of the header file to add. 362 * @param includingName The name of the file including this header file. 363 */ 364 public void addMacroReferenceTree(byte[] hash, String macroName, String refLocation) { 365 Pair<FileLock, RandomAccessFile> dbLock; 366 try { 367 //newTree.clearBatch(); 368 conn.setAutoCommit(false); 369 if(16 == hash.length ) { 370 newMacro.setBytes(1, hash); 371 372 } 373 else { 374 System.err.println("Problem with the MD5 We're exiting..." ); 375 System.exit(0); 376 } 377 378 newMacro.setString(2, macroName); 379 newMacro.setString(3, refLocation); 380 381 //System.out.println("NEW MACRO " + newMacro); 382 383 //dbLock = lock_db(); 384 newMacro.executeUpdate(); 385 //release_db(dbLock); 386 387 conn.commit(); 388 } catch (SQLException e) { 389 System.err.println("Updating error: " + macroName + " at "+ refLocation + " not inserted in the table"); 390 e.printStackTrace(); 391 } 392 } 393 394 /**, 395 * Tells whether a particular macroReference refers to the same macroDefinition in the same context 396 * i.e. even if the global context is different the macro definition may not have changed 397 * @param the hash value to look for in the database 398 * @return An ArrayList representing all the GNodes associated with this hash value 399 */ 400 public boolean isThereMacroWithSameHash(byte[] hash, String refLocation) { 401 boolean result = false; 402 try { 403 queryMacro.setBytes(1, hash); 404 queryMacro.setString(2, refLocation); 405 406 ResultSet rs = queryMacro.executeQuery(); 407 408 while (rs.next()) { //We've found a macro with the same signature 409 result = true; 410 break; 411 } 412 413 rs.close(); 414 } catch (Exception e) { 415 e.printStackTrace(); 416 } 417 418 return result; 419 } 420 421 /**, 422 * Tells whether a particular function call refers to the same function Definition in the same context 423 * i.e. even if the global context is different the function definition may not have changed 424 * @param the hash value to look for in the database 425 * @return An ArrayList representing all the GNodes associated with this hash value 426 */ 427 public boolean isThereFuncCallWithSameHash(byte[] hash, String callLocation) { 428 boolean result = false; 429 try { 430 queryFuncCall.setBytes(1, hash); 431 queryFuncCall.setString(2, callLocation); 432 433 ResultSet rs = queryFuncCall.executeQuery(); 434 435 while (rs.next()) { //We've found a macro with the same signature 436 result = true; 437 break; 438 } 439 440 rs.close(); 441 } catch (Exception e) { 442 e.printStackTrace(); 443 } 444 445 return result; 446 } 447 295 448 /**, 296 449 * Returns all the GNodes associated with one particular MD5 digest in the database -
trunk/cpp_analysis/src/xtc/lang/c4/TreeLogger.java
r103 r196 1 1 package xtc.lang.c4; 2 2 3 import java.io.ByteArrayOutputStream;4 import java.io.ObjectOutputStream;5 import java.security.MessageDigest;6 3 import java.util.ArrayList; 7 4 … … 16 13 */ 17 14 DBHandler dbHandler; 15 16 boolean DBGLogger = false; 18 17 19 18 public TreeLogger() { … … 23 22 24 23 25 private boolean compareByteArray(byte [] b1, byte[] b2 ) { 26 if(b1.length != b2.length) 27 return false; 28 for(int i=0; i<b1.length; i++) { 29 if(b1[i] != b2[i]) 30 return false; 31 } 32 return true; 33 } 34 35 36 private byte[] cryptoTest(GNode myNode) { 37 // Create a Message Digest from a Factory method 38 try { 39 MessageDigest md = MessageDigest.getInstance("MD5"); 40 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 41 ObjectOutputStream oos = new ObjectOutputStream(bos); 42 oos.writeObject(myNode.toString()); 43 oos.flush(); 44 oos.close(); 45 bos.close(); 46 47 byte [] data = bos.toByteArray(); 48 md.update(data); 49 50 byte [] dig = md.digest(); 51 52 return dig; 53 54 } catch (Exception e) { 55 // TODO: handle exception 56 e.printStackTrace(); 57 return null; 58 } 59 60 } 24 61 25 62 26 /** … … 82 46 return hash; 83 47 } 48 49 /** 50 * Method to check whether a macro reference referring to the same definition has already been reported 51 * We look at the signature of the definition and the location. That should be unique key 52 * @param macroDef The definition associated with the reference 53 * @param macroName The name of the macro to consider 54 * @param refLocation The Location of the reference 55 * @return <code>true</code> if the file has already been considered 56 */ 57 public boolean alreadyConsideredMacro( GNode macroDef, String macroName, String refLocation) { 58 59 //First we compute the new hash value 60 //int newHash = hashFunction(rootNode); 61 byte[] md5key = C4SignatureUtils.cryptoTest(macroDef); 62 63 64 //We look for the same hash value in the DB 65 if(dbHandler.isThereMacroWithSameHash(md5key, refLocation)) { 66 //We look for potential collisions by comparing the whole trees 67 //We've already considered that macro... 68 //Right now we do not compare the trees. 69 if (DBGLogger) { 70 System.out.println("MACRO " + macroName + " LOCATION " + refLocation); 71 } 72 return true; 73 } else { 74 if (DBGLogger) { 75 System.out.println("ADDING " + macroName); 76 } 77 //At this point, we know that the tree has been already considered 78 //We insert it into the DB 79 dbHandler.addMacroReferenceTree(md5key, macroName, refLocation); 80 return false; 81 } 82 83 } 84 84 85 86 87 /** 88 * Method to check whether a function call referring to the same definition has already been reported 89 * We look at the signature of the definition and the location. That should be unique key 90 * @param macroDef The definition associated with the reference 91 * @param macroName The name of the macro to consider 92 * @param refLocation The Location of the reference 93 * @return <code>true</code> if the file has already been considered 94 */ 95 public boolean alreadyConsideredFuncCall( GNode funcDef, String funcName, String callLocation) { 96 //First we compute the new hash value 97 //int newHash = hashFunction(rootNode); 98 byte[] md5key = C4SignatureUtils.cryptoTest(funcDef); 99 100 101 //We look for the same hash value in the DB 102 if(dbHandler.isThereFuncCallWithSameHash(md5key, callLocation)) { 103 //We look for potential collisions by comparing the whole trees 104 //We've already considered that macro... 105 //Right now we do not compare the trees. 106 if (DBGLogger) { 107 System.out.println("Func " + funcName + " LOCATION " + callLocation); 108 } 109 return true; 110 } else { 111 112 //At this point, we know that the tree has been already considered 113 //We insert it into the DB 114 dbHandler.addFuncCallTree(md5key, funcName, callLocation); 115 return false; 116 } 117 118 } 119 120 85 121 /** 86 122 * Method to check whether a Node representing the root of an header file has already been considered. … … 89 125 * @return <code>true</code> if the file has already been considered 90 126 */ 91 public boolean alreadyConsidered (String name, GNode rootNode) {127 public boolean alreadyConsideredTree(String name, GNode rootNode) { 92 128 //First we compute the new hash value 93 129 //int newHash = hashFunction(rootNode); 94 byte[] md5key = cryptoTest(rootNode);130 byte[] md5key = C4SignatureUtils.cryptoTest(rootNode); 95 131 96 132 … … 98 134 ArrayList<String> matchingTrees = dbHandler.getNodeWithSameHash(md5key); 99 135 //We look for potential collisions by comparing the whole trees 100 if(0 < matchingTrees.size()) { 136 137 if(0 < matchingTrees.size()) { 101 138 //We've already considered that file... 102 139 //Right now we do not compare the trees. 103 return true; 140 //System.out.println("TREE " + name); 141 return true; 104 142 } 105 143 106 //At this point, we know that the tree has been already considered144 //At this point, we know that the tree has not been already considered 107 145 //We insert it into the DB 108 146 dbHandler.addHeaderTree(rootNode, name, md5key); … … 144 182 } 145 183 184 185 186 146 187 /** 147 188 * Gets the dbHandler associate with this treeLogger to be shared with the convertibleLogger
