跳轉到內容

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

來自 rose-public@nersc.gov 郵件列表 (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).

處理自定義命令列選項

[編輯 | 編輯原始碼]

待辦事項

華夏公益教科書