跳转到内容

ROSE 编译器框架/教程

来自维基教科书,开放世界中的开放书籍

调试 SgNodes

[编辑 | 编辑源代码]
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

来自 [email protected] 邮件列表 (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."

过滤 SgNodes

[编辑 | 编辑源代码]

检查是否一个SgNode位于用户指定的地址(ROSE API 中未定义此函数;您需要自己实现)

 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;
  }

检查是否一个SgNode不在用户指定的地址(ROSE API 中未定义此函数;您需要自己实现)

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

获取所有SgFunctionDeclaration位于用户指定的地址的节点

   // 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;
   }

遍历文件

[编辑 | 编辑源代码]

使用以下方法遍历仅命名输入文件(不包括头文件)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;
}

生成 DOT 文件

[编辑 | 编辑源代码]

utility_functions.h定义generateDOT(SgProject* project).

处理自定义命令行选项

[编辑 | 编辑源代码]

待办事项

华夏公益教科书