本文整理汇总了C++中TranslationUnit::getCXTranslationUnit方法的典型用法代码示例。如果您正苦于以下问题:C++ TranslationUnit::getCXTranslationUnit方法的具体用法?C++ TranslationUnit::getCXTranslationUnit怎么用?C++ TranslationUnit::getCXTranslationUnit使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类TranslationUnit
的用法示例。
在下文中一共展示了TranslationUnit::getCXTranslationUnit方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: findReferences
core::Error findReferences(const core::libclang::FileLocation& location,
std::string* pSpelling,
std::vector<core::libclang::FileRange>* pRefs)
{
Cursor cursor = rSourceIndex().referencedCursorForFileLocation(location);
if (!cursor.isValid() || !cursor.isDeclaration())
return Success();
// get it's USR (bail if it doesn't have one)
std::string USR = cursor.getUSR();
if (USR.empty())
return Success();
// determine what translation units to look in -- if this is a package
// then we look throughout all the source code in the package.
if (rCompilationDatabase().hasTranslationUnit(
location.filePath.absolutePath()))
{
// get all translation units to search
std::vector<std::string> files = rCompilationDatabase()
.translationUnits();
// get translation units we've already indexed
std::map<std::string,TranslationUnit> indexedUnits =
rSourceIndex().getIndexedTranslationUnits();
for (const std::string& filename : files)
{
// first look in already indexed translation units
// (this will pickup unsaved files)
std::map<std::string,TranslationUnit>::iterator it =
indexedUnits.find(filename);
if (it != indexedUnits.end())
{
findReferences(USR,
it->second.getCXTranslationUnit(),
pSpelling,
pRefs);
}
else
{
// get the compilation arguments for this file and use them to
// create a temporary translation unit to search
std::vector<std::string> compileArgs =
rCompilationDatabase().compileArgsForTranslationUnit(filename,
true);
if (compileArgs.empty())
continue;
// create temporary index
CXIndex index = libclang::clang().createIndex(
1 /* Exclude PCH */,
(rSourceIndex().verbose() > 0) ? 1 : 0);
// get args in form clang expects
core::system::ProcessArgs argsArray(compileArgs);
// parse the translation unit
CXTranslationUnit tu = libclang::clang().parseTranslationUnit(
index,
filename.c_str(),
argsArray.args(),
gsl::narrow_cast<int>(argsArray.argCount()),
nullptr, 0, // no unsaved files
CXTranslationUnit_None |
CXTranslationUnit_Incomplete);
// find references
findReferences(USR, tu, pSpelling, pRefs);
// dispose translation unit and index
libclang::clang().disposeTranslationUnit(tu);
libclang::clang().disposeIndex(index);
}
}
}
// not a package, just search locally
else
{
TranslationUnit tu = rSourceIndex().getTranslationUnit(
location.filePath.absolutePath(),
true);
if (!tu.empty())
findReferences(USR, tu.getCXTranslationUnit(), pSpelling, pRefs);
}
return Success();
}