ROSE Compiler Framework/Tutorial

Debugging SgNodes edit

SgNode* node = /* initialized */;

node->variantT() // Get node type as Enumeration
getVariantName(node->variantT()) // Get as string

SageInterface::get_name(...) // Get a useful name to describe the SgNode

From rose-public@nersc.gov mailing list (https://mailman.nersc.gov/pipermail/rose-public/2012-August/001786.html)

unparseToString() needs full scope information to work properly (thinking about C++
name qualification and other nasty things) It does fail on dangling AST pieces.

A workaround it is
1)to call it AFTER the AST pieces are attached to somewhere.
2) call other member functions like ->class_name(), ->get_name() etc for debugging.

We had discussion to relax it to handle partial AST. But it never made into our priority
list."

Filtering SgNodes edit

Check if an SgNode is in a user-specified location (function not defined in ROSE API yet; you need to implement yourself):

 bool
 IsNodeInUserLocation(const SgLocatedNode *const node, const std::string& path)
  {
   std::string filename = node->getFilenameString();
          
   StringUtility::FileNameClassification file_classification =
     StringUtility::classifyFileName(filename, path);

   StringUtility::FileNameLocation file_location =
     file_classification.getLocation();
                        
   return StringUtility::FILENAME_LOCATION_USER == file_location;
  }

Check if an SgNode is NOT in a user-specified location (function not defined in ROSE API yet; you need to implement yourself:

 bool IsNodeNotInUserLocation(const SgNode* node)
 {
   const SgLocatedNode* located_node = isSgLocatedNode(node);
   if (located_node != NULL)
   {
     return ! IsNodeInUserLocation(
           located_node,
           source_directory);
   }
   else
   {
     return true;
   }
 };

Get all SgFunctionDeclaration nodes in a user-specified location:

   // Function Declarations

   // Use NodeQuery to grab all SgFunctionDeclaration nodes
   SgProject * project = <initialize>;
   static Rose_STL_Container<SgNode*> function_decls =
     NodeQuery::querySubTree(project,
                 V_SgFunctionDeclaration);

   // Create iterators
   Rose_STL_Container<SgNode*>::iterator ibegin_function_decls =
     function_decls.begin();
   Rose_STL_Container<SgNode*>::iterator iend_function_decls =
     function_decls.end();

   // Remove SgFunctionDeclaration nodes that aren't in the
   // user-specified location
   iend_function_decls =
     remove_if(ibegin_function_decls,
          iend_function_decls,
          IsNodeNotInUserLocation);

   // Check what we've found...
   for ( ;
      ibegin_function_decls != iend_function_decls;
      ++ibegin_function_decls)
   {
     std::cout << (*ibegin_function_decls)->unparseToString() << std::endl;
   }

Traversing files edit

Traverse only the named input file(s) (excluding header files) using traverseInputFiles:

int main(int argc, char** argv)
{
  SgProject* project = new SgProject(argc, argv);
  Traversal traversal; // you need to define this
  traversal.traverseInputFiles(project);
  return 0;
}

Generating DOT files edit

utility_functions.h defines generateDOT(SgProject* project).

Processing Custom Commandline Options edit

TODO: