ClassDef MocNg::parseClass(clang::CXXRecordDecl* RD, clang::Sema& Sema) { clang::Preprocessor &PP = Sema.getPreprocessor(); ClassDef Def; Def.Record = RD; for (auto it = RD->decls_begin(); it != RD->decls_end(); ++it) { if (clang::StaticAssertDecl *S = llvm::dyn_cast<clang::StaticAssertDecl>(*it) ) { if (auto *E = llvm::dyn_cast<clang::UnaryExprOrTypeTraitExpr>(S->getAssertExpr())) if (clang::ParenExpr *PE = llvm::dyn_cast<clang::ParenExpr>(E->getArgumentExpr())) { llvm::StringRef key = S->getMessage()->getString(); if (key == "qt_property") { clang::StringLiteral *Val = llvm::dyn_cast<clang::StringLiteral>(PE->getSubExpr()); if (Val) { PropertyParser Parser(Val->getString(), // Val->getStrTokenLoc(0), Val->getLocationOfByte(0, PP.getSourceManager(), PP.getLangOpts(), PP.getTargetInfo()), Sema, Def.Record); Def.Properties.push_back(Parser.parseProperty()); Def.addExtra(Parser.Extra); } else { PP.getDiagnostics().Report(S->getLocation(), PP.getDiagnostics().getCustomDiagID(clang::DiagnosticsEngine::Error, "Invalid Q_PROPERTY annotation")); } } else if (key == "qt_private_property") { clang::StringLiteral *Val1 = nullptr, *Val2 = nullptr; std::tie(Val1, Val2) = ExtractLiterals(PE, PP, "Q_PRIVATE_PROPERTY", "Invalid Q_PRIVATE_PROPERTY annotation"); if (Val1 && Val2) { PropertyParser Parser(Val2->getString(), Val2->getLocationOfByte(0, PP.getSourceManager(), PP.getLangOpts(), PP.getTargetInfo()), Sema, Def.Record); PropertyDef P = Parser.parseProperty(true); P.inPrivateClass = Val1->getString(); Def.Properties.push_back(std::move(P)); Def.addExtra(Parser.Extra); } } else if (key == "qt_private_slot") { clang::StringLiteral *Val1 = nullptr, *Val2 = nullptr; std::tie(Val1, Val2) = ExtractLiterals(PE, PP, "Q_PRIVATE_SLOT", "Invalid Q_PRIVATE_SLOT annotation"); if (Val1 && Val2) { PropertyParser Parser(Val2->getString(), Val2->getLocationOfByte(0, PP.getSourceManager(), PP.getLangOpts(), PP.getTargetInfo()), Sema, Def.Record); PrivateSlotDef P = Parser.parsePrivateSlot(); P.InPrivateClass = Val1->getString(); if (!P.Name.empty()) { Def.PrivateSlotCount += P.NumDefault + 1; Def.PrivateSlots.push_back(std::move(P)); } } } else if (key == "qt_enums") { parseEnums(Def, false, PE->getSubExpr(), Sema); } else if (key == "qt_flags") { parseEnums(Def, true, PE->getSubExpr(), Sema); } else if (key == "qt_qobject") { Def.HasQObject = true; } else if (key == "qt_fake") { Def.HasQGadget = false; } else if (key == "qt_qgadget") { Def.HasQGadget = true; } else if (key == "qt_classinfo") { clang::StringLiteral *Val1 = nullptr, *Val2 = nullptr; std::tie(Val1, Val2) = ExtractLiterals(PE, PP, "Q_CLASSINFO", "Expected string literal in Q_CLASSINFO"); if (Val1 && Val2) { Def.ClassInfo.emplace_back(Val1->getString(), Val2->getString()); } } else if (key == "qt_interfaces") { parseInterfaces(Def, PE->getSubExpr(), Sema); } else if (key == "qt_plugin_metadata") { parsePluginMetaData(Def, PE->getSubExpr(), Sema); HasPlugin = true; } } } else if (clang::CXXMethodDecl *M = llvm::dyn_cast<clang::CXXMethodDecl>(*it)) { for (auto attr_it = M->specific_attr_begin<clang::AnnotateAttr>(); attr_it != M->specific_attr_end<clang::AnnotateAttr>(); ++attr_it) { const clang::AnnotateAttr *A = *attr_it; if (A->getAnnotation() == "qt_signal") { Def.Signals.push_back(M); } else if (A->getAnnotation() == "qt_slot") { Def.Slots.push_back(M); } else if (A->getAnnotation() == "qt_invokable" || A->getAnnotation() == "qt_scriptable" ) { if (auto *C = llvm::dyn_cast<clang::CXXConstructorDecl>(M)) { Def.Constructors.push_back(C); } else { Def.Methods.push_back(M); } } else if (A->getAnnotation().startswith("qt_revision:")) { Def.RevisionMethodCount++; } } } } //Check notify Signals for (PropertyDef &P: Def.Properties) { if (!P.notify.Str.empty()) { int Idx = 0; for (clang::CXXMethodDecl *MD : Def.Signals) { if (MD->getName() == P.notify.Str) { P.notify.notifyId = Idx; P.notify.MD = MD; break; } Idx += 1 + MD->getNumParams() - MD->getMinRequiredArguments(); } if (P.notify.notifyId < 0 ) { PP.getDiagnostics().Report(P.notify.Loc, PP.getDiagnostics().getCustomDiagID(clang::DiagnosticsEngine::Error, "NOTIFY signal '%0' of property '%1' does not exist in class %2")) << P.notify.Str << P.name << Def.Record; } else { Def.NotifyCount++; } } if (P.revision > 0) Def.RevisionPropertyCount++; } return Def; }
static void parseEnums(ClassDef &Def, bool isFlag, clang::Expr *Content, clang::Sema &Sema) { clang::Preprocessor &PP = Sema.getPreprocessor(); clang::StringLiteral *Val = llvm::dyn_cast<clang::StringLiteral>(Content); if (!Val) { PP.getDiagnostics().Report(Content->getExprLoc(), PP.getDiagnostics().getCustomDiagID(clang::DiagnosticsEngine::Error, "Invalid Q_ENUMS annotation")); return; } llvm::MemoryBuffer* Buf = maybe_unique(llvm::MemoryBuffer::getMemBufferCopy(Val->getString(), "Q_ENUMS")); clang::Lexer Lex(CreateFileIDForMemBuffer(PP, Buf, Content->getExprLoc()), Buf, PP.getSourceManager(), PP.getLangOpts()); clang::CXXScopeSpec SS; clang::Token Tok, Next; Lex.LexFromRawLexer(Tok); for (; !Tok.is(clang::tok::eof); Tok = Next) { Lex.LexFromRawLexer(Next); clang::IdentifierInfo* II = nullptr; if (Tok.is(clang::tok::raw_identifier)) II = PP.LookUpIdentifierInfo(Tok); if (Tok.is(clang::tok::identifier)) { if (Next.is(clang::tok::coloncolon)) { if (Sema.ActOnCXXNestedNameSpecifier(Sema.getScopeForContext(Def.Record), *II, GetFromLiteral(Tok, Val, PP), GetFromLiteral(Next, Val, PP), {}, false, SS)) SS.SetInvalid({GetFromLiteral(Tok, Val, PP), GetFromLiteral(Next, Val, PP)}); Lex.LexFromRawLexer(Next); continue; } clang::LookupResult Found(Sema, II, GetFromLiteral(Tok, Val, PP), clang::Sema::LookupNestedNameSpecifierName); if (SS.isEmpty()) Sema.LookupQualifiedName(Found, Def.Record); else { clang::DeclContext* DC = Sema.computeDeclContext(SS); Sema.LookupQualifiedName(Found, DC ? DC : Def.Record); } llvm::StringRef Alias; clang::EnumDecl* R = Found.getAsSingle<clang::EnumDecl>(); if (!R) { if (clang::TypedefDecl *TD = Found.getAsSingle<clang::TypedefDecl>()) { const clang::EnumType* ET = TD->getUnderlyingType()->getAs<clang::EnumType>(); const clang::TemplateSpecializationType* TDR = TD->getUnderlyingType()->getAs<clang::TemplateSpecializationType>(); if(TDR && TDR->getNumArgs() == 1 && TDR->getTemplateName().getAsTemplateDecl()->getName() == "QFlags") ET = TDR->getArg(0).getAsType()->getAs<clang::EnumType>(); if (ET) { R = ET->getDecl(); if (TD->getIdentifier()) Alias = TD->getName(); } } } if (Found.empty() || !R) { // TODO: typo correction // This should be an error, but the official moc do not understand that as an error. PP.getDiagnostics().Report(GetFromLiteral(Tok, Val, PP), PP.getDiagnostics().getCustomDiagID(clang::DiagnosticsEngine::Warning, "no enum names %0")) << Found.getLookupName(); break; } if (R->getDeclContext() == Def.Record) { if (Alias.empty() && R->getIdentifier()) Alias = R->getName(); Def.addEnum(R, Alias.empty() ? R->getNameAsString() : std::string(Alias), isFlag); } else if (R->getDeclContext()->isRecord() && llvm::isa<clang::CXXRecordDecl>(R->getDeclContext())) { // TODO: check it is a QObject Def.addExtra(llvm::cast<clang::CXXRecordDecl>(R->getDeclContext())); } SS.clear(); continue; } else if (Tok.is(clang::tok::coloncolon)) { if (SS.isEmpty()) { SS.MakeGlobal(Sema.getASTContext(), GetFromLiteral(Tok, Val, PP)); continue; } } PP.getDiagnostics().Report(GetFromLiteral(Tok, Val, PP), PP.getDiagnostics().getCustomDiagID(clang::DiagnosticsEngine::Error, "Invalid token in Q_ENUMS")); break; } }