本文整理汇总了C++中document::Ptr::parse方法的典型用法代码示例。如果您正苦于以下问题:C++ Ptr::parse方法的具体用法?C++ Ptr::parse怎么用?C++ Ptr::parse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类document::Ptr
的用法示例。
在下文中一共展示了Ptr::parse方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: test_codegen_definition_middle_member
void CppToolsPlugin::test_codegen_definition_middle_member()
{
const QByteArray srcText = "\n"
"class Foo\n" // line 1
"{\n"
"void foo();\n" // line 3
"void bar();\n" // line 4
"void car();\n" // line 5
"};\n"
"\n";
const QByteArray dstText = QString::fromLatin1(
"\n"
"#include \"%1/file.h\"\n" // line 1
"int x;\n"
"\n"
"void Foo::foo()\n" // line 4
"{\n"
"\n"
"}\n" // line 7
"\n"
"void Foo::car()\n" // line 9
"{\n"
"\n"
"}\n"
"\n"
"int y;\n").arg(QDir::tempPath()).toLatin1();
Document::Ptr src = Document::create(QDir::tempPath() + QLatin1String("/file.h"));
Utils::FileSaver srcSaver(src->fileName());
srcSaver.write(srcText);
srcSaver.finalize();
src->setUtf8Source(srcText);
src->parse();
src->check();
QCOMPARE(src->diagnosticMessages().size(), 0);
QCOMPARE(src->globalSymbolCount(), 1U);
Document::Ptr dst = Document::create(QDir::tempPath() + QLatin1String("/file.cpp"));
dst->addIncludeFile(Document::Include(QLatin1String("file.h"), src->fileName(), 1,
Client::IncludeLocal));
Utils::FileSaver dstSaver(dst->fileName());
dstSaver.write(dstText);
dstSaver.finalize();
dst->setUtf8Source(dstText);
dst->parse();
dst->check();
QCOMPARE(dst->diagnosticMessages().size(), 0);
QCOMPARE(dst->globalSymbolCount(), 4U);
Snapshot snapshot;
snapshot.insert(src);
snapshot.insert(dst);
Class *foo = src->globalSymbolAt(0)->asClass();
QVERIFY(foo);
QCOMPARE(foo->line(), 1U);
QCOMPARE(foo->column(), 7U);
QCOMPARE(foo->memberCount(), 3U);
Declaration *decl = foo->memberAt(1)->asDeclaration();
QVERIFY(decl);
QCOMPARE(decl->line(), 4U);
QCOMPARE(decl->column(), 6U);
CppRefactoringChanges changes(snapshot);
InsertionPointLocator find(changes);
QList<InsertionLocation> locList = find.methodDefinition(decl);
QVERIFY(locList.size() == 1);
InsertionLocation loc = locList.first();
QCOMPARE(loc.fileName(), dst->fileName());
QCOMPARE(loc.line(), 7U);
QCOMPARE(loc.column(), 2U);
QCOMPARE(loc.prefix(), QLatin1String("\n\n"));
QCOMPARE(loc.suffix(), QString());
}
示例2: context
void tst_Lookup::simple_class_1()
{
const QByteArray source = "\n"
"@interface Zoo {} +(id)alloc; -(id)init; @end\n"
"@implementation Zoo +(id)alloc{} -(id)init{} -(void)dealloc{} @end\n";
Document::Ptr doc = Document::create("simple_class_1");
doc->setSource(source);
doc->parse();
doc->check();
QVERIFY(doc->diagnosticMessages().isEmpty());
QCOMPARE(doc->globalSymbolCount(), 2U);
Snapshot snapshot;
snapshot.insert(doc);
ObjCClass *iface = doc->globalSymbolAt(0)->asObjCClass();
QVERIFY(iface);
QVERIFY(iface->isInterface());
QCOMPARE(iface->memberCount(), 2U);
ObjCClass *impl = doc->globalSymbolAt(1)->asObjCClass();
QVERIFY(impl);
QVERIFY(!impl->isInterface());
QCOMPARE(impl->memberCount(), 3U);
Declaration *allocMethodIface = iface->memberAt(0)->asDeclaration();
QVERIFY(allocMethodIface);
QVERIFY(allocMethodIface->name() && allocMethodIface->name()->identifier());
QCOMPARE(QLatin1String(allocMethodIface->name()->identifier()->chars()), QLatin1String("alloc"));
ObjCMethod *allocMethodImpl = impl->memberAt(0)->asObjCMethod();
QVERIFY(allocMethodImpl);
QVERIFY(allocMethodImpl->name() && allocMethodImpl->name()->identifier());
QCOMPARE(QLatin1String(allocMethodImpl->name()->identifier()->chars()), QLatin1String("alloc"));
ObjCMethod *deallocMethod = impl->memberAt(2)->asObjCMethod();
QVERIFY(deallocMethod);
QVERIFY(deallocMethod->name() && deallocMethod->name()->identifier());
QCOMPARE(QLatin1String(deallocMethod->name()->identifier()->chars()), QLatin1String("dealloc"));
const LookupContext context(doc, snapshot);
// check class resolving:
ClassOrNamespace *klass = context.lookupType(impl->name(), impl->enclosingScope());
QVERIFY(klass != 0);
QCOMPARE(klass->symbols().size(), 2);
QVERIFY(klass->symbols().contains(iface));
QVERIFY(klass->symbols().contains(impl));
// check method resolving:
QList<LookupItem> results = context.lookup(allocMethodImpl->name(), impl);
QCOMPARE(results.size(), 2);
QCOMPARE(results.at(0).declaration(), allocMethodIface);
QCOMPARE(results.at(1).declaration(), allocMethodImpl);
results = context.lookup(deallocMethod->name(), impl);
QCOMPARE(results.size(), 1);
QCOMPARE(results.at(0).declaration(), deallocMethod);
}
示例3: iface_impl_scoping
void tst_Lookup::iface_impl_scoping()
{
const QByteArray source = "\n"
"@interface Scooping{}-(int)method1:(int)arg;-(void)method2;@end\n"
"@implementation Scooping-(int)method1:(int)arg{return arg;}@end\n";
Document::Ptr doc = Document::create("class_with_protocol_with_protocol");
doc->setSource(source);
doc->parse();
doc->check();
QVERIFY(doc->diagnosticMessages().isEmpty());
QCOMPARE(doc->globalSymbolCount(), 2U);
Snapshot snapshot;
snapshot.insert(doc);
ObjCClass *iface = doc->globalSymbolAt(0)->asObjCClass();
QVERIFY(iface);
QVERIFY(iface->isInterface());
ObjCClass *impl = doc->globalSymbolAt(1)->asObjCClass();
QVERIFY(impl);
QVERIFY(!impl->isInterface());
QCOMPARE(iface->memberCount(), 2U);
QCOMPARE(impl->memberCount(), 1U);
ObjCMethod *method1Impl = impl->memberAt(0)->asObjCMethod();
QVERIFY(method1Impl);
QCOMPARE(method1Impl->identifier()->chars(), "method1");
// get the body of method1
QCOMPARE(method1Impl->memberCount(), 2U);
Argument *method1Arg = method1Impl->memberAt(0)->asArgument();
QVERIFY(method1Arg);
QCOMPARE(method1Arg->identifier()->chars(), "arg");
QVERIFY(method1Arg->type()->isIntegerType());
Block *method1Body = method1Impl->memberAt(1)->asBlock();
QVERIFY(method1Body);
const LookupContext context(doc, snapshot);
{ // verify if we can resolve "arg" in the body
QCOMPARE(method1Impl->argumentCount(), 1U);
Argument *arg = method1Impl->argumentAt(0)->asArgument();
QVERIFY(arg);
QVERIFY(arg->name());
QVERIFY(arg->name()->identifier());
QCOMPARE(arg->name()->identifier()->chars(), "arg");
QVERIFY(arg->type()->isIntegerType());
const QList<LookupItem> candidates = context.lookup(arg->name(), method1Body->enclosingScope());
QCOMPARE(candidates.size(), 1);
QVERIFY(candidates.at(0).declaration()->type()->asIntegerType());
}
Declaration *method2 = iface->memberAt(1)->asDeclaration();
QVERIFY(method2);
QCOMPARE(method2->identifier()->chars(), "method2");
{ // verify if we can resolve "method2" in the body
const QList<LookupItem> candidates = context.lookup(method2->name(), method1Body->enclosingScope());
QCOMPARE(candidates.size(), 1);
QCOMPARE(candidates.at(0).declaration(), method2);
}
}
示例4: changes
Utils::ChangeSet FunctionDeclDefLink::changes(const Snapshot &snapshot, int targetOffset)
{
Utils::ChangeSet changes;
// parse the current source declaration
TypeOfExpression typeOfExpression; // ### just need to preprocess...
typeOfExpression.init(sourceDocument, snapshot);
QString newDeclText = linkSelection.selectedText();
for (int i = 0; i < newDeclText.size(); ++i) {
if (newDeclText.at(i).toAscii() == 0)
newDeclText[i] = QLatin1Char('\n');
}
newDeclText.append(QLatin1String("{}"));
const QString newDeclTextPreprocessed = typeOfExpression.preprocess(newDeclText);
Document::Ptr newDeclDoc = Document::create(QLatin1String("<decl>"));
newDeclDoc->setSource(newDeclTextPreprocessed.toUtf8());
newDeclDoc->parse(Document::ParseDeclaration);
newDeclDoc->check();
// extract the function symbol
if (!newDeclDoc->translationUnit()->ast())
return changes;
FunctionDefinitionAST *newDef = newDeclDoc->translationUnit()->ast()->asFunctionDefinition();
if (!newDef)
return changes;
Function *newFunction = newDef->symbol;
if (!newFunction)
return changes;
Overview overview;
overview.setShowReturnTypes(true);
overview.setShowTemplateParameters(true);
overview.setShowArgumentNames(true);
overview.setShowFunctionSignatures(true);
// abort if the name of the newly parsed function is not the expected one
DeclaratorIdAST *newDeclId = getDeclaratorId(newDef->declarator);
if (!newDeclId || !newDeclId->name || !newDeclId->name->name
|| overview(newDeclId->name->name) != nameInitial) {
return changes;
}
LookupContext sourceContext(sourceDocument, snapshot);
LookupContext targetContext(targetFile->cppDocument(), snapshot);
// sync return type
{
// set up for rewriting return type
SubstitutionEnvironment env;
env.setContext(sourceContext);
env.switchScope(sourceFunction->enclosingScope());
ClassOrNamespace *targetCoN = targetContext.lookupType(targetFunction->enclosingScope());
if (!targetCoN)
targetCoN = targetContext.globalNamespace();
UseMinimalNames q(targetCoN);
env.enter(&q);
Control *control = sourceContext.control().data();
// get return type start position and declarator info from declaration
DeclaratorAST *declarator = 0;
SpecifierAST *firstReplaceableSpecifier = 0;
TranslationUnit *targetTranslationUnit = targetFile->cppDocument()->translationUnit();
if (SimpleDeclarationAST *simple = targetDeclaration->asSimpleDeclaration()) {
declarator = simple->declarator_list->value;
firstReplaceableSpecifier = findFirstReplaceableSpecifier(
targetTranslationUnit, simple->decl_specifier_list);
} else if (FunctionDefinitionAST *def = targetDeclaration->asFunctionDefinition()) {
declarator = def->declarator;
firstReplaceableSpecifier = findFirstReplaceableSpecifier(
targetTranslationUnit, def->decl_specifier_list);
}
int returnTypeStart = 0;
if (firstReplaceableSpecifier)
returnTypeStart = targetFile->startOf(firstReplaceableSpecifier);
else
returnTypeStart = targetFile->startOf(declarator);
if (!newFunction->returnType().isEqualTo(sourceFunction->returnType())
&& !newFunction->returnType().isEqualTo(targetFunction->returnType())) {
FullySpecifiedType type = rewriteType(newFunction->returnType(), &env, control);
const QString replacement = overview(type, targetFunction->name());
changes.replace(returnTypeStart,
targetFile->startOf(targetFunctionDeclarator->lparen_token),
replacement);
}
}
// sync parameters
{
// set up for rewriting parameter types
SubstitutionEnvironment env;
env.setContext(sourceContext);
env.switchScope(sourceFunction);
ClassOrNamespace *targetCoN = targetContext.lookupType(targetFunction);
if (!targetCoN)
targetCoN = targetContext.globalNamespace();
UseMinimalNames q(targetCoN);
//.........这里部分代码省略.........