void StaticConstructorTraversal::visit(SgNode *n) { // Get declared variables SgInitializedName *vName = isSgInitializedName(n); if (vName && !isAcreIgnore(vName->get_declaration())) { Sg_File_Info *fInfo = vName->get_file_info(); SgScopeStatement *scope = vName->get_scope(); // Find global variables (variables in namespaces count, e.g. std) if (!fInfo->isCompilerGenerated() && (isSgGlobal(scope) || isSgNamespaceDefinitionStatement(scope))) { // Walk typedefs until reach pointer to base type SgTypedefType *tdType = isSgTypedefType(vName->get_type()); while (tdType && isSgTypedefType(tdType->get_base_type())) tdType = isSgTypedefType(tdType->get_base_type()); // Determine if type is a class (i.e. type with a constructor) SgClassType *cType = isSgClassType(vName->get_type()); if (tdType) cType = isSgClassType(tdType->get_base_type()); // Output location of globals with a static constructor if (cType) { *out << "Static Constructor Violation: " << fInfo->get_filename() << " @ " << fInfo->get_line() << "\n"; } } } }
bool ClangToSageTranslator::VisitTypedefDecl(clang::TypedefDecl * typedef_decl, SgNode ** node) { #if DEBUG_VISIT_DECL std::cerr << "ClangToSageTranslator::VisitTypedefDecl" << std::endl; #endif bool res = true; SgName name(typedef_decl->getNameAsString()); SgType * type = buildTypeFromQualifiedType(typedef_decl->getUnderlyingType()); SgTypedefDeclaration * sg_typedef_decl = SageBuilder::buildTypedefDeclaration_nfi(name, type, SageBuilder::topScopeStack()); if (isSgClassType(type)) { std::map<SgClassType *, bool>::iterator bool_it = p_class_type_decl_first_see_in_type.find(isSgClassType(type)); ROSE_ASSERT(bool_it != p_class_type_decl_first_see_in_type.end()); if (bool_it->second) { sg_typedef_decl->set_declaration(isSgNamedType(type)->get_declaration()->get_definingDeclaration()); sg_typedef_decl->set_typedefBaseTypeContainsDefiningDeclaration(true); } } else if (isSgEnumType(type)) { std::map<SgEnumType *, bool>::iterator bool_it = p_enum_type_decl_first_see_in_type.find(isSgEnumType(type)); ROSE_ASSERT(bool_it != p_enum_type_decl_first_see_in_type.end()); if (bool_it->second) { sg_typedef_decl->set_declaration(isSgEnumType(type)->get_declaration()->get_definingDeclaration()); sg_typedef_decl->set_typedefBaseTypeContainsDefiningDeclaration(true); } } *node = sg_typedef_decl; return VisitTypedefNameDecl(typedef_decl, node) && res; }
ffi_type *getFFIType(SgType *t) { t = t->stripTypedefsAndModifiers(); switch (t->variantT()) { case V_SgTypeBool: return &ffi_type_uchar; case V_SgTypeChar: return &ffi_type_schar; case V_SgTypeDouble: return &ffi_type_double; case V_SgTypeFloat: return &ffi_type_float; case V_SgTypeInt: return &ffi_type_sint; case V_SgTypeLongDouble: return &ffi_type_longdouble; case V_SgTypeLong: return &ffi_type_slong; case V_SgTypeLongLong: return &ffi_type_sint64; /* NOTE: inaccurate */ case V_SgTypeShort: return &ffi_type_sshort; case V_SgTypeUnsignedChar: return &ffi_type_uchar; case V_SgTypeUnsignedInt: return &ffi_type_uint; case V_SgTypeUnsignedLongLong: return &ffi_type_uint64; /* NOTE: inaccurate */ case V_SgTypeUnsignedLong: return &ffi_type_ulong; case V_SgTypeUnsignedShort: return &ffi_type_ushort; case V_SgTypeVoid: return &ffi_type_void; case V_SgClassType: return getFFIClassType(isSgClassType(t)); case V_SgArrayType: return &ffi_type_pointer; case V_SgPointerType: return &ffi_type_pointer; default: throw InterpError("Encountered unsupported type: " + t->class_name()); } }
int main(int argc, char* argv[]) { SgProject* proj = frontend(argc,argv); SgFunctionDeclaration* mainDecl = SageInterface::findMain(proj); SgFunctionDefinition* mainDef = mainDecl->get_definition(); // std::vector<SgNode*> dotExps = NodeQuery::querySubTree(mainDef, V_SgDotExp); std::vector<SgNode*> varRefs = NodeQuery::querySubTree(mainDef,V_SgVarRefExp); int classExps = 0; for (unsigned int i = 0; i < varRefs.size(); i++) { if (isSgClassType(isSgVarRefExp(varRefs[i])->get_type())) { SgClassType* ct = isSgClassType(isSgVarRefExp(varRefs[i])->get_type()); std::cout << "name of ref: " << isSgVarRefExp(varRefs[i])->get_symbol()->get_name().getString() << std::endl; if (SageInterface::isStructType(ct)) { SgDeclarationStatement* decl = isSgType(ct)->getAssociatedDeclaration(); SgDeclarationStatement* defining_decl = decl->get_definingDeclaration(); if (!(defining_decl->isNameOnly())) { if (isSgClassDeclaration(defining_decl)) { if (isSgClassDeclaration(defining_decl)->get_definition()) { SgDeclarationStatementPtrList member_stats = isSgClassDeclaration(defining_decl)->get_definition()->get_members(); SgDeclarationStatementPtrList::iterator j = member_stats.begin(); for (; j != member_stats.end(); j++) { SgDeclarationStatement* d = isSgDeclarationStatement(*j); std::cout << "decl stat name: " << d->class_name() << std::endl; SgInitializedNamePtrList init_lst = isSgVariableDeclaration(d)->get_variables(); SgInitializedNamePtrList::iterator k = init_lst.begin(); std::cout << "variables in initialized name ptr list..." << std::endl; for (; k != init_lst.end(); k++) { std::cout << isSgInitializedName(*k)->get_name().getString() << std::endl; std::cout << isSgInitializedName(*k)->get_type()->class_name() << std::endl; } } classExps+=1; } } } } } } std::cout << "num class_exp: " << classExps << std::endl; return 0; }
const SgClassDeclaration *Type::getClassDeclaration() const { if( const SgClassType *ct = isSgClassType(t_) ) { if( const SgDeclarationStatement *dec = ct->get_declaration() ) { if( const SgClassDeclaration *cdec = isSgClassDeclaration( dec ) ) { //std::cout << "SgClassDeclaration: it's a declaration at line " << cdec->get_file_info()->get_line() << std::endl; return cdec; } } } return 0; }
void CompassAnalyses::StaticConstructorInitialization::Traversal:: visit(SgNode* node) { // Test for static initialization of variables of type class, such initializations where they are // static or appear in global scope can be called in an order dependent upon the compiler and this // can lead to subtle bugs in large scale applications. SgVariableDeclaration* variableDeclaration = isSgVariableDeclaration(node); if (variableDeclaration != NULL) { SgInitializedNamePtrList::iterator i = variableDeclaration->get_variables().begin(); while (i != variableDeclaration->get_variables().end()) { SgInitializedName* initializedName = *i; // Check the type and see if it is a class (check for typedefs too) SgType* variableType = initializedName->get_type(); SgClassType *classType = isSgClassType(variableType); if (classType != NULL) { // Now check if this is a global or namespace variable or an static class member // This might also have to be a test for other scopes as well. SgScopeStatement* scope = variableDeclaration->get_scope(); if (isSgGlobal(scope) != NULL || isSgNamespaceDefinitionStatement(scope) != NULL) { // printf ("Found a global variable defining a class = %p \n",initializedName); // variableDeclaration->get_file_info()->display("global variable defining a class"); output->addOutput(new CheckerOutput(initializedName)); } if (isSgClassDefinition(scope) != NULL) { // Now check if it is a static data member if (variableDeclaration->get_declarationModifier().get_storageModifier().isStatic() == true) { // printf ("Found a static data member defining a class = %p \n",initializedName); // variableDeclaration->get_file_info()->display("static data member defining a class"); output->addOutput(new CheckerOutput(initializedName)); } } } // increment though the variables in the declaration (typically just one) i++; } } } //End of the visit function.
void Traversal::visit(SgNode* node) { SgVariableDeclaration* variableDeclaration = isSgVariableDeclaration(node); // Look for variable declarations appearing in global scope! // if (variableDeclaration != NULL && isSgGlobal(variableDeclaration->get_parent()) != NULL) if (variableDeclaration != NULL) { SgInitializedNamePtrList::iterator i = variableDeclaration->get_variables().begin(); while (i != variableDeclaration->get_variables().end()) { SgInitializedName* initializedName = *i; // Check the type and see if it is a class (check for typedefs too) SgType* variableType = initializedName->get_type(); SgClassType *classType = isSgClassType(variableType); if (classType != NULL) { // Now check if this is a global variable or an static class member SgScopeStatement* scope = variableDeclaration->get_scope(); if (isSgGlobal(scope) != NULL) { printf ("Found a global variable defining a class \n"); // variableDeclaration->get_file_info()->display("global variable defining a class"); outputPositionInformation(variableDeclaration); } if (isSgClassDefinition(scope) != NULL) { // Now check if it is a static data member if (variableDeclaration->get_declarationModifier().get_storageModifier().isStatic() == true) { printf ("Found a static data member defining a class \n"); // variableDeclaration->get_file_info()->display("static data member defining a class"); outputPositionInformation(variableDeclaration); } } } // increment though the variables in the declaration (typically just one) i++; } } }
// DQ (8/14/2007): This function does not make sense for Fortran. void UnparseFortran_type::unparseClassType(SgType* type, SgUnparse_Info& info) { // printf ("Inside of UnparserFortran::unparseClassType \n"); SgClassType* class_type = isSgClassType(type); ROSE_ASSERT(class_type != NULL); // DQ (10/7/2004): We need to output just the name when isTypeFirstPart == false and isTypeSecondPart == false // this allows us to handle: "doubleArray* arrayPtr2 = new doubleArray();" if (info.isTypeSecondPart() == false) { // Fortran is not as complex as C++, so we can, at least for now, skip the name qualification! ROSE_ASSERT(class_type != NULL); curprint("TYPE ( "); curprint(class_type->get_name().str()); curprint(" ) "); } }
void ModelBuilder::add(Model::model_t & model, SgType * sg_type) { SgModifierType * modifier_type = isSgModifierType(sg_type); if (modifier_type != NULL) { add(model, modifier_type->get_base_type()); return; } Model::type_t element = Model::build<Model::e_model_type>(); element->node->type = sg_type; SgNamedType * named_type = isSgNamedType(sg_type); SgArrayType * array_type = isSgArrayType(sg_type); SgPointerType * pointer_type = isSgPointerType(sg_type); SgReferenceType * reference_type = isSgReferenceType(sg_type); if (named_type != NULL) { SgClassType * class_type = isSgClassType(named_type); SgEnumType * enum_type = isSgEnumType(named_type); SgTypedefType * typedef_type = isSgTypedefType(named_type); SgDeclarationStatement * decl_stmt = named_type->get_declaration()->get_firstNondefiningDeclaration(); assert(decl_stmt != NULL); SgSymbol * decl_sym = decl_stmt->get_symbol_from_symbol_table(); assert(decl_sym != NULL); if (class_type != NULL) { element->node->kind = Model::node_t<Model::e_model_type>::e_class_type; SgClassSymbol * class_sym = isSgClassSymbol(decl_sym); assert(class_sym != NULL); element->node->base_class = model.lookup_class(class_sym); if (element->node->base_class == NULL) { add(model, class_sym); element->node->base_class = model.lookup_class(class_sym); } assert(element->node->base_class != NULL); } else if (enum_type != NULL) { element->node->kind = Model::node_t<Model::e_model_type>::e_enum_type; SgEnumSymbol * enum_sym = isSgEnumSymbol(decl_sym); assert(enum_sym != NULL); element->node->enum_symbol = enum_sym; } else if (typedef_type != NULL) { element->node->kind = Model::node_t<Model::e_model_type>::e_typedef_type; SgTypedefSymbol * typedef_sym = isSgTypedefSymbol(decl_sym); assert(typedef_sym != NULL); element->node->typedef_symbol = typedef_sym; element->node->base_type = model.lookup_type(typedef_type->get_base_type()); if (element->node->base_type == NULL) { add(model, typedef_type->get_base_type()); element->node->base_type = model.lookup_type(typedef_type->get_base_type()); } assert(element->node->base_type != NULL); } else assert(false); } else if (array_type != NULL) { element->node->kind = Model::node_t<Model::e_model_type>::e_array_type; element->node->base_type = model.lookup_type(array_type->get_base_type()); if (element->node->base_type == NULL) { add(model, array_type->get_base_type()); element->node->base_type = model.lookup_type(array_type->get_base_type()); } assert(element->node->base_type != NULL); } else if (pointer_type != NULL) { element->node->kind = Model::node_t<Model::e_model_type>::e_pointer_type; element->node->base_type = model.lookup_type(pointer_type->get_base_type()); if (element->node->base_type == NULL) { add(model, pointer_type->get_base_type()); element->node->base_type = model.lookup_type(pointer_type->get_base_type()); } assert(element->node->base_type != NULL); } else if (reference_type != NULL) { element->node->kind = Model::node_t<Model::e_model_type>::e_reference_type; element->node->base_type = model.lookup_type(reference_type->get_base_type()); if (element->node->base_type == NULL) { add(model, reference_type->get_base_type()); element->node->base_type = model.lookup_type(reference_type->get_base_type()); } assert(element->node->base_type != NULL); } else { element->node->kind = Model::node_t<Model::e_model_type>::e_native_type; } element->scope->parent.a_namespace = NULL; /// \todo model.types.push_back(element); }
void FixupSelfReferentialMacrosInAST::visit ( SgNode* node ) { // DQ (3/11/2006): Set NULL pointers where we would like to have none. // printf ("In FixupSelfReferentialMacrosInAST::visit(): node = %s \n",node->class_name().c_str()); ROSE_ASSERT(node != NULL); switch (node->variantT()) { case V_SgInitializedName: { SgInitializedName* initializedName = isSgInitializedName(node); ROSE_ASSERT(initializedName != NULL); SgType* type = initializedName->get_type()->stripType(); SgClassType* classType = isSgClassType(type); if (classType != NULL) { SgClassDeclaration* targetClassDeclaration = isSgClassDeclaration(classType->get_declaration()); SgName className = targetClassDeclaration->get_name(); // printf ("In FixupSelfReferentialMacrosInAST::visit(): Found a class declaration name = %s \n",className.str()); // For sudo_exec_pty.c also look for siginfo if (className == "sigaction" || className == "siginfo") { // printf ("In FixupSelfReferentialMacrosInAST::visit(): Found a sigaction type \n"); // Note we could also check that the declaration came from a known header file. SgStatement* associatedStatement = isSgStatement(initializedName->get_parent()); if (associatedStatement != NULL) { // Add a macro to undefine the "#define sa_handler __sigaction_handler.sa_handler" macro. // printf ("In FixupSelfReferentialMacrosInAST::visit(): Add a macro to undefine the macro #define sa_handler __sigaction_handler.sa_handler \n"); // PreprocessingInfo* macro = new PreprocessingInfo(DirectiveType, const std::string & inputString,const std::string & filenameString, int line_no , int col_no,int nol, RelativePositionType relPos ); PreprocessingInfo::DirectiveType directiveType = PreprocessingInfo::CpreprocessorUndefDeclaration; // We are puting out all macros anytime we see either type. This might be too much... // From the sigaction.h file (included by signal.h): addMacro(associatedStatement,"#undef sa_handler\n",directiveType); addMacro(associatedStatement,"#undef sa_sigaction\n",directiveType); // From the siginfo.h file (included by signal.h): addMacro(associatedStatement,"#undef si_pid\n", directiveType); addMacro(associatedStatement,"#undef si_uid\n", directiveType); addMacro(associatedStatement,"#undef si_timerid\n",directiveType); addMacro(associatedStatement,"#undef si_overrun\n",directiveType); addMacro(associatedStatement,"#undef si_status\n", directiveType); addMacro(associatedStatement,"#undef si_utime\n", directiveType); addMacro(associatedStatement,"#undef si_stime\n", directiveType); addMacro(associatedStatement,"#undef si_value\n", directiveType); addMacro(associatedStatement,"#undef si_int\n", directiveType); addMacro(associatedStatement,"#undef si_ptr\n", directiveType); addMacro(associatedStatement,"#undef si_addr\n", directiveType); addMacro(associatedStatement,"#undef si_band\n", directiveType); addMacro(associatedStatement,"#undef si_fd\n", directiveType); } } } } default: { // printf ("Not handled in FixupSelfReferentialMacrosInAST::visit(%s) \n",node->class_name().c_str()); } } }
/** * Const-qualify immutable objects * * \todo count assignments, if only one, report violation */ bool DCL00_C( const SgNode *node ) { const SgInitializedName *varName = isSgInitializedName(node); if (!varName) return false; /** * Ignore variables generated by macros */ if ((varName->get_name().getString().substr(0,2) == "__") || isCompilerGeneratedNode(node)) return false; /** * Ignore global variables */ if (isGlobalVar(varName)) return false; /** * Ignore variables that are already const, are function pointers, or are * declared inside of a struct, enum, or as an argument to a function */ SgType *varType = varName->get_type(); if (isConstType(varType) || isConstType(varType->dereference()) || isConstType(varType->dereference()->dereference()) || isSgFunctionType(varType) || isSgClassType(varType) || findParentOfType(varName, SgCtorInitializerList) || findParentOfType(varName, SgEnumDeclaration) || findParentOfType(varName, SgClassDeclaration)) return false; /** * DCL13-C is a subset of this rule, figure out which rule we are dealing * with here */ std::string ruleStr; std::string errStr; if (findParentOfType(varName, SgFunctionParameterList)) { /** ignore function prototypes, just worry about the definitions */ const SgFunctionDeclaration *fnDecl = findParentOfType(varName, SgFunctionDeclaration); /** * Disabling assertion due to C++ code */ if (!fnDecl) return false; // assert(fnDecl); if (!fnDecl->get_definition()) return false; if (isSgPointerType(varName->get_type()) || isSgArrayType(varName->get_type())) { ruleStr = "DCL13-C"; errStr = "Declare function parameters that are pointers to values not changed by the function as const: "; } else { return false; } } else { ruleStr = "DCL00-C"; errStr = "Const-qualify immutable objects: "; } /** * Ignore global variables or variables declared as extern */ const SgScopeStatement *varScope = varName->get_scope(); if (isSgGlobal(varScope) || isExternVar(varName)) return false; FOREACH_SUBNODE(varScope, nodes, i, V_SgVarRefExp) { const SgVarRefExp *iVar = isSgVarRefExp(*i); assert(iVar); if (getRefDecl(iVar) != varName) continue; const SgNode *parent = iVar->get_parent(); while(isSgCastExp(parent)) { parent = parent->get_parent(); } assert(parent); /** * If the variable is written to or it's address is taken, we can no * longer be sure it should be const, if it's a struct and gets * dereferenced, who knows what's getting written there :/ */ if (varWrittenTo(iVar) || isSgArrowExp(parent) || findParentOfType(iVar, SgAddressOfOp)) return false; /** * If the variable is a pointer or array, and we pass it to a function * or as an argument to pointer arithmetic, or assign it's value * somewhere, we can longer be sure it should be const */ if ((isSgPointerType(varType) || isSgArrayType(varType)) && (findParentOfType(iVar, SgFunctionCallExp) || isSgAddOp(parent) || isSgSubtractOp(parent) || isSgAssignOp(parent) || isSgPntrArrRefExp(parent) || isSgPointerDerefExp(parent) || isSgAssignInitializer(parent))) return false; } const std::string msg = errStr + varName->get_name().getString(); print_error(node, ruleStr.c_str(), msg.c_str(), true); return true; }
std::string initializeVariable(SgInitializedName* initName) { //if array type we need to get the index expression std::string index_expression_string; std::stringstream nameStringStream; SgName initNameName = initName->get_qualified_name(); SgSymbol* initNameSym = initName->search_for_symbol_from_symbol_table(); if (variablesOfNameX.find(initNameName.getString()) == variablesOfNameX.end()) { nameStringStream << initNameName.getString() << "_0"; variablesOfNameX[initNameName.getString()] = 1; } else { int occurrence = variablesOfNameX[initNameName.getString()]; nameStringStream << initNameName.getString() << "_" << occurrence; variablesOfNameX[initNameName.getString()] = occurrence+1; } SymbolToZ3[initNameSym] = nameStringStream.str(); SymbolToInstances[initNameSym] = 0; SgType* initNameType = initName->get_type(); std::string typeZ3; if (initNameType->isIntegerType()) { typeZ3 = "Int"; } else if (initNameType->isFloatType()) { typeZ3 = "Real"; } else if (isSgArrayType(initNameType)) { SgArrayType* arrTyp = isSgArrayType(initNameType); ROSE_ASSERT(arrTyp != NULL); SgType* underlying_type = arrTyp->get_base_type(); std::string array_typeZ3; if (underlying_type->isIntegerType()) { array_typeZ3 = "Int"; } else if (underlying_type->isFloatType()) { array_typeZ3 = "Real"; } else { std::cout << "unknown underlying type of array!" << std::endl; std::cout << underlying_type->class_name() << std::endl; ROSE_ASSERT(false); } SgExpression* ind = arrTyp->get_index(); std::stringstream arrStr; index_expression_string = getSgExpressionString(ind); typeZ3 = "(Array Int " + array_typeZ3 + ")"; } else if (isSgClassType(initNameType)) { std::cout << "structs are not yet implemented" << std::endl; ROSE_ASSERT(false); } else if (isSgPointerType(initNameType)) { std::cout << "pointers are not yet implemented" << std::endl; ROSE_ASSERT(false); } else if (isSgEnumType(initNameType)) { SgEnumType* et = isSgEnumType(initNameType); SgEnumDeclaration* enum_d = isSgEnumDeclaration(et->getAssociatedDeclaration()); getSgDeclarationStatement(enum_d); typeZ3 = et->get_name().getString(); } else { std::cout << "unknown type: " << initNameType->class_name() << std::endl; ROSE_ASSERT(false); } std::string name = nameStringStream.str() + "_0"; std::stringstream streamZ3; if (isSgArrayType(initNameType)) { streamZ3 << "(declare-const " << name << " " << typeZ3 << ")"; streamZ3 << "\n(declare-fun " << name << "_len () Int)"; streamZ3 << "\n(assert (= " << name << "_len " << index_expression_string << "))"; #ifdef ARRAY_TEST std::cout << "arrStream: " << streamZ3.str() << std::endl; #endif } else if (isSgEnumType(initNameType)) { streamZ3 << "(declare-const " << name << " " << typeZ3 << ")"; } else { streamZ3 << "(declare-fun " << name << " () " << typeZ3 << ")"; } return streamZ3.str(); }
AstParameterizedTypeAttribute::AstParameterizedTypeAttribute(SgNamedType *genericType_) : genericType(genericType_) { isSgClassType(genericType); }
int main(int argc, char **argv) { SgProject *project = frontend(argc, argv); // Instantiate a class hierarchy wrapper. ClassHierarchyWrapper classHierarchy( project ); std::list<SgNode *> nodes2 = NodeQuery::querySubTree(project, V_SgVariableDefinition); for (std::list<SgNode *>::iterator it = nodes2.begin(); it != nodes2.end(); ++it ) { SgNode *n = *it; ROSE_ASSERT(n != NULL); SgVariableDefinition *varDefn = isSgVariableDefinition(n); ROSE_ASSERT(varDefn != NULL); std::cout << "Var defn: " << varDefn->unparseToCompleteString() << std::endl; } std::list<SgNode *> nodes1 = NodeQuery::querySubTree(project, V_SgVariableDeclaration); for (std::list<SgNode *>::iterator it = nodes1.begin(); it != nodes1.end(); ++it ) { SgNode *n = *it; ROSE_ASSERT(n != NULL); SgVariableDeclaration *varDecl = isSgVariableDeclaration(n); ROSE_ASSERT(varDecl != NULL); SgInitializedNamePtrList &variables = varDecl->get_variables(); SgInitializedNamePtrList::iterator varIter; for (varIter = variables.begin(); varIter != variables.end(); ++varIter) { SgNode *var = *varIter; ROSE_ASSERT(var != NULL); SgInitializedName *initName = isSgInitializedName(var); ROSE_ASSERT(initName != NULL); if ( isSgClassType(initName->get_type()) ) { SgClassType *classType = isSgClassType(initName->get_type()); ROSE_ASSERT(classType != NULL); SgDeclarationStatement *declStmt = classType->get_declaration(); ROSE_ASSERT(declStmt != NULL); SgClassDeclaration *classDeclaration = isSgClassDeclaration(declStmt); ROSE_ASSERT(classDeclaration != NULL); // std::cout << "From var decl got: " << classDeclaration->unparseToCompleteString() << std::endl; SgClassDefinition *classDefinition = classDeclaration->get_definition(); if ( classDefinition != NULL ) { std::cout << "From var decl got: " << classDefinition->unparseToCompleteString() << std::endl; } } } } std::list<SgNode *> nodes = NodeQuery::querySubTree(project, V_SgClassDeclaration); for (std::list<SgNode *>::iterator it = nodes.begin(); it != nodes.end(); ++it ) { SgNode *n = *it; ROSE_ASSERT(n != NULL); SgClassDeclaration *classDeclaration1 = isSgClassDeclaration(n); ROSE_ASSERT(classDeclaration1 != NULL); SgDeclarationStatement *definingDecl = classDeclaration1->get_definingDeclaration(); if ( definingDecl == NULL ) continue; SgClassDeclaration *classDeclaration = isSgClassDeclaration(definingDecl); ROSE_ASSERT(classDeclaration != NULL); SgClassDefinition *classDefinition = classDeclaration->get_definition(); ROSE_ASSERT(classDefinition != NULL); std::cout << "Calling getSubclasses on " << classDefinition->unparseToCompleteString() << std::endl; SgClassDefinitionPtrList subclasses = classHierarchy.getSubclasses(classDefinition); // Iterate over all subclasses. for (SgClassDefinitionPtrList::iterator subclassIt = subclasses.begin(); subclassIt != subclasses.end(); ++subclassIt) { SgClassDefinition *subclass = *subclassIt; ROSE_ASSERT(subclass != NULL); std::cout << "subclass" << std::endl; } } #if 0 #if 0 std::list<SgNode *> nodes = NodeQuery::querySubTree(project, V_SgClassDefinition); for (std::list<SgNode *>::iterator it = nodes.begin(); it != nodes.end(); ++it ) { SgNode *n = *it; ROSE_ASSERT(n != NULL); SgClassDefinition *classDefinition = isSgClassDefinition(n); ROSE_ASSERT(classDefinition != NULL); std::cout << "Calling getSubclasses on " << classDefinition->unparseToCompleteString() << std::endl; SgClassDefinitionPtrList subclasses = classHierarchy.getSubclasses(classDefinition); // Iterate over all subclasses. for (SgClassDefinitionPtrList::iterator subclassIt = subclasses.begin(); subclassIt != subclasses.end(); ++subclassIt) { SgClassDefinition *subclass = *subclassIt; ROSE_ASSERT(subclass != NULL); std::cout << "subclass" << std::endl; } } #else // Collect all function/method invocations. std::list<SgNode *> nodes = NodeQuery::querySubTree(project, V_SgFunctionCallExp); unsigned int numCallSites = 0; unsigned int numMonomorphicCallSites = 0; unsigned int numPossibleResolutions = 0; // Visit each call site. for (std::list<SgNode *>::iterator it = nodes.begin(); it != nodes.end(); ++it ) { SgNode *n = *it; ROSE_ASSERT(n != NULL); SgFunctionCallExp *functionCallExp = isSgFunctionCallExp(n); ROSE_ASSERT(functionCallExp != NULL); // We are only interested in examining method invocations. bool isDotExp = false; if ( !isMethodCall(functionCallExp, isDotExp) ) continue; numCallSites++; // Certainly can be resolved to the static method. numPossibleResolutions++; if ( isDotExp ) { // If this is a dot expression (i.e., a.foo()), we can // statically determine its type. numMonomorphicCallSites++; continue; } // Retrieve the static function declaration. SgFunctionDeclaration *functionDeclaration = getFunctionDeclaration(functionCallExp); // Ensure it is actually a method declaration. SgMemberFunctionDeclaration *memberFunctionDeclaration = isSgMemberFunctionDeclaration(functionDeclaration); ROSE_ASSERT(memberFunctionDeclaration != NULL); unsigned int numOverridesForMethod = 0; if ( ( isVirtual(functionDeclaration) ) || ( isDeclaredVirtualWithinAncestor(functionDeclaration) ) ) { SgClassDefinition *classDefinition = isSgClassDefinition(memberFunctionDeclaration->get_scope()); ROSE_ASSERT(classDefinition != NULL); SgClassDefinitionPtrList subclasses = classHierarchy.getSubclasses(classDefinition); // Iterate over all subclasses. for (SgClassDefinitionPtrList::iterator subclassIt = subclasses.begin(); subclassIt != subclasses.end(); ++subclassIt) { SgClassDefinition *subclass = *subclassIt; ROSE_ASSERT(subclass != NULL); std::cout << "subclass" << std::endl; // Iterate over all of the methods defined in this subclass. SgDeclarationStatementPtrList &decls = subclass->get_members(); for (SgDeclarationStatementPtrList::iterator declIter = decls.begin(); declIter != decls.end(); ++declIter) { SgDeclarationStatement *declStmt = *declIter; ROSE_ASSERT(declStmt != NULL); SgMemberFunctionDeclaration *method = isSgMemberFunctionDeclaration(declStmt); if ( method == NULL ) { continue; } // Determine whether subclass of the class defining this // method overrides the method. if ( methodOverridesVirtualMethod(method, memberFunctionDeclaration) ) { numOverridesForMethod++; } } } if ( numOverridesForMethod == 0 ) numMonomorphicCallSites++; numPossibleResolutions += numOverridesForMethod; std::cout << "Method invocation has " << numOverridesForMethod + 1 << " possible resolutions " << std::endl; std::cout << functionCallExp->unparseToCompleteString() << std::endl; } } #endif #endif return 0; }
void FixupSelfReferentialMacrosInAST::visit ( SgNode* node ) { // DQ (3/11/2006): Set NULL pointers where we would like to have none. // printf ("In FixupSelfReferentialMacrosInAST::visit(): node = %s \n",node->class_name().c_str()); ROSE_ASSERT(node != NULL); switch (node->variantT()) { case V_SgInitializedName: { SgInitializedName* initializedName = isSgInitializedName(node); ROSE_ASSERT(initializedName != NULL); SgType* type = initializedName->get_type()->stripType(); SgClassType* classType = isSgClassType(type); if (classType != NULL) { SgClassDeclaration* targetClassDeclaration = isSgClassDeclaration(classType->get_declaration()); SgName className = targetClassDeclaration->get_name(); // printf ("In FixupSelfReferentialMacrosInAST::visit(): Found a class declaration name = %s \n",className.str()); // For sudo_exec_pty.c also look for siginfo if (className == "sigaction" || className == "siginfo") { // printf ("In FixupSelfReferentialMacrosInAST::visit(): Found a sigaction type \n"); // Note we could also check that the declaration came from a known header file. SgStatement* associatedStatement = isSgStatement(initializedName->get_parent()); if (associatedStatement != NULL) { // Add a macro to undefine the "#define sa_handler __sigaction_handler.sa_handler" macro. // printf ("In FixupSelfReferentialMacrosInAST::visit(): Add a macro to undefine the macro #define sa_handler __sigaction_handler.sa_handler \n"); // PreprocessingInfo* macro = new PreprocessingInfo(DirectiveType, const std::string & inputString,const std::string & filenameString, int line_no , int col_no,int nol, RelativePositionType relPos ); PreprocessingInfo::DirectiveType directiveType = PreprocessingInfo::CpreprocessorUndefDeclaration; // We are puting out all macros anytime we see either type. This might be too much... // From the sigaction.h file (included by signal.h): addMacro(associatedStatement,"#undef sa_handler\n",directiveType); addMacro(associatedStatement,"#undef sa_sigaction\n",directiveType); // From the siginfo.h file (included by signal.h): addMacro(associatedStatement,"#undef si_pid\n", directiveType); addMacro(associatedStatement,"#undef si_uid\n", directiveType); addMacro(associatedStatement,"#undef si_timerid\n",directiveType); addMacro(associatedStatement,"#undef si_overrun\n",directiveType); addMacro(associatedStatement,"#undef si_status\n", directiveType); addMacro(associatedStatement,"#undef si_utime\n", directiveType); addMacro(associatedStatement,"#undef si_stime\n", directiveType); addMacro(associatedStatement,"#undef si_value\n", directiveType); addMacro(associatedStatement,"#undef si_int\n", directiveType); addMacro(associatedStatement,"#undef si_ptr\n", directiveType); addMacro(associatedStatement,"#undef si_addr\n", directiveType); addMacro(associatedStatement,"#undef si_band\n", directiveType); addMacro(associatedStatement,"#undef si_fd\n", directiveType); } } } } default: { // printf ("Not handled in FixupSelfReferentialMacrosInAST::visit(%s) \n",node->class_name().c_str()); } } #if 0 // DQ (12/30/2013): Comments and CPP directives have not yet been attached to the AST, so we can't process them here. // SgLocatedNode* locatedNode = isSgLocatedNode(node); // if (locatedNode != NULL) SgStatement* stmt = isSgStatement(node); if (stmt != NULL) { // Find all #define statements and look for self referencing macros int numberOfComments = -1; if (stmt->getAttachedPreprocessingInfo() != NULL) numberOfComments = stmt->getAttachedPreprocessingInfo()->size(); std::string s = std::string(" --- startOfConstruct: file = " ) + stmt->get_startOfConstruct()->get_filenameString() + " raw filename = " + stmt->get_startOfConstruct()->get_raw_filename() + " raw line = " + StringUtility::numberToString(stmt->get_startOfConstruct()->get_raw_line()) + " raw column = " + StringUtility::numberToString(stmt->get_startOfConstruct()->get_raw_col()) + " #comments = " + StringUtility::numberToString(numberOfComments) + " \n "; AttachedPreprocessingInfoType* comments = stmt->getAttachedPreprocessingInfo(); if (comments != NULL) { printf ("Found attached comments (at %p of type: %s): \n",stmt,stmt->class_name().c_str()); AttachedPreprocessingInfoType::iterator i; for (i = comments->begin(); i != comments->end(); i++) { ROSE_ASSERT ( (*i) != NULL ); printf (" Attached Comment (relativePosition=%s): %s\n", ((*i)->getRelativePosition() == PreprocessingInfo::before) ? "before" : "after", (*i)->getString().c_str()); printf ("Comment/Directive getNumberOfLines = %d getColumnNumberOfEndOfString = %d \n",(*i)->getNumberOfLines(),(*i)->getColumnNumberOfEndOfString()); (*i)->get_file_info()->display("comment/directive location"); } } else { printf ("No attached comments (at %p of type: %s): \n",stmt,stmt->class_name().c_str()); } } #endif }
bool ClangToSageTranslator::VisitVarDecl(clang::VarDecl * var_decl, SgNode ** node) { #if DEBUG_VISIT_DECL std::cerr << "ClangToSageTranslator::VisitVarDecl" << std::endl; #endif bool res = true; // Create the SAGE node: SgVariableDeclaration SgName name(var_decl->getNameAsString()); SgType * type = buildTypeFromQualifiedType(var_decl->getType()); clang::Expr * init_expr = var_decl->getInit(); SgNode * tmp_init = Traverse(init_expr); SgExpression * expr = isSgExpression(tmp_init); if (tmp_init != NULL && expr == NULL) { std::cerr << "Runtime error: not a SgInitializer..." << std::endl; // TODO res = false; } SgExprListExp * expr_list_expr = isSgExprListExp(expr); SgInitializer * init = NULL; if (expr_list_expr != NULL) init = SageBuilder::buildAggregateInitializer(expr_list_expr, type); else if (expr != NULL) init = SageBuilder::buildAssignInitializer_nfi(expr, expr->get_type()); if (init != NULL) applySourceRange(init, init_expr->getSourceRange()); SgVariableDeclaration * sg_var_decl = new SgVariableDeclaration(name, type, init); // scope: obtain from the scope stack. if (isSgClassType(type)) { std::map<SgClassType *, bool>::iterator bool_it = p_class_type_decl_first_see_in_type.find(isSgClassType(type)); ROSE_ASSERT(bool_it != p_class_type_decl_first_see_in_type.end()); if (bool_it->second) { sg_var_decl->set_baseTypeDefiningDeclaration(isSgNamedType(type)->get_declaration()->get_definingDeclaration()); sg_var_decl->set_variableDeclarationContainsBaseTypeDefiningDeclaration(true); } } else if (isSgEnumType(type)) { std::map<SgEnumType *, bool>::iterator bool_it = p_enum_type_decl_first_see_in_type.find(isSgEnumType(type)); ROSE_ASSERT(bool_it != p_enum_type_decl_first_see_in_type.end()); if (bool_it->second) { sg_var_decl->set_baseTypeDefiningDeclaration(isSgEnumType(type)->get_declaration()->get_definingDeclaration()); sg_var_decl->set_variableDeclarationContainsBaseTypeDefiningDeclaration(true); } } sg_var_decl->set_firstNondefiningDeclaration(sg_var_decl); sg_var_decl->set_parent(SageBuilder::topScopeStack()); ROSE_ASSERT(sg_var_decl->get_variables().size() == 1); SgInitializedName * init_name = sg_var_decl->get_variables()[0]; ROSE_ASSERT(init_name != NULL); init_name->set_scope(SageBuilder::topScopeStack()); applySourceRange(init_name, var_decl->getSourceRange()); SgVariableDefinition * var_def = isSgVariableDefinition(init_name->get_declptr()); ROSE_ASSERT(var_def != NULL); applySourceRange(var_def, var_decl->getSourceRange()); SgVariableSymbol * var_symbol = new SgVariableSymbol(init_name); SageBuilder::topScopeStack()->insert_symbol(name, var_symbol); *node = sg_var_decl; return VisitDeclaratorDecl(var_decl, node) && res; }
void FixupSelfReferentialMacrosInAST::visit ( SgNode* node ) { // DQ (3/11/2006): Set NULL pointers where we would like to have none. // printf ("In FixupSelfReferentialMacrosInAST::visit(): node = %s \n",node->class_name().c_str()); ROSE_ASSERT(node != NULL); switch (node->variantT()) { case V_SgInitializedName: { SgInitializedName* initializedName = isSgInitializedName(node); ROSE_ASSERT(initializedName != NULL); SgType* type = initializedName->get_type()->stripType(); SgClassType* classType = isSgClassType(type); if (classType != NULL) { SgClassDeclaration* targetClassDeclaration = isSgClassDeclaration(classType->get_declaration()); SgName className = targetClassDeclaration->get_name(); // printf ("In FixupSelfReferentialMacrosInAST::visit(): Found a class declaration name = %s \n",className.str()); if (className == "sigaction") { // printf ("In FixupSelfReferentialMacrosInAST::visit(): Found a sigaction type \n"); // Note we could also check that the declaration came from a known header file. SgStatement* associatedStatement = isSgStatement(initializedName->get_parent()); if (associatedStatement != NULL) { // Add a macro to undefine the "#define sa_handler __sigaction_handler.sa_handler" macro. // printf ("In FixupSelfReferentialMacrosInAST::visit(): Add a macro to undefine the macro #define sa_handler __sigaction_handler.sa_handler \n"); // PreprocessingInfo* macro = new PreprocessingInfo(DirectiveType, const std::string & inputString,const std::string & filenameString, int line_no , int col_no,int nol, RelativePositionType relPos ); PreprocessingInfo::DirectiveType directiveType = PreprocessingInfo::CpreprocessorUndefDeclaration; std::string macroString = "#undef sa_handler\n"; std::string filenameString = "macro_call_fixupSelfReferentialMacrosInAST"; int line_no = 1; int col_no = 1; int nol = 1; PreprocessingInfo::RelativePositionType relPos = PreprocessingInfo::before; PreprocessingInfo* macro = new PreprocessingInfo(directiveType,macroString,filenameString,line_no,col_no,nol,relPos); // printf ("Attaching CPP directive %s to IR node %p as attributes. \n",PreprocessingInfo::directiveTypeName(macro->getTypeOfDirective()).c_str(),associatedStatement); associatedStatement->addToAttachedPreprocessingInfo(macro); #if 0 printf ("Exiting as a test! \n"); ROSE_ASSERT(false); #endif } } } } default: { // printf ("Not handled in FixupSelfReferentialMacrosInAST::visit(%s) \n",node->class_name().c_str()); } } }
bool ClangToSageTranslator::VisitFieldDecl(clang::FieldDecl * field_decl, SgNode ** node) { #if DEBUG_VISIT_DECL std::cerr << "ClangToSageTranslator::VisitFieldDecl" << std::endl; #endif bool res = true; SgName name(field_decl->getNameAsString()); SgType * type = buildTypeFromQualifiedType(field_decl->getType()); clang::Expr * init_expr = field_decl->getInClassInitializer(); SgNode * tmp_init = Traverse(init_expr); SgExpression * expr = isSgExpression(tmp_init); // TODO expression list if aggregated initializer ! if (tmp_init != NULL && expr == NULL) { std::cerr << "Runtime error: not a SgInitializer..." << std::endl; res = false; } SgInitializer * init = expr != NULL ? SageBuilder::buildAssignInitializer_nfi(expr, expr->get_type()) : NULL; if (init != NULL) applySourceRange(init, init_expr->getSourceRange()); // Cannot use 'SageBuilder::buildVariableDeclaration' because of anonymous field // *node = SageBuilder::buildVariableDeclaration(name, type, init, SageBuilder::topScopeStack()); // Build it by hand... SgVariableDeclaration * var_decl = new SgVariableDeclaration(name, type, init); if (isSgClassType(type)) { std::map<SgClassType *, bool>::iterator bool_it = p_class_type_decl_first_see_in_type.find(isSgClassType(type)); ROSE_ASSERT(bool_it != p_class_type_decl_first_see_in_type.end()); if (bool_it->second) { var_decl->set_baseTypeDefiningDeclaration(isSgNamedType(type)->get_declaration()->get_definingDeclaration()); var_decl->set_variableDeclarationContainsBaseTypeDefiningDeclaration(true); } } else if (isSgEnumType(type)) { std::map<SgEnumType *, bool>::iterator bool_it = p_enum_type_decl_first_see_in_type.find(isSgEnumType(type)); ROSE_ASSERT(bool_it != p_enum_type_decl_first_see_in_type.end()); if (bool_it->second) { var_decl->set_baseTypeDefiningDeclaration(isSgEnumType(type)->get_declaration()->get_definingDeclaration()); var_decl->set_variableDeclarationContainsBaseTypeDefiningDeclaration(true); } } var_decl->set_firstNondefiningDeclaration(var_decl); var_decl->set_parent(SageBuilder::topScopeStack()); ROSE_ASSERT(var_decl->get_variables().size() == 1); SgInitializedName * init_name = var_decl->get_variables()[0]; ROSE_ASSERT(init_name != NULL); init_name->set_scope(SageBuilder::topScopeStack()); applySourceRange(init_name, field_decl->getSourceRange()); SgVariableDefinition * var_def = isSgVariableDefinition(init_name->get_declptr()); ROSE_ASSERT(var_def != NULL); applySourceRange(var_def, field_decl->getSourceRange()); SgVariableSymbol * var_symbol = new SgVariableSymbol(init_name); SageBuilder::topScopeStack()->insert_symbol(name, var_symbol); *node = var_decl; return VisitDeclaratorDecl(field_decl, node) && res; }
bool Type::isClass() const { return isSgClassType(t_) != 0; }