ASTMatchers.h 247 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- ASTMatchers.h - Structural query framework ---------------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. //
  14. // This file implements matchers to be used together with the MatchFinder to
  15. // match AST nodes.
  16. //
  17. // Matchers are created by generator functions, which can be combined in
  18. // a functional in-language DSL to express queries over the C++ AST.
  19. //
  20. // For example, to match a class with a certain name, one would call:
  21. // cxxRecordDecl(hasName("MyClass"))
  22. // which returns a matcher that can be used to find all AST nodes that declare
  23. // a class named 'MyClass'.
  24. //
  25. // For more complicated match expressions we're often interested in accessing
  26. // multiple parts of the matched AST nodes once a match is found. In that case,
  27. // call `.bind("name")` on match expressions that match the nodes you want to
  28. // access.
  29. //
  30. // For example, when we're interested in child classes of a certain class, we
  31. // would write:
  32. // cxxRecordDecl(hasName("MyClass"), has(recordDecl().bind("child")))
  33. // When the match is found via the MatchFinder, a user provided callback will
  34. // be called with a BoundNodes instance that contains a mapping from the
  35. // strings that we provided for the `.bind()` calls to the nodes that were
  36. // matched.
  37. // In the given example, each time our matcher finds a match we get a callback
  38. // where "child" is bound to the RecordDecl node of the matching child
  39. // class declaration.
  40. //
  41. // See ASTMatchersInternal.h for a more in-depth explanation of the
  42. // implementation details of the matcher framework.
  43. //
  44. // See ASTMatchFinder.h for how to use the generated matchers to run over
  45. // an AST.
  46. //
  47. //===----------------------------------------------------------------------===//
  48. #ifndef LLVM_CLANG_ASTMATCHERS_ASTMATCHERS_H
  49. #define LLVM_CLANG_ASTMATCHERS_ASTMATCHERS_H
  50. #include "clang/AST/ASTContext.h"
  51. #include "clang/AST/ASTTypeTraits.h"
  52. #include "clang/AST/Attr.h"
  53. #include "clang/AST/CXXInheritance.h"
  54. #include "clang/AST/Decl.h"
  55. #include "clang/AST/DeclCXX.h"
  56. #include "clang/AST/DeclFriend.h"
  57. #include "clang/AST/DeclObjC.h"
  58. #include "clang/AST/DeclTemplate.h"
  59. #include "clang/AST/Expr.h"
  60. #include "clang/AST/ExprCXX.h"
  61. #include "clang/AST/ExprObjC.h"
  62. #include "clang/AST/LambdaCapture.h"
  63. #include "clang/AST/NestedNameSpecifier.h"
  64. #include "clang/AST/OpenMPClause.h"
  65. #include "clang/AST/OperationKinds.h"
  66. #include "clang/AST/ParentMapContext.h"
  67. #include "clang/AST/Stmt.h"
  68. #include "clang/AST/StmtCXX.h"
  69. #include "clang/AST/StmtObjC.h"
  70. #include "clang/AST/StmtOpenMP.h"
  71. #include "clang/AST/TemplateBase.h"
  72. #include "clang/AST/TemplateName.h"
  73. #include "clang/AST/Type.h"
  74. #include "clang/AST/TypeLoc.h"
  75. #include "clang/ASTMatchers/ASTMatchersInternal.h"
  76. #include "clang/ASTMatchers/ASTMatchersMacros.h"
  77. #include "clang/Basic/AttrKinds.h"
  78. #include "clang/Basic/ExceptionSpecificationType.h"
  79. #include "clang/Basic/FileManager.h"
  80. #include "clang/Basic/IdentifierTable.h"
  81. #include "clang/Basic/LLVM.h"
  82. #include "clang/Basic/SourceManager.h"
  83. #include "clang/Basic/Specifiers.h"
  84. #include "clang/Basic/TypeTraits.h"
  85. #include "llvm/ADT/ArrayRef.h"
  86. #include "llvm/ADT/SmallVector.h"
  87. #include "llvm/ADT/StringRef.h"
  88. #include "llvm/Support/Casting.h"
  89. #include "llvm/Support/Compiler.h"
  90. #include "llvm/Support/ErrorHandling.h"
  91. #include "llvm/Support/Regex.h"
  92. #include <cassert>
  93. #include <cstddef>
  94. #include <iterator>
  95. #include <limits>
  96. #include <string>
  97. #include <utility>
  98. #include <vector>
  99. namespace clang {
  100. namespace ast_matchers {
  101. /// Maps string IDs to AST nodes matched by parts of a matcher.
  102. ///
  103. /// The bound nodes are generated by calling \c bind("id") on the node matchers
  104. /// of the nodes we want to access later.
  105. ///
  106. /// The instances of BoundNodes are created by \c MatchFinder when the user's
  107. /// callbacks are executed every time a match is found.
  108. class BoundNodes {
  109. public:
  110. /// Returns the AST node bound to \c ID.
  111. ///
  112. /// Returns NULL if there was no node bound to \c ID or if there is a node but
  113. /// it cannot be converted to the specified type.
  114. template <typename T>
  115. const T *getNodeAs(StringRef ID) const {
  116. return MyBoundNodes.getNodeAs<T>(ID);
  117. }
  118. /// Type of mapping from binding identifiers to bound nodes. This type
  119. /// is an associative container with a key type of \c std::string and a value
  120. /// type of \c clang::DynTypedNode
  121. using IDToNodeMap = internal::BoundNodesMap::IDToNodeMap;
  122. /// Retrieve mapping from binding identifiers to bound nodes.
  123. const IDToNodeMap &getMap() const {
  124. return MyBoundNodes.getMap();
  125. }
  126. private:
  127. friend class internal::BoundNodesTreeBuilder;
  128. /// Create BoundNodes from a pre-filled map of bindings.
  129. BoundNodes(internal::BoundNodesMap &MyBoundNodes)
  130. : MyBoundNodes(MyBoundNodes) {}
  131. internal::BoundNodesMap MyBoundNodes;
  132. };
  133. /// Types of matchers for the top-level classes in the AST class
  134. /// hierarchy.
  135. /// @{
  136. using DeclarationMatcher = internal::Matcher<Decl>;
  137. using StatementMatcher = internal::Matcher<Stmt>;
  138. using TypeMatcher = internal::Matcher<QualType>;
  139. using TypeLocMatcher = internal::Matcher<TypeLoc>;
  140. using NestedNameSpecifierMatcher = internal::Matcher<NestedNameSpecifier>;
  141. using NestedNameSpecifierLocMatcher = internal::Matcher<NestedNameSpecifierLoc>;
  142. using CXXBaseSpecifierMatcher = internal::Matcher<CXXBaseSpecifier>;
  143. using CXXCtorInitializerMatcher = internal::Matcher<CXXCtorInitializer>;
  144. using TemplateArgumentMatcher = internal::Matcher<TemplateArgument>;
  145. using TemplateArgumentLocMatcher = internal::Matcher<TemplateArgumentLoc>;
  146. using LambdaCaptureMatcher = internal::Matcher<LambdaCapture>;
  147. using AttrMatcher = internal::Matcher<Attr>;
  148. /// @}
  149. /// Matches any node.
  150. ///
  151. /// Useful when another matcher requires a child matcher, but there's no
  152. /// additional constraint. This will often be used with an explicit conversion
  153. /// to an \c internal::Matcher<> type such as \c TypeMatcher.
  154. ///
  155. /// Example: \c DeclarationMatcher(anything()) matches all declarations, e.g.,
  156. /// \code
  157. /// "int* p" and "void f()" in
  158. /// int* p;
  159. /// void f();
  160. /// \endcode
  161. ///
  162. /// Usable as: Any Matcher
  163. inline internal::TrueMatcher anything() { return internal::TrueMatcher(); }
  164. /// Matches the top declaration context.
  165. ///
  166. /// Given
  167. /// \code
  168. /// int X;
  169. /// namespace NS {
  170. /// int Y;
  171. /// } // namespace NS
  172. /// \endcode
  173. /// decl(hasDeclContext(translationUnitDecl()))
  174. /// matches "int X", but not "int Y".
  175. extern const internal::VariadicDynCastAllOfMatcher<Decl, TranslationUnitDecl>
  176. translationUnitDecl;
  177. /// Matches typedef declarations.
  178. ///
  179. /// Given
  180. /// \code
  181. /// typedef int X;
  182. /// using Y = int;
  183. /// \endcode
  184. /// typedefDecl()
  185. /// matches "typedef int X", but not "using Y = int"
  186. extern const internal::VariadicDynCastAllOfMatcher<Decl, TypedefDecl>
  187. typedefDecl;
  188. /// Matches typedef name declarations.
  189. ///
  190. /// Given
  191. /// \code
  192. /// typedef int X;
  193. /// using Y = int;
  194. /// \endcode
  195. /// typedefNameDecl()
  196. /// matches "typedef int X" and "using Y = int"
  197. extern const internal::VariadicDynCastAllOfMatcher<Decl, TypedefNameDecl>
  198. typedefNameDecl;
  199. /// Matches type alias declarations.
  200. ///
  201. /// Given
  202. /// \code
  203. /// typedef int X;
  204. /// using Y = int;
  205. /// \endcode
  206. /// typeAliasDecl()
  207. /// matches "using Y = int", but not "typedef int X"
  208. extern const internal::VariadicDynCastAllOfMatcher<Decl, TypeAliasDecl>
  209. typeAliasDecl;
  210. /// Matches type alias template declarations.
  211. ///
  212. /// typeAliasTemplateDecl() matches
  213. /// \code
  214. /// template <typename T>
  215. /// using Y = X<T>;
  216. /// \endcode
  217. extern const internal::VariadicDynCastAllOfMatcher<Decl, TypeAliasTemplateDecl>
  218. typeAliasTemplateDecl;
  219. /// Matches AST nodes that were expanded within the main-file.
  220. ///
  221. /// Example matches X but not Y
  222. /// (matcher = cxxRecordDecl(isExpansionInMainFile())
  223. /// \code
  224. /// #include <Y.h>
  225. /// class X {};
  226. /// \endcode
  227. /// Y.h:
  228. /// \code
  229. /// class Y {};
  230. /// \endcode
  231. ///
  232. /// Usable as: Matcher<Decl>, Matcher<Stmt>, Matcher<TypeLoc>
  233. AST_POLYMORPHIC_MATCHER(isExpansionInMainFile,
  234. AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt, TypeLoc)) {
  235. auto &SourceManager = Finder->getASTContext().getSourceManager();
  236. return SourceManager.isInMainFile(
  237. SourceManager.getExpansionLoc(Node.getBeginLoc()));
  238. }
  239. /// Matches AST nodes that were expanded within system-header-files.
  240. ///
  241. /// Example matches Y but not X
  242. /// (matcher = cxxRecordDecl(isExpansionInSystemHeader())
  243. /// \code
  244. /// #include <SystemHeader.h>
  245. /// class X {};
  246. /// \endcode
  247. /// SystemHeader.h:
  248. /// \code
  249. /// class Y {};
  250. /// \endcode
  251. ///
  252. /// Usable as: Matcher<Decl>, Matcher<Stmt>, Matcher<TypeLoc>
  253. AST_POLYMORPHIC_MATCHER(isExpansionInSystemHeader,
  254. AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt, TypeLoc)) {
  255. auto &SourceManager = Finder->getASTContext().getSourceManager();
  256. auto ExpansionLoc = SourceManager.getExpansionLoc(Node.getBeginLoc());
  257. if (ExpansionLoc.isInvalid()) {
  258. return false;
  259. }
  260. return SourceManager.isInSystemHeader(ExpansionLoc);
  261. }
  262. /// Matches AST nodes that were expanded within files whose name is
  263. /// partially matching a given regex.
  264. ///
  265. /// Example matches Y but not X
  266. /// (matcher = cxxRecordDecl(isExpansionInFileMatching("AST.*"))
  267. /// \code
  268. /// #include "ASTMatcher.h"
  269. /// class X {};
  270. /// \endcode
  271. /// ASTMatcher.h:
  272. /// \code
  273. /// class Y {};
  274. /// \endcode
  275. ///
  276. /// Usable as: Matcher<Decl>, Matcher<Stmt>, Matcher<TypeLoc>
  277. AST_POLYMORPHIC_MATCHER_REGEX(isExpansionInFileMatching,
  278. AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt,
  279. TypeLoc),
  280. RegExp) {
  281. auto &SourceManager = Finder->getASTContext().getSourceManager();
  282. auto ExpansionLoc = SourceManager.getExpansionLoc(Node.getBeginLoc());
  283. if (ExpansionLoc.isInvalid()) {
  284. return false;
  285. }
  286. auto FileEntry =
  287. SourceManager.getFileEntryForID(SourceManager.getFileID(ExpansionLoc));
  288. if (!FileEntry) {
  289. return false;
  290. }
  291. auto Filename = FileEntry->getName();
  292. return RegExp->match(Filename);
  293. }
  294. /// Matches statements that are (transitively) expanded from the named macro.
  295. /// Does not match if only part of the statement is expanded from that macro or
  296. /// if different parts of the statement are expanded from different
  297. /// appearances of the macro.
  298. AST_POLYMORPHIC_MATCHER_P(isExpandedFromMacro,
  299. AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Stmt, TypeLoc),
  300. std::string, MacroName) {
  301. // Verifies that the statement' beginning and ending are both expanded from
  302. // the same instance of the given macro.
  303. auto& Context = Finder->getASTContext();
  304. llvm::Optional<SourceLocation> B =
  305. internal::getExpansionLocOfMacro(MacroName, Node.getBeginLoc(), Context);
  306. if (!B) return false;
  307. llvm::Optional<SourceLocation> E =
  308. internal::getExpansionLocOfMacro(MacroName, Node.getEndLoc(), Context);
  309. if (!E) return false;
  310. return *B == *E;
  311. }
  312. /// Matches declarations.
  313. ///
  314. /// Examples matches \c X, \c C, and the friend declaration inside \c C;
  315. /// \code
  316. /// void X();
  317. /// class C {
  318. /// friend X;
  319. /// };
  320. /// \endcode
  321. extern const internal::VariadicAllOfMatcher<Decl> decl;
  322. /// Matches decomposition-declarations.
  323. ///
  324. /// Examples matches the declaration node with \c foo and \c bar, but not
  325. /// \c number.
  326. /// (matcher = declStmt(has(decompositionDecl())))
  327. ///
  328. /// \code
  329. /// int number = 42;
  330. /// auto [foo, bar] = std::make_pair{42, 42};
  331. /// \endcode
  332. extern const internal::VariadicDynCastAllOfMatcher<Decl, DecompositionDecl>
  333. decompositionDecl;
  334. /// Matches binding declarations
  335. /// Example matches \c foo and \c bar
  336. /// (matcher = bindingDecl()
  337. ///
  338. /// \code
  339. /// auto [foo, bar] = std::make_pair{42, 42};
  340. /// \endcode
  341. extern const internal::VariadicDynCastAllOfMatcher<Decl, BindingDecl>
  342. bindingDecl;
  343. /// Matches a declaration of a linkage specification.
  344. ///
  345. /// Given
  346. /// \code
  347. /// extern "C" {}
  348. /// \endcode
  349. /// linkageSpecDecl()
  350. /// matches "extern "C" {}"
  351. extern const internal::VariadicDynCastAllOfMatcher<Decl, LinkageSpecDecl>
  352. linkageSpecDecl;
  353. /// Matches a declaration of anything that could have a name.
  354. ///
  355. /// Example matches \c X, \c S, the anonymous union type, \c i, and \c U;
  356. /// \code
  357. /// typedef int X;
  358. /// struct S {
  359. /// union {
  360. /// int i;
  361. /// } U;
  362. /// };
  363. /// \endcode
  364. extern const internal::VariadicDynCastAllOfMatcher<Decl, NamedDecl> namedDecl;
  365. /// Matches a declaration of label.
  366. ///
  367. /// Given
  368. /// \code
  369. /// goto FOO;
  370. /// FOO: bar();
  371. /// \endcode
  372. /// labelDecl()
  373. /// matches 'FOO:'
  374. extern const internal::VariadicDynCastAllOfMatcher<Decl, LabelDecl> labelDecl;
  375. /// Matches a declaration of a namespace.
  376. ///
  377. /// Given
  378. /// \code
  379. /// namespace {}
  380. /// namespace test {}
  381. /// \endcode
  382. /// namespaceDecl()
  383. /// matches "namespace {}" and "namespace test {}"
  384. extern const internal::VariadicDynCastAllOfMatcher<Decl, NamespaceDecl>
  385. namespaceDecl;
  386. /// Matches a declaration of a namespace alias.
  387. ///
  388. /// Given
  389. /// \code
  390. /// namespace test {}
  391. /// namespace alias = ::test;
  392. /// \endcode
  393. /// namespaceAliasDecl()
  394. /// matches "namespace alias" but not "namespace test"
  395. extern const internal::VariadicDynCastAllOfMatcher<Decl, NamespaceAliasDecl>
  396. namespaceAliasDecl;
  397. /// Matches class, struct, and union declarations.
  398. ///
  399. /// Example matches \c X, \c Z, \c U, and \c S
  400. /// \code
  401. /// class X;
  402. /// template<class T> class Z {};
  403. /// struct S {};
  404. /// union U {};
  405. /// \endcode
  406. extern const internal::VariadicDynCastAllOfMatcher<Decl, RecordDecl> recordDecl;
  407. /// Matches C++ class declarations.
  408. ///
  409. /// Example matches \c X, \c Z
  410. /// \code
  411. /// class X;
  412. /// template<class T> class Z {};
  413. /// \endcode
  414. extern const internal::VariadicDynCastAllOfMatcher<Decl, CXXRecordDecl>
  415. cxxRecordDecl;
  416. /// Matches C++ class template declarations.
  417. ///
  418. /// Example matches \c Z
  419. /// \code
  420. /// template<class T> class Z {};
  421. /// \endcode
  422. extern const internal::VariadicDynCastAllOfMatcher<Decl, ClassTemplateDecl>
  423. classTemplateDecl;
  424. /// Matches C++ class template specializations.
  425. ///
  426. /// Given
  427. /// \code
  428. /// template<typename T> class A {};
  429. /// template<> class A<double> {};
  430. /// A<int> a;
  431. /// \endcode
  432. /// classTemplateSpecializationDecl()
  433. /// matches the specializations \c A<int> and \c A<double>
  434. extern const internal::VariadicDynCastAllOfMatcher<
  435. Decl, ClassTemplateSpecializationDecl>
  436. classTemplateSpecializationDecl;
  437. /// Matches C++ class template partial specializations.
  438. ///
  439. /// Given
  440. /// \code
  441. /// template<class T1, class T2, int I>
  442. /// class A {};
  443. ///
  444. /// template<class T, int I>
  445. /// class A<T, T*, I> {};
  446. ///
  447. /// template<>
  448. /// class A<int, int, 1> {};
  449. /// \endcode
  450. /// classTemplatePartialSpecializationDecl()
  451. /// matches the specialization \c A<T,T*,I> but not \c A<int,int,1>
  452. extern const internal::VariadicDynCastAllOfMatcher<
  453. Decl, ClassTemplatePartialSpecializationDecl>
  454. classTemplatePartialSpecializationDecl;
  455. /// Matches declarator declarations (field, variable, function
  456. /// and non-type template parameter declarations).
  457. ///
  458. /// Given
  459. /// \code
  460. /// class X { int y; };
  461. /// \endcode
  462. /// declaratorDecl()
  463. /// matches \c int y.
  464. extern const internal::VariadicDynCastAllOfMatcher<Decl, DeclaratorDecl>
  465. declaratorDecl;
  466. /// Matches parameter variable declarations.
  467. ///
  468. /// Given
  469. /// \code
  470. /// void f(int x);
  471. /// \endcode
  472. /// parmVarDecl()
  473. /// matches \c int x.
  474. extern const internal::VariadicDynCastAllOfMatcher<Decl, ParmVarDecl>
  475. parmVarDecl;
  476. /// Matches C++ access specifier declarations.
  477. ///
  478. /// Given
  479. /// \code
  480. /// class C {
  481. /// public:
  482. /// int a;
  483. /// };
  484. /// \endcode
  485. /// accessSpecDecl()
  486. /// matches 'public:'
  487. extern const internal::VariadicDynCastAllOfMatcher<Decl, AccessSpecDecl>
  488. accessSpecDecl;
  489. /// Matches class bases.
  490. ///
  491. /// Examples matches \c public virtual B.
  492. /// \code
  493. /// class B {};
  494. /// class C : public virtual B {};
  495. /// \endcode
  496. extern const internal::VariadicAllOfMatcher<CXXBaseSpecifier> cxxBaseSpecifier;
  497. /// Matches constructor initializers.
  498. ///
  499. /// Examples matches \c i(42).
  500. /// \code
  501. /// class C {
  502. /// C() : i(42) {}
  503. /// int i;
  504. /// };
  505. /// \endcode
  506. extern const internal::VariadicAllOfMatcher<CXXCtorInitializer>
  507. cxxCtorInitializer;
  508. /// Matches template arguments.
  509. ///
  510. /// Given
  511. /// \code
  512. /// template <typename T> struct C {};
  513. /// C<int> c;
  514. /// \endcode
  515. /// templateArgument()
  516. /// matches 'int' in C<int>.
  517. extern const internal::VariadicAllOfMatcher<TemplateArgument> templateArgument;
  518. /// Matches template arguments (with location info).
  519. ///
  520. /// Given
  521. /// \code
  522. /// template <typename T> struct C {};
  523. /// C<int> c;
  524. /// \endcode
  525. /// templateArgumentLoc()
  526. /// matches 'int' in C<int>.
  527. extern const internal::VariadicAllOfMatcher<TemplateArgumentLoc>
  528. templateArgumentLoc;
  529. /// Matches template name.
  530. ///
  531. /// Given
  532. /// \code
  533. /// template <typename T> class X { };
  534. /// X<int> xi;
  535. /// \endcode
  536. /// templateName()
  537. /// matches 'X' in X<int>.
  538. extern const internal::VariadicAllOfMatcher<TemplateName> templateName;
  539. /// Matches non-type template parameter declarations.
  540. ///
  541. /// Given
  542. /// \code
  543. /// template <typename T, int N> struct C {};
  544. /// \endcode
  545. /// nonTypeTemplateParmDecl()
  546. /// matches 'N', but not 'T'.
  547. extern const internal::VariadicDynCastAllOfMatcher<Decl,
  548. NonTypeTemplateParmDecl>
  549. nonTypeTemplateParmDecl;
  550. /// Matches template type parameter declarations.
  551. ///
  552. /// Given
  553. /// \code
  554. /// template <typename T, int N> struct C {};
  555. /// \endcode
  556. /// templateTypeParmDecl()
  557. /// matches 'T', but not 'N'.
  558. extern const internal::VariadicDynCastAllOfMatcher<Decl, TemplateTypeParmDecl>
  559. templateTypeParmDecl;
  560. /// Matches template template parameter declarations.
  561. ///
  562. /// Given
  563. /// \code
  564. /// template <template <typename> class Z, int N> struct C {};
  565. /// \endcode
  566. /// templateTypeParmDecl()
  567. /// matches 'Z', but not 'N'.
  568. extern const internal::VariadicDynCastAllOfMatcher<Decl,
  569. TemplateTemplateParmDecl>
  570. templateTemplateParmDecl;
  571. /// Matches public C++ declarations and C++ base specifers that specify public
  572. /// inheritance.
  573. ///
  574. /// Examples:
  575. /// \code
  576. /// class C {
  577. /// public: int a; // fieldDecl(isPublic()) matches 'a'
  578. /// protected: int b;
  579. /// private: int c;
  580. /// };
  581. /// \endcode
  582. ///
  583. /// \code
  584. /// class Base {};
  585. /// class Derived1 : public Base {}; // matches 'Base'
  586. /// struct Derived2 : Base {}; // matches 'Base'
  587. /// \endcode
  588. AST_POLYMORPHIC_MATCHER(isPublic,
  589. AST_POLYMORPHIC_SUPPORTED_TYPES(Decl,
  590. CXXBaseSpecifier)) {
  591. return getAccessSpecifier(Node) == AS_public;
  592. }
  593. /// Matches protected C++ declarations and C++ base specifers that specify
  594. /// protected inheritance.
  595. ///
  596. /// Examples:
  597. /// \code
  598. /// class C {
  599. /// public: int a;
  600. /// protected: int b; // fieldDecl(isProtected()) matches 'b'
  601. /// private: int c;
  602. /// };
  603. /// \endcode
  604. ///
  605. /// \code
  606. /// class Base {};
  607. /// class Derived : protected Base {}; // matches 'Base'
  608. /// \endcode
  609. AST_POLYMORPHIC_MATCHER(isProtected,
  610. AST_POLYMORPHIC_SUPPORTED_TYPES(Decl,
  611. CXXBaseSpecifier)) {
  612. return getAccessSpecifier(Node) == AS_protected;
  613. }
  614. /// Matches private C++ declarations and C++ base specifers that specify private
  615. /// inheritance.
  616. ///
  617. /// Examples:
  618. /// \code
  619. /// class C {
  620. /// public: int a;
  621. /// protected: int b;
  622. /// private: int c; // fieldDecl(isPrivate()) matches 'c'
  623. /// };
  624. /// \endcode
  625. ///
  626. /// \code
  627. /// struct Base {};
  628. /// struct Derived1 : private Base {}; // matches 'Base'
  629. /// class Derived2 : Base {}; // matches 'Base'
  630. /// \endcode
  631. AST_POLYMORPHIC_MATCHER(isPrivate,
  632. AST_POLYMORPHIC_SUPPORTED_TYPES(Decl,
  633. CXXBaseSpecifier)) {
  634. return getAccessSpecifier(Node) == AS_private;
  635. }
  636. /// Matches non-static data members that are bit-fields.
  637. ///
  638. /// Given
  639. /// \code
  640. /// class C {
  641. /// int a : 2;
  642. /// int b;
  643. /// };
  644. /// \endcode
  645. /// fieldDecl(isBitField())
  646. /// matches 'int a;' but not 'int b;'.
  647. AST_MATCHER(FieldDecl, isBitField) {
  648. return Node.isBitField();
  649. }
  650. /// Matches non-static data members that are bit-fields of the specified
  651. /// bit width.
  652. ///
  653. /// Given
  654. /// \code
  655. /// class C {
  656. /// int a : 2;
  657. /// int b : 4;
  658. /// int c : 2;
  659. /// };
  660. /// \endcode
  661. /// fieldDecl(hasBitWidth(2))
  662. /// matches 'int a;' and 'int c;' but not 'int b;'.
  663. AST_MATCHER_P(FieldDecl, hasBitWidth, unsigned, Width) {
  664. return Node.isBitField() &&
  665. Node.getBitWidthValue(Finder->getASTContext()) == Width;
  666. }
  667. /// Matches non-static data members that have an in-class initializer.
  668. ///
  669. /// Given
  670. /// \code
  671. /// class C {
  672. /// int a = 2;
  673. /// int b = 3;
  674. /// int c;
  675. /// };
  676. /// \endcode
  677. /// fieldDecl(hasInClassInitializer(integerLiteral(equals(2))))
  678. /// matches 'int a;' but not 'int b;'.
  679. /// fieldDecl(hasInClassInitializer(anything()))
  680. /// matches 'int a;' and 'int b;' but not 'int c;'.
  681. AST_MATCHER_P(FieldDecl, hasInClassInitializer, internal::Matcher<Expr>,
  682. InnerMatcher) {
  683. const Expr *Initializer = Node.getInClassInitializer();
  684. return (Initializer != nullptr &&
  685. InnerMatcher.matches(*Initializer, Finder, Builder));
  686. }
  687. /// Determines whether the function is "main", which is the entry point
  688. /// into an executable program.
  689. AST_MATCHER(FunctionDecl, isMain) {
  690. return Node.isMain();
  691. }
  692. /// Matches the specialized template of a specialization declaration.
  693. ///
  694. /// Given
  695. /// \code
  696. /// template<typename T> class A {}; #1
  697. /// template<> class A<int> {}; #2
  698. /// \endcode
  699. /// classTemplateSpecializationDecl(hasSpecializedTemplate(classTemplateDecl()))
  700. /// matches '#2' with classTemplateDecl() matching the class template
  701. /// declaration of 'A' at #1.
  702. AST_MATCHER_P(ClassTemplateSpecializationDecl, hasSpecializedTemplate,
  703. internal::Matcher<ClassTemplateDecl>, InnerMatcher) {
  704. const ClassTemplateDecl* Decl = Node.getSpecializedTemplate();
  705. return (Decl != nullptr &&
  706. InnerMatcher.matches(*Decl, Finder, Builder));
  707. }
  708. /// Matches an entity that has been implicitly added by the compiler (e.g.
  709. /// implicit default/copy constructors).
  710. AST_POLYMORPHIC_MATCHER(isImplicit,
  711. AST_POLYMORPHIC_SUPPORTED_TYPES(Decl, Attr,
  712. LambdaCapture)) {
  713. return Node.isImplicit();
  714. }
  715. /// Matches classTemplateSpecializations, templateSpecializationType and
  716. /// functionDecl that have at least one TemplateArgument matching the given
  717. /// InnerMatcher.
  718. ///
  719. /// Given
  720. /// \code
  721. /// template<typename T> class A {};
  722. /// template<> class A<double> {};
  723. /// A<int> a;
  724. ///
  725. /// template<typename T> f() {};
  726. /// void func() { f<int>(); };
  727. /// \endcode
  728. ///
  729. /// \endcode
  730. /// classTemplateSpecializationDecl(hasAnyTemplateArgument(
  731. /// refersToType(asString("int"))))
  732. /// matches the specialization \c A<int>
  733. ///
  734. /// functionDecl(hasAnyTemplateArgument(refersToType(asString("int"))))
  735. /// matches the specialization \c f<int>
  736. AST_POLYMORPHIC_MATCHER_P(
  737. hasAnyTemplateArgument,
  738. AST_POLYMORPHIC_SUPPORTED_TYPES(ClassTemplateSpecializationDecl,
  739. TemplateSpecializationType,
  740. FunctionDecl),
  741. internal::Matcher<TemplateArgument>, InnerMatcher) {
  742. ArrayRef<TemplateArgument> List =
  743. internal::getTemplateSpecializationArgs(Node);
  744. return matchesFirstInRange(InnerMatcher, List.begin(), List.end(), Finder,
  745. Builder) != List.end();
  746. }
  747. /// Causes all nested matchers to be matched with the specified traversal kind.
  748. ///
  749. /// Given
  750. /// \code
  751. /// void foo()
  752. /// {
  753. /// int i = 3.0;
  754. /// }
  755. /// \endcode
  756. /// The matcher
  757. /// \code
  758. /// traverse(TK_IgnoreUnlessSpelledInSource,
  759. /// varDecl(hasInitializer(floatLiteral().bind("init")))
  760. /// )
  761. /// \endcode
  762. /// matches the variable declaration with "init" bound to the "3.0".
  763. template <typename T>
  764. internal::Matcher<T> traverse(TraversalKind TK,
  765. const internal::Matcher<T> &InnerMatcher) {
  766. return internal::DynTypedMatcher::constructRestrictedWrapper(
  767. new internal::TraversalMatcher<T>(TK, InnerMatcher),
  768. InnerMatcher.getID().first)
  769. .template unconditionalConvertTo<T>();
  770. }
  771. template <typename T>
  772. internal::BindableMatcher<T>
  773. traverse(TraversalKind TK, const internal::BindableMatcher<T> &InnerMatcher) {
  774. return internal::BindableMatcher<T>(
  775. internal::DynTypedMatcher::constructRestrictedWrapper(
  776. new internal::TraversalMatcher<T>(TK, InnerMatcher),
  777. InnerMatcher.getID().first)
  778. .template unconditionalConvertTo<T>());
  779. }
  780. template <typename... T>
  781. internal::TraversalWrapper<internal::VariadicOperatorMatcher<T...>>
  782. traverse(TraversalKind TK,
  783. const internal::VariadicOperatorMatcher<T...> &InnerMatcher) {
  784. return internal::TraversalWrapper<internal::VariadicOperatorMatcher<T...>>(
  785. TK, InnerMatcher);
  786. }
  787. template <template <typename ToArg, typename FromArg> class ArgumentAdapterT,
  788. typename T, typename ToTypes>
  789. internal::TraversalWrapper<
  790. internal::ArgumentAdaptingMatcherFuncAdaptor<ArgumentAdapterT, T, ToTypes>>
  791. traverse(TraversalKind TK, const internal::ArgumentAdaptingMatcherFuncAdaptor<
  792. ArgumentAdapterT, T, ToTypes> &InnerMatcher) {
  793. return internal::TraversalWrapper<
  794. internal::ArgumentAdaptingMatcherFuncAdaptor<ArgumentAdapterT, T,
  795. ToTypes>>(TK, InnerMatcher);
  796. }
  797. template <template <typename T, typename... P> class MatcherT, typename... P,
  798. typename ReturnTypesF>
  799. internal::TraversalWrapper<
  800. internal::PolymorphicMatcher<MatcherT, ReturnTypesF, P...>>
  801. traverse(TraversalKind TK,
  802. const internal::PolymorphicMatcher<MatcherT, ReturnTypesF, P...>
  803. &InnerMatcher) {
  804. return internal::TraversalWrapper<
  805. internal::PolymorphicMatcher<MatcherT, ReturnTypesF, P...>>(TK,
  806. InnerMatcher);
  807. }
  808. template <typename... T>
  809. internal::Matcher<typename internal::GetClade<T...>::Type>
  810. traverse(TraversalKind TK, const internal::MapAnyOfHelper<T...> &InnerMatcher) {
  811. return traverse(TK, InnerMatcher.with());
  812. }
  813. /// Matches expressions that match InnerMatcher after any implicit AST
  814. /// nodes are stripped off.
  815. ///
  816. /// Parentheses and explicit casts are not discarded.
  817. /// Given
  818. /// \code
  819. /// class C {};
  820. /// C a = C();
  821. /// C b;
  822. /// C c = b;
  823. /// \endcode
  824. /// The matchers
  825. /// \code
  826. /// varDecl(hasInitializer(ignoringImplicit(cxxConstructExpr())))
  827. /// \endcode
  828. /// would match the declarations for a, b, and c.
  829. /// While
  830. /// \code
  831. /// varDecl(hasInitializer(cxxConstructExpr()))
  832. /// \endcode
  833. /// only match the declarations for b and c.
  834. AST_MATCHER_P(Expr, ignoringImplicit, internal::Matcher<Expr>,
  835. InnerMatcher) {
  836. return InnerMatcher.matches(*Node.IgnoreImplicit(), Finder, Builder);
  837. }
  838. /// Matches expressions that match InnerMatcher after any implicit casts
  839. /// are stripped off.
  840. ///
  841. /// Parentheses and explicit casts are not discarded.
  842. /// Given
  843. /// \code
  844. /// int arr[5];
  845. /// int a = 0;
  846. /// char b = 0;
  847. /// const int c = a;
  848. /// int *d = arr;
  849. /// long e = (long) 0l;
  850. /// \endcode
  851. /// The matchers
  852. /// \code
  853. /// varDecl(hasInitializer(ignoringImpCasts(integerLiteral())))
  854. /// varDecl(hasInitializer(ignoringImpCasts(declRefExpr())))
  855. /// \endcode
  856. /// would match the declarations for a, b, c, and d, but not e.
  857. /// While
  858. /// \code
  859. /// varDecl(hasInitializer(integerLiteral()))
  860. /// varDecl(hasInitializer(declRefExpr()))
  861. /// \endcode
  862. /// only match the declarations for a.
  863. AST_MATCHER_P(Expr, ignoringImpCasts,
  864. internal::Matcher<Expr>, InnerMatcher) {
  865. return InnerMatcher.matches(*Node.IgnoreImpCasts(), Finder, Builder);
  866. }
  867. /// Matches expressions that match InnerMatcher after parentheses and
  868. /// casts are stripped off.
  869. ///
  870. /// Implicit and non-C Style casts are also discarded.
  871. /// Given
  872. /// \code
  873. /// int a = 0;
  874. /// char b = (0);
  875. /// void* c = reinterpret_cast<char*>(0);
  876. /// char d = char(0);
  877. /// \endcode
  878. /// The matcher
  879. /// varDecl(hasInitializer(ignoringParenCasts(integerLiteral())))
  880. /// would match the declarations for a, b, c, and d.
  881. /// while
  882. /// varDecl(hasInitializer(integerLiteral()))
  883. /// only match the declaration for a.
  884. AST_MATCHER_P(Expr, ignoringParenCasts, internal::Matcher<Expr>, InnerMatcher) {
  885. return InnerMatcher.matches(*Node.IgnoreParenCasts(), Finder, Builder);
  886. }
  887. /// Matches expressions that match InnerMatcher after implicit casts and
  888. /// parentheses are stripped off.
  889. ///
  890. /// Explicit casts are not discarded.
  891. /// Given
  892. /// \code
  893. /// int arr[5];
  894. /// int a = 0;
  895. /// char b = (0);
  896. /// const int c = a;
  897. /// int *d = (arr);
  898. /// long e = ((long) 0l);
  899. /// \endcode
  900. /// The matchers
  901. /// varDecl(hasInitializer(ignoringParenImpCasts(integerLiteral())))
  902. /// varDecl(hasInitializer(ignoringParenImpCasts(declRefExpr())))
  903. /// would match the declarations for a, b, c, and d, but not e.
  904. /// while
  905. /// varDecl(hasInitializer(integerLiteral()))
  906. /// varDecl(hasInitializer(declRefExpr()))
  907. /// would only match the declaration for a.
  908. AST_MATCHER_P(Expr, ignoringParenImpCasts,
  909. internal::Matcher<Expr>, InnerMatcher) {
  910. return InnerMatcher.matches(*Node.IgnoreParenImpCasts(), Finder, Builder);
  911. }
  912. /// Matches types that match InnerMatcher after any parens are stripped.
  913. ///
  914. /// Given
  915. /// \code
  916. /// void (*fp)(void);
  917. /// \endcode
  918. /// The matcher
  919. /// \code
  920. /// varDecl(hasType(pointerType(pointee(ignoringParens(functionType())))))
  921. /// \endcode
  922. /// would match the declaration for fp.
  923. AST_MATCHER_P_OVERLOAD(QualType, ignoringParens, internal::Matcher<QualType>,
  924. InnerMatcher, 0) {
  925. return InnerMatcher.matches(Node.IgnoreParens(), Finder, Builder);
  926. }
  927. /// Overload \c ignoringParens for \c Expr.
  928. ///
  929. /// Given
  930. /// \code
  931. /// const char* str = ("my-string");
  932. /// \endcode
  933. /// The matcher
  934. /// \code
  935. /// implicitCastExpr(hasSourceExpression(ignoringParens(stringLiteral())))
  936. /// \endcode
  937. /// would match the implicit cast resulting from the assignment.
  938. AST_MATCHER_P_OVERLOAD(Expr, ignoringParens, internal::Matcher<Expr>,
  939. InnerMatcher, 1) {
  940. const Expr *E = Node.IgnoreParens();
  941. return InnerMatcher.matches(*E, Finder, Builder);
  942. }
  943. /// Matches expressions that are instantiation-dependent even if it is
  944. /// neither type- nor value-dependent.
  945. ///
  946. /// In the following example, the expression sizeof(sizeof(T() + T()))
  947. /// is instantiation-dependent (since it involves a template parameter T),
  948. /// but is neither type- nor value-dependent, since the type of the inner
  949. /// sizeof is known (std::size_t) and therefore the size of the outer
  950. /// sizeof is known.
  951. /// \code
  952. /// template<typename T>
  953. /// void f(T x, T y) { sizeof(sizeof(T() + T()); }
  954. /// \endcode
  955. /// expr(isInstantiationDependent()) matches sizeof(sizeof(T() + T())
  956. AST_MATCHER(Expr, isInstantiationDependent) {
  957. return Node.isInstantiationDependent();
  958. }
  959. /// Matches expressions that are type-dependent because the template type
  960. /// is not yet instantiated.
  961. ///
  962. /// For example, the expressions "x" and "x + y" are type-dependent in
  963. /// the following code, but "y" is not type-dependent:
  964. /// \code
  965. /// template<typename T>
  966. /// void add(T x, int y) {
  967. /// x + y;
  968. /// }
  969. /// \endcode
  970. /// expr(isTypeDependent()) matches x + y
  971. AST_MATCHER(Expr, isTypeDependent) { return Node.isTypeDependent(); }
  972. /// Matches expression that are value-dependent because they contain a
  973. /// non-type template parameter.
  974. ///
  975. /// For example, the array bound of "Chars" in the following example is
  976. /// value-dependent.
  977. /// \code
  978. /// template<int Size> int f() { return Size; }
  979. /// \endcode
  980. /// expr(isValueDependent()) matches return Size
  981. AST_MATCHER(Expr, isValueDependent) { return Node.isValueDependent(); }
  982. /// Matches classTemplateSpecializations, templateSpecializationType and
  983. /// functionDecl where the n'th TemplateArgument matches the given InnerMatcher.
  984. ///
  985. /// Given
  986. /// \code
  987. /// template<typename T, typename U> class A {};
  988. /// A<bool, int> b;
  989. /// A<int, bool> c;
  990. ///
  991. /// template<typename T> void f() {}
  992. /// void func() { f<int>(); };
  993. /// \endcode
  994. /// classTemplateSpecializationDecl(hasTemplateArgument(
  995. /// 1, refersToType(asString("int"))))
  996. /// matches the specialization \c A<bool, int>
  997. ///
  998. /// functionDecl(hasTemplateArgument(0, refersToType(asString("int"))))
  999. /// matches the specialization \c f<int>
  1000. AST_POLYMORPHIC_MATCHER_P2(
  1001. hasTemplateArgument,
  1002. AST_POLYMORPHIC_SUPPORTED_TYPES(ClassTemplateSpecializationDecl,
  1003. TemplateSpecializationType,
  1004. FunctionDecl),
  1005. unsigned, N, internal::Matcher<TemplateArgument>, InnerMatcher) {
  1006. ArrayRef<TemplateArgument> List =
  1007. internal::getTemplateSpecializationArgs(Node);
  1008. if (List.size() <= N)
  1009. return false;
  1010. return InnerMatcher.matches(List[N], Finder, Builder);
  1011. }
  1012. /// Matches if the number of template arguments equals \p N.
  1013. ///
  1014. /// Given
  1015. /// \code
  1016. /// template<typename T> struct C {};
  1017. /// C<int> c;
  1018. /// \endcode
  1019. /// classTemplateSpecializationDecl(templateArgumentCountIs(1))
  1020. /// matches C<int>.
  1021. AST_POLYMORPHIC_MATCHER_P(
  1022. templateArgumentCountIs,
  1023. AST_POLYMORPHIC_SUPPORTED_TYPES(ClassTemplateSpecializationDecl,
  1024. TemplateSpecializationType),
  1025. unsigned, N) {
  1026. return internal::getTemplateSpecializationArgs(Node).size() == N;
  1027. }
  1028. /// Matches a TemplateArgument that refers to a certain type.
  1029. ///
  1030. /// Given
  1031. /// \code
  1032. /// struct X {};
  1033. /// template<typename T> struct A {};
  1034. /// A<X> a;
  1035. /// \endcode
  1036. /// classTemplateSpecializationDecl(hasAnyTemplateArgument(
  1037. /// refersToType(class(hasName("X")))))
  1038. /// matches the specialization \c A<X>
  1039. AST_MATCHER_P(TemplateArgument, refersToType,
  1040. internal::Matcher<QualType>, InnerMatcher) {
  1041. if (Node.getKind() != TemplateArgument::Type)
  1042. return false;
  1043. return InnerMatcher.matches(Node.getAsType(), Finder, Builder);
  1044. }
  1045. /// Matches a TemplateArgument that refers to a certain template.
  1046. ///
  1047. /// Given
  1048. /// \code
  1049. /// template<template <typename> class S> class X {};
  1050. /// template<typename T> class Y {};
  1051. /// X<Y> xi;
  1052. /// \endcode
  1053. /// classTemplateSpecializationDecl(hasAnyTemplateArgument(
  1054. /// refersToTemplate(templateName())))
  1055. /// matches the specialization \c X<Y>
  1056. AST_MATCHER_P(TemplateArgument, refersToTemplate,
  1057. internal::Matcher<TemplateName>, InnerMatcher) {
  1058. if (Node.getKind() != TemplateArgument::Template)
  1059. return false;
  1060. return InnerMatcher.matches(Node.getAsTemplate(), Finder, Builder);
  1061. }
  1062. /// Matches a canonical TemplateArgument that refers to a certain
  1063. /// declaration.
  1064. ///
  1065. /// Given
  1066. /// \code
  1067. /// struct B { int next; };
  1068. /// template<int(B::*next_ptr)> struct A {};
  1069. /// A<&B::next> a;
  1070. /// \endcode
  1071. /// classTemplateSpecializationDecl(hasAnyTemplateArgument(
  1072. /// refersToDeclaration(fieldDecl(hasName("next")))))
  1073. /// matches the specialization \c A<&B::next> with \c fieldDecl(...) matching
  1074. /// \c B::next
  1075. AST_MATCHER_P(TemplateArgument, refersToDeclaration,
  1076. internal::Matcher<Decl>, InnerMatcher) {
  1077. if (Node.getKind() == TemplateArgument::Declaration)
  1078. return InnerMatcher.matches(*Node.getAsDecl(), Finder, Builder);
  1079. return false;
  1080. }
  1081. /// Matches a sugar TemplateArgument that refers to a certain expression.
  1082. ///
  1083. /// Given
  1084. /// \code
  1085. /// struct B { int next; };
  1086. /// template<int(B::*next_ptr)> struct A {};
  1087. /// A<&B::next> a;
  1088. /// \endcode
  1089. /// templateSpecializationType(hasAnyTemplateArgument(
  1090. /// isExpr(hasDescendant(declRefExpr(to(fieldDecl(hasName("next"))))))))
  1091. /// matches the specialization \c A<&B::next> with \c fieldDecl(...) matching
  1092. /// \c B::next
  1093. AST_MATCHER_P(TemplateArgument, isExpr, internal::Matcher<Expr>, InnerMatcher) {
  1094. if (Node.getKind() == TemplateArgument::Expression)
  1095. return InnerMatcher.matches(*Node.getAsExpr(), Finder, Builder);
  1096. return false;
  1097. }
  1098. /// Matches a TemplateArgument that is an integral value.
  1099. ///
  1100. /// Given
  1101. /// \code
  1102. /// template<int T> struct C {};
  1103. /// C<42> c;
  1104. /// \endcode
  1105. /// classTemplateSpecializationDecl(
  1106. /// hasAnyTemplateArgument(isIntegral()))
  1107. /// matches the implicit instantiation of C in C<42>
  1108. /// with isIntegral() matching 42.
  1109. AST_MATCHER(TemplateArgument, isIntegral) {
  1110. return Node.getKind() == TemplateArgument::Integral;
  1111. }
  1112. /// Matches a TemplateArgument that refers to an integral type.
  1113. ///
  1114. /// Given
  1115. /// \code
  1116. /// template<int T> struct C {};
  1117. /// C<42> c;
  1118. /// \endcode
  1119. /// classTemplateSpecializationDecl(
  1120. /// hasAnyTemplateArgument(refersToIntegralType(asString("int"))))
  1121. /// matches the implicit instantiation of C in C<42>.
  1122. AST_MATCHER_P(TemplateArgument, refersToIntegralType,
  1123. internal::Matcher<QualType>, InnerMatcher) {
  1124. if (Node.getKind() != TemplateArgument::Integral)
  1125. return false;
  1126. return InnerMatcher.matches(Node.getIntegralType(), Finder, Builder);
  1127. }
  1128. /// Matches a TemplateArgument of integral type with a given value.
  1129. ///
  1130. /// Note that 'Value' is a string as the template argument's value is
  1131. /// an arbitrary precision integer. 'Value' must be euqal to the canonical
  1132. /// representation of that integral value in base 10.
  1133. ///
  1134. /// Given
  1135. /// \code
  1136. /// template<int T> struct C {};
  1137. /// C<42> c;
  1138. /// \endcode
  1139. /// classTemplateSpecializationDecl(
  1140. /// hasAnyTemplateArgument(equalsIntegralValue("42")))
  1141. /// matches the implicit instantiation of C in C<42>.
  1142. AST_MATCHER_P(TemplateArgument, equalsIntegralValue,
  1143. std::string, Value) {
  1144. if (Node.getKind() != TemplateArgument::Integral)
  1145. return false;
  1146. return toString(Node.getAsIntegral(), 10) == Value;
  1147. }
  1148. /// Matches an Objective-C autorelease pool statement.
  1149. ///
  1150. /// Given
  1151. /// \code
  1152. /// @autoreleasepool {
  1153. /// int x = 0;
  1154. /// }
  1155. /// \endcode
  1156. /// autoreleasePoolStmt(stmt()) matches the declaration of "x"
  1157. /// inside the autorelease pool.
  1158. extern const internal::VariadicDynCastAllOfMatcher<Stmt,
  1159. ObjCAutoreleasePoolStmt> autoreleasePoolStmt;
  1160. /// Matches any value declaration.
  1161. ///
  1162. /// Example matches A, B, C and F
  1163. /// \code
  1164. /// enum X { A, B, C };
  1165. /// void F();
  1166. /// \endcode
  1167. extern const internal::VariadicDynCastAllOfMatcher<Decl, ValueDecl> valueDecl;
  1168. /// Matches C++ constructor declarations.
  1169. ///
  1170. /// Example matches Foo::Foo() and Foo::Foo(int)
  1171. /// \code
  1172. /// class Foo {
  1173. /// public:
  1174. /// Foo();
  1175. /// Foo(int);
  1176. /// int DoSomething();
  1177. /// };
  1178. /// \endcode
  1179. extern const internal::VariadicDynCastAllOfMatcher<Decl, CXXConstructorDecl>
  1180. cxxConstructorDecl;
  1181. /// Matches explicit C++ destructor declarations.
  1182. ///
  1183. /// Example matches Foo::~Foo()
  1184. /// \code
  1185. /// class Foo {
  1186. /// public:
  1187. /// virtual ~Foo();
  1188. /// };
  1189. /// \endcode
  1190. extern const internal::VariadicDynCastAllOfMatcher<Decl, CXXDestructorDecl>
  1191. cxxDestructorDecl;
  1192. /// Matches enum declarations.
  1193. ///
  1194. /// Example matches X
  1195. /// \code
  1196. /// enum X {
  1197. /// A, B, C
  1198. /// };
  1199. /// \endcode
  1200. extern const internal::VariadicDynCastAllOfMatcher<Decl, EnumDecl> enumDecl;
  1201. /// Matches enum constants.
  1202. ///
  1203. /// Example matches A, B, C
  1204. /// \code
  1205. /// enum X {
  1206. /// A, B, C
  1207. /// };
  1208. /// \endcode
  1209. extern const internal::VariadicDynCastAllOfMatcher<Decl, EnumConstantDecl>
  1210. enumConstantDecl;
  1211. /// Matches tag declarations.
  1212. ///
  1213. /// Example matches X, Z, U, S, E
  1214. /// \code
  1215. /// class X;
  1216. /// template<class T> class Z {};
  1217. /// struct S {};
  1218. /// union U {};
  1219. /// enum E {
  1220. /// A, B, C
  1221. /// };
  1222. /// \endcode
  1223. extern const internal::VariadicDynCastAllOfMatcher<Decl, TagDecl> tagDecl;
  1224. /// Matches method declarations.
  1225. ///
  1226. /// Example matches y
  1227. /// \code
  1228. /// class X { void y(); };
  1229. /// \endcode
  1230. extern const internal::VariadicDynCastAllOfMatcher<Decl, CXXMethodDecl>
  1231. cxxMethodDecl;
  1232. /// Matches conversion operator declarations.
  1233. ///
  1234. /// Example matches the operator.
  1235. /// \code
  1236. /// class X { operator int() const; };
  1237. /// \endcode
  1238. extern const internal::VariadicDynCastAllOfMatcher<Decl, CXXConversionDecl>
  1239. cxxConversionDecl;
  1240. /// Matches user-defined and implicitly generated deduction guide.
  1241. ///
  1242. /// Example matches the deduction guide.
  1243. /// \code
  1244. /// template<typename T>
  1245. /// class X { X(int) };
  1246. /// X(int) -> X<int>;
  1247. /// \endcode
  1248. extern const internal::VariadicDynCastAllOfMatcher<Decl, CXXDeductionGuideDecl>
  1249. cxxDeductionGuideDecl;
  1250. /// Matches variable declarations.
  1251. ///
  1252. /// Note: this does not match declarations of member variables, which are
  1253. /// "field" declarations in Clang parlance.
  1254. ///
  1255. /// Example matches a
  1256. /// \code
  1257. /// int a;
  1258. /// \endcode
  1259. extern const internal::VariadicDynCastAllOfMatcher<Decl, VarDecl> varDecl;
  1260. /// Matches field declarations.
  1261. ///
  1262. /// Given
  1263. /// \code
  1264. /// class X { int m; };
  1265. /// \endcode
  1266. /// fieldDecl()
  1267. /// matches 'm'.
  1268. extern const internal::VariadicDynCastAllOfMatcher<Decl, FieldDecl> fieldDecl;
  1269. /// Matches indirect field declarations.
  1270. ///
  1271. /// Given
  1272. /// \code
  1273. /// struct X { struct { int a; }; };
  1274. /// \endcode
  1275. /// indirectFieldDecl()
  1276. /// matches 'a'.
  1277. extern const internal::VariadicDynCastAllOfMatcher<Decl, IndirectFieldDecl>
  1278. indirectFieldDecl;
  1279. /// Matches function declarations.
  1280. ///
  1281. /// Example matches f
  1282. /// \code
  1283. /// void f();
  1284. /// \endcode
  1285. extern const internal::VariadicDynCastAllOfMatcher<Decl, FunctionDecl>
  1286. functionDecl;
  1287. /// Matches C++ function template declarations.
  1288. ///
  1289. /// Example matches f
  1290. /// \code
  1291. /// template<class T> void f(T t) {}
  1292. /// \endcode
  1293. extern const internal::VariadicDynCastAllOfMatcher<Decl, FunctionTemplateDecl>
  1294. functionTemplateDecl;
  1295. /// Matches friend declarations.
  1296. ///
  1297. /// Given
  1298. /// \code
  1299. /// class X { friend void foo(); };
  1300. /// \endcode
  1301. /// friendDecl()
  1302. /// matches 'friend void foo()'.
  1303. extern const internal::VariadicDynCastAllOfMatcher<Decl, FriendDecl> friendDecl;
  1304. /// Matches statements.
  1305. ///
  1306. /// Given
  1307. /// \code
  1308. /// { ++a; }
  1309. /// \endcode
  1310. /// stmt()
  1311. /// matches both the compound statement '{ ++a; }' and '++a'.
  1312. extern const internal::VariadicAllOfMatcher<Stmt> stmt;
  1313. /// Matches declaration statements.
  1314. ///
  1315. /// Given
  1316. /// \code
  1317. /// int a;
  1318. /// \endcode
  1319. /// declStmt()
  1320. /// matches 'int a'.
  1321. extern const internal::VariadicDynCastAllOfMatcher<Stmt, DeclStmt> declStmt;
  1322. /// Matches member expressions.
  1323. ///
  1324. /// Given
  1325. /// \code
  1326. /// class Y {
  1327. /// void x() { this->x(); x(); Y y; y.x(); a; this->b; Y::b; }
  1328. /// int a; static int b;
  1329. /// };
  1330. /// \endcode
  1331. /// memberExpr()
  1332. /// matches this->x, x, y.x, a, this->b
  1333. extern const internal::VariadicDynCastAllOfMatcher<Stmt, MemberExpr> memberExpr;
  1334. /// Matches unresolved member expressions.
  1335. ///
  1336. /// Given
  1337. /// \code
  1338. /// struct X {
  1339. /// template <class T> void f();
  1340. /// void g();
  1341. /// };
  1342. /// template <class T> void h() { X x; x.f<T>(); x.g(); }
  1343. /// \endcode
  1344. /// unresolvedMemberExpr()
  1345. /// matches x.f<T>
  1346. extern const internal::VariadicDynCastAllOfMatcher<Stmt, UnresolvedMemberExpr>
  1347. unresolvedMemberExpr;
  1348. /// Matches member expressions where the actual member referenced could not be
  1349. /// resolved because the base expression or the member name was dependent.
  1350. ///
  1351. /// Given
  1352. /// \code
  1353. /// template <class T> void f() { T t; t.g(); }
  1354. /// \endcode
  1355. /// cxxDependentScopeMemberExpr()
  1356. /// matches t.g
  1357. extern const internal::VariadicDynCastAllOfMatcher<Stmt,
  1358. CXXDependentScopeMemberExpr>
  1359. cxxDependentScopeMemberExpr;
  1360. /// Matches call expressions.
  1361. ///
  1362. /// Example matches x.y() and y()
  1363. /// \code
  1364. /// X x;
  1365. /// x.y();
  1366. /// y();
  1367. /// \endcode
  1368. extern const internal::VariadicDynCastAllOfMatcher<Stmt, CallExpr> callExpr;
  1369. /// Matches call expressions which were resolved using ADL.
  1370. ///
  1371. /// Example matches y(x) but not y(42) or NS::y(x).
  1372. /// \code
  1373. /// namespace NS {
  1374. /// struct X {};
  1375. /// void y(X);
  1376. /// }
  1377. ///
  1378. /// void y(...);
  1379. ///
  1380. /// void test() {
  1381. /// NS::X x;
  1382. /// y(x); // Matches
  1383. /// NS::y(x); // Doesn't match
  1384. /// y(42); // Doesn't match
  1385. /// using NS::y;
  1386. /// y(x); // Found by both unqualified lookup and ADL, doesn't match
  1387. // }
  1388. /// \endcode
  1389. AST_MATCHER(CallExpr, usesADL) { return Node.usesADL(); }
  1390. /// Matches lambda expressions.
  1391. ///
  1392. /// Example matches [&](){return 5;}
  1393. /// \code
  1394. /// [&](){return 5;}
  1395. /// \endcode
  1396. extern const internal::VariadicDynCastAllOfMatcher<Stmt, LambdaExpr> lambdaExpr;
  1397. /// Matches member call expressions.
  1398. ///
  1399. /// Example matches x.y()
  1400. /// \code
  1401. /// X x;
  1402. /// x.y();
  1403. /// \endcode
  1404. extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXMemberCallExpr>
  1405. cxxMemberCallExpr;
  1406. /// Matches ObjectiveC Message invocation expressions.
  1407. ///
  1408. /// The innermost message send invokes the "alloc" class method on the
  1409. /// NSString class, while the outermost message send invokes the
  1410. /// "initWithString" instance method on the object returned from
  1411. /// NSString's "alloc". This matcher should match both message sends.
  1412. /// \code
  1413. /// [[NSString alloc] initWithString:@"Hello"]
  1414. /// \endcode
  1415. extern const internal::VariadicDynCastAllOfMatcher<Stmt, ObjCMessageExpr>
  1416. objcMessageExpr;
  1417. /// Matches Objective-C interface declarations.
  1418. ///
  1419. /// Example matches Foo
  1420. /// \code
  1421. /// @interface Foo
  1422. /// @end
  1423. /// \endcode
  1424. extern const internal::VariadicDynCastAllOfMatcher<Decl, ObjCInterfaceDecl>
  1425. objcInterfaceDecl;
  1426. /// Matches Objective-C implementation declarations.
  1427. ///
  1428. /// Example matches Foo
  1429. /// \code
  1430. /// @implementation Foo
  1431. /// @end
  1432. /// \endcode
  1433. extern const internal::VariadicDynCastAllOfMatcher<Decl, ObjCImplementationDecl>
  1434. objcImplementationDecl;
  1435. /// Matches Objective-C protocol declarations.
  1436. ///
  1437. /// Example matches FooDelegate
  1438. /// \code
  1439. /// @protocol FooDelegate
  1440. /// @end
  1441. /// \endcode
  1442. extern const internal::VariadicDynCastAllOfMatcher<Decl, ObjCProtocolDecl>
  1443. objcProtocolDecl;
  1444. /// Matches Objective-C category declarations.
  1445. ///
  1446. /// Example matches Foo (Additions)
  1447. /// \code
  1448. /// @interface Foo (Additions)
  1449. /// @end
  1450. /// \endcode
  1451. extern const internal::VariadicDynCastAllOfMatcher<Decl, ObjCCategoryDecl>
  1452. objcCategoryDecl;
  1453. /// Matches Objective-C category definitions.
  1454. ///
  1455. /// Example matches Foo (Additions)
  1456. /// \code
  1457. /// @implementation Foo (Additions)
  1458. /// @end
  1459. /// \endcode
  1460. extern const internal::VariadicDynCastAllOfMatcher<Decl, ObjCCategoryImplDecl>
  1461. objcCategoryImplDecl;
  1462. /// Matches Objective-C method declarations.
  1463. ///
  1464. /// Example matches both declaration and definition of -[Foo method]
  1465. /// \code
  1466. /// @interface Foo
  1467. /// - (void)method;
  1468. /// @end
  1469. ///
  1470. /// @implementation Foo
  1471. /// - (void)method {}
  1472. /// @end
  1473. /// \endcode
  1474. extern const internal::VariadicDynCastAllOfMatcher<Decl, ObjCMethodDecl>
  1475. objcMethodDecl;
  1476. /// Matches block declarations.
  1477. ///
  1478. /// Example matches the declaration of the nameless block printing an input
  1479. /// integer.
  1480. ///
  1481. /// \code
  1482. /// myFunc(^(int p) {
  1483. /// printf("%d", p);
  1484. /// })
  1485. /// \endcode
  1486. extern const internal::VariadicDynCastAllOfMatcher<Decl, BlockDecl>
  1487. blockDecl;
  1488. /// Matches Objective-C instance variable declarations.
  1489. ///
  1490. /// Example matches _enabled
  1491. /// \code
  1492. /// @implementation Foo {
  1493. /// BOOL _enabled;
  1494. /// }
  1495. /// @end
  1496. /// \endcode
  1497. extern const internal::VariadicDynCastAllOfMatcher<Decl, ObjCIvarDecl>
  1498. objcIvarDecl;
  1499. /// Matches Objective-C property declarations.
  1500. ///
  1501. /// Example matches enabled
  1502. /// \code
  1503. /// @interface Foo
  1504. /// @property BOOL enabled;
  1505. /// @end
  1506. /// \endcode
  1507. extern const internal::VariadicDynCastAllOfMatcher<Decl, ObjCPropertyDecl>
  1508. objcPropertyDecl;
  1509. /// Matches Objective-C \@throw statements.
  1510. ///
  1511. /// Example matches \@throw
  1512. /// \code
  1513. /// @throw obj;
  1514. /// \endcode
  1515. extern const internal::VariadicDynCastAllOfMatcher<Stmt, ObjCAtThrowStmt>
  1516. objcThrowStmt;
  1517. /// Matches Objective-C @try statements.
  1518. ///
  1519. /// Example matches @try
  1520. /// \code
  1521. /// @try {}
  1522. /// @catch (...) {}
  1523. /// \endcode
  1524. extern const internal::VariadicDynCastAllOfMatcher<Stmt, ObjCAtTryStmt>
  1525. objcTryStmt;
  1526. /// Matches Objective-C @catch statements.
  1527. ///
  1528. /// Example matches @catch
  1529. /// \code
  1530. /// @try {}
  1531. /// @catch (...) {}
  1532. /// \endcode
  1533. extern const internal::VariadicDynCastAllOfMatcher<Stmt, ObjCAtCatchStmt>
  1534. objcCatchStmt;
  1535. /// Matches Objective-C @finally statements.
  1536. ///
  1537. /// Example matches @finally
  1538. /// \code
  1539. /// @try {}
  1540. /// @finally {}
  1541. /// \endcode
  1542. extern const internal::VariadicDynCastAllOfMatcher<Stmt, ObjCAtFinallyStmt>
  1543. objcFinallyStmt;
  1544. /// Matches expressions that introduce cleanups to be run at the end
  1545. /// of the sub-expression's evaluation.
  1546. ///
  1547. /// Example matches std::string()
  1548. /// \code
  1549. /// const std::string str = std::string();
  1550. /// \endcode
  1551. extern const internal::VariadicDynCastAllOfMatcher<Stmt, ExprWithCleanups>
  1552. exprWithCleanups;
  1553. /// Matches init list expressions.
  1554. ///
  1555. /// Given
  1556. /// \code
  1557. /// int a[] = { 1, 2 };
  1558. /// struct B { int x, y; };
  1559. /// B b = { 5, 6 };
  1560. /// \endcode
  1561. /// initListExpr()
  1562. /// matches "{ 1, 2 }" and "{ 5, 6 }"
  1563. extern const internal::VariadicDynCastAllOfMatcher<Stmt, InitListExpr>
  1564. initListExpr;
  1565. /// Matches the syntactic form of init list expressions
  1566. /// (if expression have it).
  1567. AST_MATCHER_P(InitListExpr, hasSyntacticForm,
  1568. internal::Matcher<Expr>, InnerMatcher) {
  1569. const Expr *SyntForm = Node.getSyntacticForm();
  1570. return (SyntForm != nullptr &&
  1571. InnerMatcher.matches(*SyntForm, Finder, Builder));
  1572. }
  1573. /// Matches C++ initializer list expressions.
  1574. ///
  1575. /// Given
  1576. /// \code
  1577. /// std::vector<int> a({ 1, 2, 3 });
  1578. /// std::vector<int> b = { 4, 5 };
  1579. /// int c[] = { 6, 7 };
  1580. /// std::pair<int, int> d = { 8, 9 };
  1581. /// \endcode
  1582. /// cxxStdInitializerListExpr()
  1583. /// matches "{ 1, 2, 3 }" and "{ 4, 5 }"
  1584. extern const internal::VariadicDynCastAllOfMatcher<Stmt,
  1585. CXXStdInitializerListExpr>
  1586. cxxStdInitializerListExpr;
  1587. /// Matches implicit initializers of init list expressions.
  1588. ///
  1589. /// Given
  1590. /// \code
  1591. /// point ptarray[10] = { [2].y = 1.0, [2].x = 2.0, [0].x = 1.0 };
  1592. /// \endcode
  1593. /// implicitValueInitExpr()
  1594. /// matches "[0].y" (implicitly)
  1595. extern const internal::VariadicDynCastAllOfMatcher<Stmt, ImplicitValueInitExpr>
  1596. implicitValueInitExpr;
  1597. /// Matches paren list expressions.
  1598. /// ParenListExprs don't have a predefined type and are used for late parsing.
  1599. /// In the final AST, they can be met in template declarations.
  1600. ///
  1601. /// Given
  1602. /// \code
  1603. /// template<typename T> class X {
  1604. /// void f() {
  1605. /// X x(*this);
  1606. /// int a = 0, b = 1; int i = (a, b);
  1607. /// }
  1608. /// };
  1609. /// \endcode
  1610. /// parenListExpr() matches "*this" but NOT matches (a, b) because (a, b)
  1611. /// has a predefined type and is a ParenExpr, not a ParenListExpr.
  1612. extern const internal::VariadicDynCastAllOfMatcher<Stmt, ParenListExpr>
  1613. parenListExpr;
  1614. /// Matches substitutions of non-type template parameters.
  1615. ///
  1616. /// Given
  1617. /// \code
  1618. /// template <int N>
  1619. /// struct A { static const int n = N; };
  1620. /// struct B : public A<42> {};
  1621. /// \endcode
  1622. /// substNonTypeTemplateParmExpr()
  1623. /// matches "N" in the right-hand side of "static const int n = N;"
  1624. extern const internal::VariadicDynCastAllOfMatcher<Stmt,
  1625. SubstNonTypeTemplateParmExpr>
  1626. substNonTypeTemplateParmExpr;
  1627. /// Matches using declarations.
  1628. ///
  1629. /// Given
  1630. /// \code
  1631. /// namespace X { int x; }
  1632. /// using X::x;
  1633. /// \endcode
  1634. /// usingDecl()
  1635. /// matches \code using X::x \endcode
  1636. extern const internal::VariadicDynCastAllOfMatcher<Decl, UsingDecl> usingDecl;
  1637. /// Matches using-enum declarations.
  1638. ///
  1639. /// Given
  1640. /// \code
  1641. /// namespace X { enum x {...}; }
  1642. /// using enum X::x;
  1643. /// \endcode
  1644. /// usingEnumDecl()
  1645. /// matches \code using enum X::x \endcode
  1646. extern const internal::VariadicDynCastAllOfMatcher<Decl, UsingEnumDecl>
  1647. usingEnumDecl;
  1648. /// Matches using namespace declarations.
  1649. ///
  1650. /// Given
  1651. /// \code
  1652. /// namespace X { int x; }
  1653. /// using namespace X;
  1654. /// \endcode
  1655. /// usingDirectiveDecl()
  1656. /// matches \code using namespace X \endcode
  1657. extern const internal::VariadicDynCastAllOfMatcher<Decl, UsingDirectiveDecl>
  1658. usingDirectiveDecl;
  1659. /// Matches reference to a name that can be looked up during parsing
  1660. /// but could not be resolved to a specific declaration.
  1661. ///
  1662. /// Given
  1663. /// \code
  1664. /// template<typename T>
  1665. /// T foo() { T a; return a; }
  1666. /// template<typename T>
  1667. /// void bar() {
  1668. /// foo<T>();
  1669. /// }
  1670. /// \endcode
  1671. /// unresolvedLookupExpr()
  1672. /// matches \code foo<T>() \endcode
  1673. extern const internal::VariadicDynCastAllOfMatcher<Stmt, UnresolvedLookupExpr>
  1674. unresolvedLookupExpr;
  1675. /// Matches unresolved using value declarations.
  1676. ///
  1677. /// Given
  1678. /// \code
  1679. /// template<typename X>
  1680. /// class C : private X {
  1681. /// using X::x;
  1682. /// };
  1683. /// \endcode
  1684. /// unresolvedUsingValueDecl()
  1685. /// matches \code using X::x \endcode
  1686. extern const internal::VariadicDynCastAllOfMatcher<Decl,
  1687. UnresolvedUsingValueDecl>
  1688. unresolvedUsingValueDecl;
  1689. /// Matches unresolved using value declarations that involve the
  1690. /// typename.
  1691. ///
  1692. /// Given
  1693. /// \code
  1694. /// template <typename T>
  1695. /// struct Base { typedef T Foo; };
  1696. ///
  1697. /// template<typename T>
  1698. /// struct S : private Base<T> {
  1699. /// using typename Base<T>::Foo;
  1700. /// };
  1701. /// \endcode
  1702. /// unresolvedUsingTypenameDecl()
  1703. /// matches \code using Base<T>::Foo \endcode
  1704. extern const internal::VariadicDynCastAllOfMatcher<Decl,
  1705. UnresolvedUsingTypenameDecl>
  1706. unresolvedUsingTypenameDecl;
  1707. /// Matches a constant expression wrapper.
  1708. ///
  1709. /// Example matches the constant in the case statement:
  1710. /// (matcher = constantExpr())
  1711. /// \code
  1712. /// switch (a) {
  1713. /// case 37: break;
  1714. /// }
  1715. /// \endcode
  1716. extern const internal::VariadicDynCastAllOfMatcher<Stmt, ConstantExpr>
  1717. constantExpr;
  1718. /// Matches parentheses used in expressions.
  1719. ///
  1720. /// Example matches (foo() + 1)
  1721. /// \code
  1722. /// int foo() { return 1; }
  1723. /// int a = (foo() + 1);
  1724. /// \endcode
  1725. extern const internal::VariadicDynCastAllOfMatcher<Stmt, ParenExpr> parenExpr;
  1726. /// Matches constructor call expressions (including implicit ones).
  1727. ///
  1728. /// Example matches string(ptr, n) and ptr within arguments of f
  1729. /// (matcher = cxxConstructExpr())
  1730. /// \code
  1731. /// void f(const string &a, const string &b);
  1732. /// char *ptr;
  1733. /// int n;
  1734. /// f(string(ptr, n), ptr);
  1735. /// \endcode
  1736. extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXConstructExpr>
  1737. cxxConstructExpr;
  1738. /// Matches unresolved constructor call expressions.
  1739. ///
  1740. /// Example matches T(t) in return statement of f
  1741. /// (matcher = cxxUnresolvedConstructExpr())
  1742. /// \code
  1743. /// template <typename T>
  1744. /// void f(const T& t) { return T(t); }
  1745. /// \endcode
  1746. extern const internal::VariadicDynCastAllOfMatcher<Stmt,
  1747. CXXUnresolvedConstructExpr>
  1748. cxxUnresolvedConstructExpr;
  1749. /// Matches implicit and explicit this expressions.
  1750. ///
  1751. /// Example matches the implicit this expression in "return i".
  1752. /// (matcher = cxxThisExpr())
  1753. /// \code
  1754. /// struct foo {
  1755. /// int i;
  1756. /// int f() { return i; }
  1757. /// };
  1758. /// \endcode
  1759. extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXThisExpr>
  1760. cxxThisExpr;
  1761. /// Matches nodes where temporaries are created.
  1762. ///
  1763. /// Example matches FunctionTakesString(GetStringByValue())
  1764. /// (matcher = cxxBindTemporaryExpr())
  1765. /// \code
  1766. /// FunctionTakesString(GetStringByValue());
  1767. /// FunctionTakesStringByPointer(GetStringPointer());
  1768. /// \endcode
  1769. extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXBindTemporaryExpr>
  1770. cxxBindTemporaryExpr;
  1771. /// Matches nodes where temporaries are materialized.
  1772. ///
  1773. /// Example: Given
  1774. /// \code
  1775. /// struct T {void func();};
  1776. /// T f();
  1777. /// void g(T);
  1778. /// \endcode
  1779. /// materializeTemporaryExpr() matches 'f()' in these statements
  1780. /// \code
  1781. /// T u(f());
  1782. /// g(f());
  1783. /// f().func();
  1784. /// \endcode
  1785. /// but does not match
  1786. /// \code
  1787. /// f();
  1788. /// \endcode
  1789. extern const internal::VariadicDynCastAllOfMatcher<Stmt,
  1790. MaterializeTemporaryExpr>
  1791. materializeTemporaryExpr;
  1792. /// Matches new expressions.
  1793. ///
  1794. /// Given
  1795. /// \code
  1796. /// new X;
  1797. /// \endcode
  1798. /// cxxNewExpr()
  1799. /// matches 'new X'.
  1800. extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXNewExpr> cxxNewExpr;
  1801. /// Matches delete expressions.
  1802. ///
  1803. /// Given
  1804. /// \code
  1805. /// delete X;
  1806. /// \endcode
  1807. /// cxxDeleteExpr()
  1808. /// matches 'delete X'.
  1809. extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXDeleteExpr>
  1810. cxxDeleteExpr;
  1811. /// Matches noexcept expressions.
  1812. ///
  1813. /// Given
  1814. /// \code
  1815. /// bool a() noexcept;
  1816. /// bool b() noexcept(true);
  1817. /// bool c() noexcept(false);
  1818. /// bool d() noexcept(noexcept(a()));
  1819. /// bool e = noexcept(b()) || noexcept(c());
  1820. /// \endcode
  1821. /// cxxNoexceptExpr()
  1822. /// matches `noexcept(a())`, `noexcept(b())` and `noexcept(c())`.
  1823. /// doesn't match the noexcept specifier in the declarations a, b, c or d.
  1824. extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXNoexceptExpr>
  1825. cxxNoexceptExpr;
  1826. /// Matches array subscript expressions.
  1827. ///
  1828. /// Given
  1829. /// \code
  1830. /// int i = a[1];
  1831. /// \endcode
  1832. /// arraySubscriptExpr()
  1833. /// matches "a[1]"
  1834. extern const internal::VariadicDynCastAllOfMatcher<Stmt, ArraySubscriptExpr>
  1835. arraySubscriptExpr;
  1836. /// Matches the value of a default argument at the call site.
  1837. ///
  1838. /// Example matches the CXXDefaultArgExpr placeholder inserted for the
  1839. /// default value of the second parameter in the call expression f(42)
  1840. /// (matcher = cxxDefaultArgExpr())
  1841. /// \code
  1842. /// void f(int x, int y = 0);
  1843. /// f(42);
  1844. /// \endcode
  1845. extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXDefaultArgExpr>
  1846. cxxDefaultArgExpr;
  1847. /// Matches overloaded operator calls.
  1848. ///
  1849. /// Note that if an operator isn't overloaded, it won't match. Instead, use
  1850. /// binaryOperator matcher.
  1851. /// Currently it does not match operators such as new delete.
  1852. /// FIXME: figure out why these do not match?
  1853. ///
  1854. /// Example matches both operator<<((o << b), c) and operator<<(o, b)
  1855. /// (matcher = cxxOperatorCallExpr())
  1856. /// \code
  1857. /// ostream &operator<< (ostream &out, int i) { };
  1858. /// ostream &o; int b = 1, c = 1;
  1859. /// o << b << c;
  1860. /// \endcode
  1861. /// See also the binaryOperation() matcher for more-general matching of binary
  1862. /// uses of this AST node.
  1863. extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXOperatorCallExpr>
  1864. cxxOperatorCallExpr;
  1865. /// Matches rewritten binary operators
  1866. ///
  1867. /// Example matches use of "<":
  1868. /// \code
  1869. /// #include <compare>
  1870. /// struct HasSpaceshipMem {
  1871. /// int a;
  1872. /// constexpr auto operator<=>(const HasSpaceshipMem&) const = default;
  1873. /// };
  1874. /// void compare() {
  1875. /// HasSpaceshipMem hs1, hs2;
  1876. /// if (hs1 < hs2)
  1877. /// return;
  1878. /// }
  1879. /// \endcode
  1880. /// See also the binaryOperation() matcher for more-general matching
  1881. /// of this AST node.
  1882. extern const internal::VariadicDynCastAllOfMatcher<Stmt,
  1883. CXXRewrittenBinaryOperator>
  1884. cxxRewrittenBinaryOperator;
  1885. /// Matches expressions.
  1886. ///
  1887. /// Example matches x()
  1888. /// \code
  1889. /// void f() { x(); }
  1890. /// \endcode
  1891. extern const internal::VariadicDynCastAllOfMatcher<Stmt, Expr> expr;
  1892. /// Matches expressions that refer to declarations.
  1893. ///
  1894. /// Example matches x in if (x)
  1895. /// \code
  1896. /// bool x;
  1897. /// if (x) {}
  1898. /// \endcode
  1899. extern const internal::VariadicDynCastAllOfMatcher<Stmt, DeclRefExpr>
  1900. declRefExpr;
  1901. /// Matches a reference to an ObjCIvar.
  1902. ///
  1903. /// Example: matches "a" in "init" method:
  1904. /// \code
  1905. /// @implementation A {
  1906. /// NSString *a;
  1907. /// }
  1908. /// - (void) init {
  1909. /// a = @"hello";
  1910. /// }
  1911. /// \endcode
  1912. extern const internal::VariadicDynCastAllOfMatcher<Stmt, ObjCIvarRefExpr>
  1913. objcIvarRefExpr;
  1914. /// Matches a reference to a block.
  1915. ///
  1916. /// Example: matches "^{}":
  1917. /// \code
  1918. /// void f() { ^{}(); }
  1919. /// \endcode
  1920. extern const internal::VariadicDynCastAllOfMatcher<Stmt, BlockExpr> blockExpr;
  1921. /// Matches if statements.
  1922. ///
  1923. /// Example matches 'if (x) {}'
  1924. /// \code
  1925. /// if (x) {}
  1926. /// \endcode
  1927. extern const internal::VariadicDynCastAllOfMatcher<Stmt, IfStmt> ifStmt;
  1928. /// Matches for statements.
  1929. ///
  1930. /// Example matches 'for (;;) {}'
  1931. /// \code
  1932. /// for (;;) {}
  1933. /// int i[] = {1, 2, 3}; for (auto a : i);
  1934. /// \endcode
  1935. extern const internal::VariadicDynCastAllOfMatcher<Stmt, ForStmt> forStmt;
  1936. /// Matches the increment statement of a for loop.
  1937. ///
  1938. /// Example:
  1939. /// forStmt(hasIncrement(unaryOperator(hasOperatorName("++"))))
  1940. /// matches '++x' in
  1941. /// \code
  1942. /// for (x; x < N; ++x) { }
  1943. /// \endcode
  1944. AST_MATCHER_P(ForStmt, hasIncrement, internal::Matcher<Stmt>,
  1945. InnerMatcher) {
  1946. const Stmt *const Increment = Node.getInc();
  1947. return (Increment != nullptr &&
  1948. InnerMatcher.matches(*Increment, Finder, Builder));
  1949. }
  1950. /// Matches the initialization statement of a for loop.
  1951. ///
  1952. /// Example:
  1953. /// forStmt(hasLoopInit(declStmt()))
  1954. /// matches 'int x = 0' in
  1955. /// \code
  1956. /// for (int x = 0; x < N; ++x) { }
  1957. /// \endcode
  1958. AST_MATCHER_P(ForStmt, hasLoopInit, internal::Matcher<Stmt>,
  1959. InnerMatcher) {
  1960. const Stmt *const Init = Node.getInit();
  1961. return (Init != nullptr && InnerMatcher.matches(*Init, Finder, Builder));
  1962. }
  1963. /// Matches range-based for statements.
  1964. ///
  1965. /// cxxForRangeStmt() matches 'for (auto a : i)'
  1966. /// \code
  1967. /// int i[] = {1, 2, 3}; for (auto a : i);
  1968. /// for(int j = 0; j < 5; ++j);
  1969. /// \endcode
  1970. extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXForRangeStmt>
  1971. cxxForRangeStmt;
  1972. /// Matches the initialization statement of a for loop.
  1973. ///
  1974. /// Example:
  1975. /// forStmt(hasLoopVariable(anything()))
  1976. /// matches 'int x' in
  1977. /// \code
  1978. /// for (int x : a) { }
  1979. /// \endcode
  1980. AST_MATCHER_P(CXXForRangeStmt, hasLoopVariable, internal::Matcher<VarDecl>,
  1981. InnerMatcher) {
  1982. const VarDecl *const Var = Node.getLoopVariable();
  1983. return (Var != nullptr && InnerMatcher.matches(*Var, Finder, Builder));
  1984. }
  1985. /// Matches the range initialization statement of a for loop.
  1986. ///
  1987. /// Example:
  1988. /// forStmt(hasRangeInit(anything()))
  1989. /// matches 'a' in
  1990. /// \code
  1991. /// for (int x : a) { }
  1992. /// \endcode
  1993. AST_MATCHER_P(CXXForRangeStmt, hasRangeInit, internal::Matcher<Expr>,
  1994. InnerMatcher) {
  1995. const Expr *const Init = Node.getRangeInit();
  1996. return (Init != nullptr && InnerMatcher.matches(*Init, Finder, Builder));
  1997. }
  1998. /// Matches while statements.
  1999. ///
  2000. /// Given
  2001. /// \code
  2002. /// while (true) {}
  2003. /// \endcode
  2004. /// whileStmt()
  2005. /// matches 'while (true) {}'.
  2006. extern const internal::VariadicDynCastAllOfMatcher<Stmt, WhileStmt> whileStmt;
  2007. /// Matches do statements.
  2008. ///
  2009. /// Given
  2010. /// \code
  2011. /// do {} while (true);
  2012. /// \endcode
  2013. /// doStmt()
  2014. /// matches 'do {} while(true)'
  2015. extern const internal::VariadicDynCastAllOfMatcher<Stmt, DoStmt> doStmt;
  2016. /// Matches break statements.
  2017. ///
  2018. /// Given
  2019. /// \code
  2020. /// while (true) { break; }
  2021. /// \endcode
  2022. /// breakStmt()
  2023. /// matches 'break'
  2024. extern const internal::VariadicDynCastAllOfMatcher<Stmt, BreakStmt> breakStmt;
  2025. /// Matches continue statements.
  2026. ///
  2027. /// Given
  2028. /// \code
  2029. /// while (true) { continue; }
  2030. /// \endcode
  2031. /// continueStmt()
  2032. /// matches 'continue'
  2033. extern const internal::VariadicDynCastAllOfMatcher<Stmt, ContinueStmt>
  2034. continueStmt;
  2035. /// Matches co_return statements.
  2036. ///
  2037. /// Given
  2038. /// \code
  2039. /// while (true) { co_return; }
  2040. /// \endcode
  2041. /// coreturnStmt()
  2042. /// matches 'co_return'
  2043. extern const internal::VariadicDynCastAllOfMatcher<Stmt, CoreturnStmt>
  2044. coreturnStmt;
  2045. /// Matches return statements.
  2046. ///
  2047. /// Given
  2048. /// \code
  2049. /// return 1;
  2050. /// \endcode
  2051. /// returnStmt()
  2052. /// matches 'return 1'
  2053. extern const internal::VariadicDynCastAllOfMatcher<Stmt, ReturnStmt> returnStmt;
  2054. /// Matches goto statements.
  2055. ///
  2056. /// Given
  2057. /// \code
  2058. /// goto FOO;
  2059. /// FOO: bar();
  2060. /// \endcode
  2061. /// gotoStmt()
  2062. /// matches 'goto FOO'
  2063. extern const internal::VariadicDynCastAllOfMatcher<Stmt, GotoStmt> gotoStmt;
  2064. /// Matches label statements.
  2065. ///
  2066. /// Given
  2067. /// \code
  2068. /// goto FOO;
  2069. /// FOO: bar();
  2070. /// \endcode
  2071. /// labelStmt()
  2072. /// matches 'FOO:'
  2073. extern const internal::VariadicDynCastAllOfMatcher<Stmt, LabelStmt> labelStmt;
  2074. /// Matches address of label statements (GNU extension).
  2075. ///
  2076. /// Given
  2077. /// \code
  2078. /// FOO: bar();
  2079. /// void *ptr = &&FOO;
  2080. /// goto *bar;
  2081. /// \endcode
  2082. /// addrLabelExpr()
  2083. /// matches '&&FOO'
  2084. extern const internal::VariadicDynCastAllOfMatcher<Stmt, AddrLabelExpr>
  2085. addrLabelExpr;
  2086. /// Matches switch statements.
  2087. ///
  2088. /// Given
  2089. /// \code
  2090. /// switch(a) { case 42: break; default: break; }
  2091. /// \endcode
  2092. /// switchStmt()
  2093. /// matches 'switch(a)'.
  2094. extern const internal::VariadicDynCastAllOfMatcher<Stmt, SwitchStmt> switchStmt;
  2095. /// Matches case and default statements inside switch statements.
  2096. ///
  2097. /// Given
  2098. /// \code
  2099. /// switch(a) { case 42: break; default: break; }
  2100. /// \endcode
  2101. /// switchCase()
  2102. /// matches 'case 42:' and 'default:'.
  2103. extern const internal::VariadicDynCastAllOfMatcher<Stmt, SwitchCase> switchCase;
  2104. /// Matches case statements inside switch statements.
  2105. ///
  2106. /// Given
  2107. /// \code
  2108. /// switch(a) { case 42: break; default: break; }
  2109. /// \endcode
  2110. /// caseStmt()
  2111. /// matches 'case 42:'.
  2112. extern const internal::VariadicDynCastAllOfMatcher<Stmt, CaseStmt> caseStmt;
  2113. /// Matches default statements inside switch statements.
  2114. ///
  2115. /// Given
  2116. /// \code
  2117. /// switch(a) { case 42: break; default: break; }
  2118. /// \endcode
  2119. /// defaultStmt()
  2120. /// matches 'default:'.
  2121. extern const internal::VariadicDynCastAllOfMatcher<Stmt, DefaultStmt>
  2122. defaultStmt;
  2123. /// Matches compound statements.
  2124. ///
  2125. /// Example matches '{}' and '{{}}' in 'for (;;) {{}}'
  2126. /// \code
  2127. /// for (;;) {{}}
  2128. /// \endcode
  2129. extern const internal::VariadicDynCastAllOfMatcher<Stmt, CompoundStmt>
  2130. compoundStmt;
  2131. /// Matches catch statements.
  2132. ///
  2133. /// \code
  2134. /// try {} catch(int i) {}
  2135. /// \endcode
  2136. /// cxxCatchStmt()
  2137. /// matches 'catch(int i)'
  2138. extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXCatchStmt>
  2139. cxxCatchStmt;
  2140. /// Matches try statements.
  2141. ///
  2142. /// \code
  2143. /// try {} catch(int i) {}
  2144. /// \endcode
  2145. /// cxxTryStmt()
  2146. /// matches 'try {}'
  2147. extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXTryStmt> cxxTryStmt;
  2148. /// Matches throw expressions.
  2149. ///
  2150. /// \code
  2151. /// try { throw 5; } catch(int i) {}
  2152. /// \endcode
  2153. /// cxxThrowExpr()
  2154. /// matches 'throw 5'
  2155. extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXThrowExpr>
  2156. cxxThrowExpr;
  2157. /// Matches null statements.
  2158. ///
  2159. /// \code
  2160. /// foo();;
  2161. /// \endcode
  2162. /// nullStmt()
  2163. /// matches the second ';'
  2164. extern const internal::VariadicDynCastAllOfMatcher<Stmt, NullStmt> nullStmt;
  2165. /// Matches asm statements.
  2166. ///
  2167. /// \code
  2168. /// int i = 100;
  2169. /// __asm("mov al, 2");
  2170. /// \endcode
  2171. /// asmStmt()
  2172. /// matches '__asm("mov al, 2")'
  2173. extern const internal::VariadicDynCastAllOfMatcher<Stmt, AsmStmt> asmStmt;
  2174. /// Matches bool literals.
  2175. ///
  2176. /// Example matches true
  2177. /// \code
  2178. /// true
  2179. /// \endcode
  2180. extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXBoolLiteralExpr>
  2181. cxxBoolLiteral;
  2182. /// Matches string literals (also matches wide string literals).
  2183. ///
  2184. /// Example matches "abcd", L"abcd"
  2185. /// \code
  2186. /// char *s = "abcd";
  2187. /// wchar_t *ws = L"abcd";
  2188. /// \endcode
  2189. extern const internal::VariadicDynCastAllOfMatcher<Stmt, StringLiteral>
  2190. stringLiteral;
  2191. /// Matches character literals (also matches wchar_t).
  2192. ///
  2193. /// Not matching Hex-encoded chars (e.g. 0x1234, which is a IntegerLiteral),
  2194. /// though.
  2195. ///
  2196. /// Example matches 'a', L'a'
  2197. /// \code
  2198. /// char ch = 'a';
  2199. /// wchar_t chw = L'a';
  2200. /// \endcode
  2201. extern const internal::VariadicDynCastAllOfMatcher<Stmt, CharacterLiteral>
  2202. characterLiteral;
  2203. /// Matches integer literals of all sizes / encodings, e.g.
  2204. /// 1, 1L, 0x1 and 1U.
  2205. ///
  2206. /// Does not match character-encoded integers such as L'a'.
  2207. extern const internal::VariadicDynCastAllOfMatcher<Stmt, IntegerLiteral>
  2208. integerLiteral;
  2209. /// Matches float literals of all sizes / encodings, e.g.
  2210. /// 1.0, 1.0f, 1.0L and 1e10.
  2211. ///
  2212. /// Does not match implicit conversions such as
  2213. /// \code
  2214. /// float a = 10;
  2215. /// \endcode
  2216. extern const internal::VariadicDynCastAllOfMatcher<Stmt, FloatingLiteral>
  2217. floatLiteral;
  2218. /// Matches imaginary literals, which are based on integer and floating
  2219. /// point literals e.g.: 1i, 1.0i
  2220. extern const internal::VariadicDynCastAllOfMatcher<Stmt, ImaginaryLiteral>
  2221. imaginaryLiteral;
  2222. /// Matches fixed point literals
  2223. extern const internal::VariadicDynCastAllOfMatcher<Stmt, FixedPointLiteral>
  2224. fixedPointLiteral;
  2225. /// Matches user defined literal operator call.
  2226. ///
  2227. /// Example match: "foo"_suffix
  2228. extern const internal::VariadicDynCastAllOfMatcher<Stmt, UserDefinedLiteral>
  2229. userDefinedLiteral;
  2230. /// Matches compound (i.e. non-scalar) literals
  2231. ///
  2232. /// Example match: {1}, (1, 2)
  2233. /// \code
  2234. /// int array[4] = {1};
  2235. /// vector int myvec = (vector int)(1, 2);
  2236. /// \endcode
  2237. extern const internal::VariadicDynCastAllOfMatcher<Stmt, CompoundLiteralExpr>
  2238. compoundLiteralExpr;
  2239. /// Matches co_await expressions.
  2240. ///
  2241. /// Given
  2242. /// \code
  2243. /// co_await 1;
  2244. /// \endcode
  2245. /// coawaitExpr()
  2246. /// matches 'co_await 1'
  2247. extern const internal::VariadicDynCastAllOfMatcher<Stmt, CoawaitExpr>
  2248. coawaitExpr;
  2249. /// Matches co_await expressions where the type of the promise is dependent
  2250. extern const internal::VariadicDynCastAllOfMatcher<Stmt, DependentCoawaitExpr>
  2251. dependentCoawaitExpr;
  2252. /// Matches co_yield expressions.
  2253. ///
  2254. /// Given
  2255. /// \code
  2256. /// co_yield 1;
  2257. /// \endcode
  2258. /// coyieldExpr()
  2259. /// matches 'co_yield 1'
  2260. extern const internal::VariadicDynCastAllOfMatcher<Stmt, CoyieldExpr>
  2261. coyieldExpr;
  2262. /// Matches nullptr literal.
  2263. extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXNullPtrLiteralExpr>
  2264. cxxNullPtrLiteralExpr;
  2265. /// Matches GNU __builtin_choose_expr.
  2266. extern const internal::VariadicDynCastAllOfMatcher<Stmt, ChooseExpr>
  2267. chooseExpr;
  2268. /// Matches GNU __null expression.
  2269. extern const internal::VariadicDynCastAllOfMatcher<Stmt, GNUNullExpr>
  2270. gnuNullExpr;
  2271. /// Matches C11 _Generic expression.
  2272. extern const internal::VariadicDynCastAllOfMatcher<Stmt, GenericSelectionExpr>
  2273. genericSelectionExpr;
  2274. /// Matches atomic builtins.
  2275. /// Example matches __atomic_load_n(ptr, 1)
  2276. /// \code
  2277. /// void foo() { int *ptr; __atomic_load_n(ptr, 1); }
  2278. /// \endcode
  2279. extern const internal::VariadicDynCastAllOfMatcher<Stmt, AtomicExpr> atomicExpr;
  2280. /// Matches statement expression (GNU extension).
  2281. ///
  2282. /// Example match: ({ int X = 4; X; })
  2283. /// \code
  2284. /// int C = ({ int X = 4; X; });
  2285. /// \endcode
  2286. extern const internal::VariadicDynCastAllOfMatcher<Stmt, StmtExpr> stmtExpr;
  2287. /// Matches binary operator expressions.
  2288. ///
  2289. /// Example matches a || b
  2290. /// \code
  2291. /// !(a || b)
  2292. /// \endcode
  2293. /// See also the binaryOperation() matcher for more-general matching.
  2294. extern const internal::VariadicDynCastAllOfMatcher<Stmt, BinaryOperator>
  2295. binaryOperator;
  2296. /// Matches unary operator expressions.
  2297. ///
  2298. /// Example matches !a
  2299. /// \code
  2300. /// !a || b
  2301. /// \endcode
  2302. extern const internal::VariadicDynCastAllOfMatcher<Stmt, UnaryOperator>
  2303. unaryOperator;
  2304. /// Matches conditional operator expressions.
  2305. ///
  2306. /// Example matches a ? b : c
  2307. /// \code
  2308. /// (a ? b : c) + 42
  2309. /// \endcode
  2310. extern const internal::VariadicDynCastAllOfMatcher<Stmt, ConditionalOperator>
  2311. conditionalOperator;
  2312. /// Matches binary conditional operator expressions (GNU extension).
  2313. ///
  2314. /// Example matches a ?: b
  2315. /// \code
  2316. /// (a ?: b) + 42;
  2317. /// \endcode
  2318. extern const internal::VariadicDynCastAllOfMatcher<Stmt,
  2319. BinaryConditionalOperator>
  2320. binaryConditionalOperator;
  2321. /// Matches opaque value expressions. They are used as helpers
  2322. /// to reference another expressions and can be met
  2323. /// in BinaryConditionalOperators, for example.
  2324. ///
  2325. /// Example matches 'a'
  2326. /// \code
  2327. /// (a ?: c) + 42;
  2328. /// \endcode
  2329. extern const internal::VariadicDynCastAllOfMatcher<Stmt, OpaqueValueExpr>
  2330. opaqueValueExpr;
  2331. /// Matches a C++ static_assert declaration.
  2332. ///
  2333. /// Example:
  2334. /// staticAssertExpr()
  2335. /// matches
  2336. /// static_assert(sizeof(S) == sizeof(int))
  2337. /// in
  2338. /// \code
  2339. /// struct S {
  2340. /// int x;
  2341. /// };
  2342. /// static_assert(sizeof(S) == sizeof(int));
  2343. /// \endcode
  2344. extern const internal::VariadicDynCastAllOfMatcher<Decl, StaticAssertDecl>
  2345. staticAssertDecl;
  2346. /// Matches a reinterpret_cast expression.
  2347. ///
  2348. /// Either the source expression or the destination type can be matched
  2349. /// using has(), but hasDestinationType() is more specific and can be
  2350. /// more readable.
  2351. ///
  2352. /// Example matches reinterpret_cast<char*>(&p) in
  2353. /// \code
  2354. /// void* p = reinterpret_cast<char*>(&p);
  2355. /// \endcode
  2356. extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXReinterpretCastExpr>
  2357. cxxReinterpretCastExpr;
  2358. /// Matches a C++ static_cast expression.
  2359. ///
  2360. /// \see hasDestinationType
  2361. /// \see reinterpretCast
  2362. ///
  2363. /// Example:
  2364. /// cxxStaticCastExpr()
  2365. /// matches
  2366. /// static_cast<long>(8)
  2367. /// in
  2368. /// \code
  2369. /// long eight(static_cast<long>(8));
  2370. /// \endcode
  2371. extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXStaticCastExpr>
  2372. cxxStaticCastExpr;
  2373. /// Matches a dynamic_cast expression.
  2374. ///
  2375. /// Example:
  2376. /// cxxDynamicCastExpr()
  2377. /// matches
  2378. /// dynamic_cast<D*>(&b);
  2379. /// in
  2380. /// \code
  2381. /// struct B { virtual ~B() {} }; struct D : B {};
  2382. /// B b;
  2383. /// D* p = dynamic_cast<D*>(&b);
  2384. /// \endcode
  2385. extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXDynamicCastExpr>
  2386. cxxDynamicCastExpr;
  2387. /// Matches a const_cast expression.
  2388. ///
  2389. /// Example: Matches const_cast<int*>(&r) in
  2390. /// \code
  2391. /// int n = 42;
  2392. /// const int &r(n);
  2393. /// int* p = const_cast<int*>(&r);
  2394. /// \endcode
  2395. extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXConstCastExpr>
  2396. cxxConstCastExpr;
  2397. /// Matches a C-style cast expression.
  2398. ///
  2399. /// Example: Matches (int) 2.2f in
  2400. /// \code
  2401. /// int i = (int) 2.2f;
  2402. /// \endcode
  2403. extern const internal::VariadicDynCastAllOfMatcher<Stmt, CStyleCastExpr>
  2404. cStyleCastExpr;
  2405. /// Matches explicit cast expressions.
  2406. ///
  2407. /// Matches any cast expression written in user code, whether it be a
  2408. /// C-style cast, a functional-style cast, or a keyword cast.
  2409. ///
  2410. /// Does not match implicit conversions.
  2411. ///
  2412. /// Note: the name "explicitCast" is chosen to match Clang's terminology, as
  2413. /// Clang uses the term "cast" to apply to implicit conversions as well as to
  2414. /// actual cast expressions.
  2415. ///
  2416. /// \see hasDestinationType.
  2417. ///
  2418. /// Example: matches all five of the casts in
  2419. /// \code
  2420. /// int((int)(reinterpret_cast<int>(static_cast<int>(const_cast<int>(42)))))
  2421. /// \endcode
  2422. /// but does not match the implicit conversion in
  2423. /// \code
  2424. /// long ell = 42;
  2425. /// \endcode
  2426. extern const internal::VariadicDynCastAllOfMatcher<Stmt, ExplicitCastExpr>
  2427. explicitCastExpr;
  2428. /// Matches the implicit cast nodes of Clang's AST.
  2429. ///
  2430. /// This matches many different places, including function call return value
  2431. /// eliding, as well as any type conversions.
  2432. extern const internal::VariadicDynCastAllOfMatcher<Stmt, ImplicitCastExpr>
  2433. implicitCastExpr;
  2434. /// Matches any cast nodes of Clang's AST.
  2435. ///
  2436. /// Example: castExpr() matches each of the following:
  2437. /// \code
  2438. /// (int) 3;
  2439. /// const_cast<Expr *>(SubExpr);
  2440. /// char c = 0;
  2441. /// \endcode
  2442. /// but does not match
  2443. /// \code
  2444. /// int i = (0);
  2445. /// int k = 0;
  2446. /// \endcode
  2447. extern const internal::VariadicDynCastAllOfMatcher<Stmt, CastExpr> castExpr;
  2448. /// Matches functional cast expressions
  2449. ///
  2450. /// Example: Matches Foo(bar);
  2451. /// \code
  2452. /// Foo f = bar;
  2453. /// Foo g = (Foo) bar;
  2454. /// Foo h = Foo(bar);
  2455. /// \endcode
  2456. extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXFunctionalCastExpr>
  2457. cxxFunctionalCastExpr;
  2458. /// Matches functional cast expressions having N != 1 arguments
  2459. ///
  2460. /// Example: Matches Foo(bar, bar)
  2461. /// \code
  2462. /// Foo h = Foo(bar, bar);
  2463. /// \endcode
  2464. extern const internal::VariadicDynCastAllOfMatcher<Stmt, CXXTemporaryObjectExpr>
  2465. cxxTemporaryObjectExpr;
  2466. /// Matches predefined identifier expressions [C99 6.4.2.2].
  2467. ///
  2468. /// Example: Matches __func__
  2469. /// \code
  2470. /// printf("%s", __func__);
  2471. /// \endcode
  2472. extern const internal::VariadicDynCastAllOfMatcher<Stmt, PredefinedExpr>
  2473. predefinedExpr;
  2474. /// Matches C99 designated initializer expressions [C99 6.7.8].
  2475. ///
  2476. /// Example: Matches { [2].y = 1.0, [0].x = 1.0 }
  2477. /// \code
  2478. /// point ptarray[10] = { [2].y = 1.0, [0].x = 1.0 };
  2479. /// \endcode
  2480. extern const internal::VariadicDynCastAllOfMatcher<Stmt, DesignatedInitExpr>
  2481. designatedInitExpr;
  2482. /// Matches designated initializer expressions that contain
  2483. /// a specific number of designators.
  2484. ///
  2485. /// Example: Given
  2486. /// \code
  2487. /// point ptarray[10] = { [2].y = 1.0, [0].x = 1.0 };
  2488. /// point ptarray2[10] = { [2].y = 1.0, [2].x = 0.0, [0].x = 1.0 };
  2489. /// \endcode
  2490. /// designatorCountIs(2)
  2491. /// matches '{ [2].y = 1.0, [0].x = 1.0 }',
  2492. /// but not '{ [2].y = 1.0, [2].x = 0.0, [0].x = 1.0 }'.
  2493. AST_MATCHER_P(DesignatedInitExpr, designatorCountIs, unsigned, N) {
  2494. return Node.size() == N;
  2495. }
  2496. /// Matches \c QualTypes in the clang AST.
  2497. extern const internal::VariadicAllOfMatcher<QualType> qualType;
  2498. /// Matches \c Types in the clang AST.
  2499. extern const internal::VariadicAllOfMatcher<Type> type;
  2500. /// Matches \c TypeLocs in the clang AST.
  2501. extern const internal::VariadicAllOfMatcher<TypeLoc> typeLoc;
  2502. /// Matches if any of the given matchers matches.
  2503. ///
  2504. /// Unlike \c anyOf, \c eachOf will generate a match result for each
  2505. /// matching submatcher.
  2506. ///
  2507. /// For example, in:
  2508. /// \code
  2509. /// class A { int a; int b; };
  2510. /// \endcode
  2511. /// The matcher:
  2512. /// \code
  2513. /// cxxRecordDecl(eachOf(has(fieldDecl(hasName("a")).bind("v")),
  2514. /// has(fieldDecl(hasName("b")).bind("v"))))
  2515. /// \endcode
  2516. /// will generate two results binding "v", the first of which binds
  2517. /// the field declaration of \c a, the second the field declaration of
  2518. /// \c b.
  2519. ///
  2520. /// Usable as: Any Matcher
  2521. extern const internal::VariadicOperatorMatcherFunc<
  2522. 2, std::numeric_limits<unsigned>::max()>
  2523. eachOf;
  2524. /// Matches if any of the given matchers matches.
  2525. ///
  2526. /// Usable as: Any Matcher
  2527. extern const internal::VariadicOperatorMatcherFunc<
  2528. 2, std::numeric_limits<unsigned>::max()>
  2529. anyOf;
  2530. /// Matches if all given matchers match.
  2531. ///
  2532. /// Usable as: Any Matcher
  2533. extern const internal::VariadicOperatorMatcherFunc<
  2534. 2, std::numeric_limits<unsigned>::max()>
  2535. allOf;
  2536. /// Matches any node regardless of the submatcher.
  2537. ///
  2538. /// However, \c optionally will retain any bindings generated by the submatcher.
  2539. /// Useful when additional information which may or may not present about a main
  2540. /// matching node is desired.
  2541. ///
  2542. /// For example, in:
  2543. /// \code
  2544. /// class Foo {
  2545. /// int bar;
  2546. /// }
  2547. /// \endcode
  2548. /// The matcher:
  2549. /// \code
  2550. /// cxxRecordDecl(
  2551. /// optionally(has(
  2552. /// fieldDecl(hasName("bar")).bind("var")
  2553. /// ))).bind("record")
  2554. /// \endcode
  2555. /// will produce a result binding for both "record" and "var".
  2556. /// The matcher will produce a "record" binding for even if there is no data
  2557. /// member named "bar" in that class.
  2558. ///
  2559. /// Usable as: Any Matcher
  2560. extern const internal::VariadicOperatorMatcherFunc<1, 1> optionally;
  2561. /// Matches sizeof (C99), alignof (C++11) and vec_step (OpenCL)
  2562. ///
  2563. /// Given
  2564. /// \code
  2565. /// Foo x = bar;
  2566. /// int y = sizeof(x) + alignof(x);
  2567. /// \endcode
  2568. /// unaryExprOrTypeTraitExpr()
  2569. /// matches \c sizeof(x) and \c alignof(x)
  2570. extern const internal::VariadicDynCastAllOfMatcher<Stmt,
  2571. UnaryExprOrTypeTraitExpr>
  2572. unaryExprOrTypeTraitExpr;
  2573. /// Matches any of the \p NodeMatchers with InnerMatchers nested within
  2574. ///
  2575. /// Given
  2576. /// \code
  2577. /// if (true);
  2578. /// for (; true; );
  2579. /// \endcode
  2580. /// with the matcher
  2581. /// \code
  2582. /// mapAnyOf(ifStmt, forStmt).with(
  2583. /// hasCondition(cxxBoolLiteralExpr(equals(true)))
  2584. /// ).bind("trueCond")
  2585. /// \endcode
  2586. /// matches the \c if and the \c for. It is equivalent to:
  2587. /// \code
  2588. /// auto trueCond = hasCondition(cxxBoolLiteralExpr(equals(true)));
  2589. /// anyOf(
  2590. /// ifStmt(trueCond).bind("trueCond"),
  2591. /// forStmt(trueCond).bind("trueCond")
  2592. /// );
  2593. /// \endcode
  2594. ///
  2595. /// The with() chain-call accepts zero or more matchers which are combined
  2596. /// as-if with allOf() in each of the node matchers.
  2597. /// Usable as: Any Matcher
  2598. template <typename T, typename... U>
  2599. auto mapAnyOf(internal::VariadicDynCastAllOfMatcher<T, U> const &...) {
  2600. return internal::MapAnyOfHelper<U...>();
  2601. }
  2602. /// Matches nodes which can be used with binary operators.
  2603. ///
  2604. /// The code
  2605. /// \code
  2606. /// var1 != var2;
  2607. /// \endcode
  2608. /// might be represented in the clang AST as a binaryOperator, a
  2609. /// cxxOperatorCallExpr or a cxxRewrittenBinaryOperator, depending on
  2610. ///
  2611. /// * whether the types of var1 and var2 are fundamental (binaryOperator) or at
  2612. /// least one is a class type (cxxOperatorCallExpr)
  2613. /// * whether the code appears in a template declaration, if at least one of the
  2614. /// vars is a dependent-type (binaryOperator)
  2615. /// * whether the code relies on a rewritten binary operator, such as a
  2616. /// spaceship operator or an inverted equality operator
  2617. /// (cxxRewrittenBinaryOperator)
  2618. ///
  2619. /// This matcher elides details in places where the matchers for the nodes are
  2620. /// compatible.
  2621. ///
  2622. /// Given
  2623. /// \code
  2624. /// binaryOperation(
  2625. /// hasOperatorName("!="),
  2626. /// hasLHS(expr().bind("lhs")),
  2627. /// hasRHS(expr().bind("rhs"))
  2628. /// )
  2629. /// \endcode
  2630. /// matches each use of "!=" in:
  2631. /// \code
  2632. /// struct S{
  2633. /// bool operator!=(const S&) const;
  2634. /// };
  2635. ///
  2636. /// void foo()
  2637. /// {
  2638. /// 1 != 2;
  2639. /// S() != S();
  2640. /// }
  2641. ///
  2642. /// template<typename T>
  2643. /// void templ()
  2644. /// {
  2645. /// 1 != 2;
  2646. /// T() != S();
  2647. /// }
  2648. /// struct HasOpEq
  2649. /// {
  2650. /// bool operator==(const HasOpEq &) const;
  2651. /// };
  2652. ///
  2653. /// void inverse()
  2654. /// {
  2655. /// HasOpEq s1;
  2656. /// HasOpEq s2;
  2657. /// if (s1 != s2)
  2658. /// return;
  2659. /// }
  2660. ///
  2661. /// struct HasSpaceship
  2662. /// {
  2663. /// bool operator<=>(const HasOpEq &) const;
  2664. /// };
  2665. ///
  2666. /// void use_spaceship()
  2667. /// {
  2668. /// HasSpaceship s1;
  2669. /// HasSpaceship s2;
  2670. /// if (s1 != s2)
  2671. /// return;
  2672. /// }
  2673. /// \endcode
  2674. extern const internal::MapAnyOfMatcher<BinaryOperator, CXXOperatorCallExpr,
  2675. CXXRewrittenBinaryOperator>
  2676. binaryOperation;
  2677. /// Matches function calls and constructor calls
  2678. ///
  2679. /// Because CallExpr and CXXConstructExpr do not share a common
  2680. /// base class with API accessing arguments etc, AST Matchers for code
  2681. /// which should match both are typically duplicated. This matcher
  2682. /// removes the need for duplication.
  2683. ///
  2684. /// Given code
  2685. /// \code
  2686. /// struct ConstructorTakesInt
  2687. /// {
  2688. /// ConstructorTakesInt(int i) {}
  2689. /// };
  2690. ///
  2691. /// void callTakesInt(int i)
  2692. /// {
  2693. /// }
  2694. ///
  2695. /// void doCall()
  2696. /// {
  2697. /// callTakesInt(42);
  2698. /// }
  2699. ///
  2700. /// void doConstruct()
  2701. /// {
  2702. /// ConstructorTakesInt cti(42);
  2703. /// }
  2704. /// \endcode
  2705. ///
  2706. /// The matcher
  2707. /// \code
  2708. /// invocation(hasArgument(0, integerLiteral(equals(42))))
  2709. /// \endcode
  2710. /// matches the expression in both doCall and doConstruct
  2711. extern const internal::MapAnyOfMatcher<CallExpr, CXXConstructExpr> invocation;
  2712. /// Matches unary expressions that have a specific type of argument.
  2713. ///
  2714. /// Given
  2715. /// \code
  2716. /// int a, c; float b; int s = sizeof(a) + sizeof(b) + alignof(c);
  2717. /// \endcode
  2718. /// unaryExprOrTypeTraitExpr(hasArgumentOfType(asString("int"))
  2719. /// matches \c sizeof(a) and \c alignof(c)
  2720. AST_MATCHER_P(UnaryExprOrTypeTraitExpr, hasArgumentOfType,
  2721. internal::Matcher<QualType>, InnerMatcher) {
  2722. const QualType ArgumentType = Node.getTypeOfArgument();
  2723. return InnerMatcher.matches(ArgumentType, Finder, Builder);
  2724. }
  2725. /// Matches unary expressions of a certain kind.
  2726. ///
  2727. /// Given
  2728. /// \code
  2729. /// int x;
  2730. /// int s = sizeof(x) + alignof(x)
  2731. /// \endcode
  2732. /// unaryExprOrTypeTraitExpr(ofKind(UETT_SizeOf))
  2733. /// matches \c sizeof(x)
  2734. ///
  2735. /// If the matcher is use from clang-query, UnaryExprOrTypeTrait parameter
  2736. /// should be passed as a quoted string. e.g., ofKind("UETT_SizeOf").
  2737. AST_MATCHER_P(UnaryExprOrTypeTraitExpr, ofKind, UnaryExprOrTypeTrait, Kind) {
  2738. return Node.getKind() == Kind;
  2739. }
  2740. /// Same as unaryExprOrTypeTraitExpr, but only matching
  2741. /// alignof.
  2742. inline internal::BindableMatcher<Stmt> alignOfExpr(
  2743. const internal::Matcher<UnaryExprOrTypeTraitExpr> &InnerMatcher) {
  2744. return stmt(unaryExprOrTypeTraitExpr(
  2745. allOf(anyOf(ofKind(UETT_AlignOf), ofKind(UETT_PreferredAlignOf)),
  2746. InnerMatcher)));
  2747. }
  2748. /// Same as unaryExprOrTypeTraitExpr, but only matching
  2749. /// sizeof.
  2750. inline internal::BindableMatcher<Stmt> sizeOfExpr(
  2751. const internal::Matcher<UnaryExprOrTypeTraitExpr> &InnerMatcher) {
  2752. return stmt(unaryExprOrTypeTraitExpr(
  2753. allOf(ofKind(UETT_SizeOf), InnerMatcher)));
  2754. }
  2755. /// Matches NamedDecl nodes that have the specified name.
  2756. ///
  2757. /// Supports specifying enclosing namespaces or classes by prefixing the name
  2758. /// with '<enclosing>::'.
  2759. /// Does not match typedefs of an underlying type with the given name.
  2760. ///
  2761. /// Example matches X (Name == "X")
  2762. /// \code
  2763. /// class X;
  2764. /// \endcode
  2765. ///
  2766. /// Example matches X (Name is one of "::a::b::X", "a::b::X", "b::X", "X")
  2767. /// \code
  2768. /// namespace a { namespace b { class X; } }
  2769. /// \endcode
  2770. inline internal::Matcher<NamedDecl> hasName(StringRef Name) {
  2771. return internal::Matcher<NamedDecl>(
  2772. new internal::HasNameMatcher({std::string(Name)}));
  2773. }
  2774. /// Matches NamedDecl nodes that have any of the specified names.
  2775. ///
  2776. /// This matcher is only provided as a performance optimization of hasName.
  2777. /// \code
  2778. /// hasAnyName(a, b, c)
  2779. /// \endcode
  2780. /// is equivalent to, but faster than
  2781. /// \code
  2782. /// anyOf(hasName(a), hasName(b), hasName(c))
  2783. /// \endcode
  2784. extern const internal::VariadicFunction<internal::Matcher<NamedDecl>, StringRef,
  2785. internal::hasAnyNameFunc>
  2786. hasAnyName;
  2787. /// Matches NamedDecl nodes whose fully qualified names contain
  2788. /// a substring matched by the given RegExp.
  2789. ///
  2790. /// Supports specifying enclosing namespaces or classes by
  2791. /// prefixing the name with '<enclosing>::'. Does not match typedefs
  2792. /// of an underlying type with the given name.
  2793. ///
  2794. /// Example matches X (regexp == "::X")
  2795. /// \code
  2796. /// class X;
  2797. /// \endcode
  2798. ///
  2799. /// Example matches X (regexp is one of "::X", "^foo::.*X", among others)
  2800. /// \code
  2801. /// namespace foo { namespace bar { class X; } }
  2802. /// \endcode
  2803. AST_MATCHER_REGEX(NamedDecl, matchesName, RegExp) {
  2804. std::string FullNameString = "::" + Node.getQualifiedNameAsString();
  2805. return RegExp->match(FullNameString);
  2806. }
  2807. /// Matches overloaded operator names.
  2808. ///
  2809. /// Matches overloaded operator names specified in strings without the
  2810. /// "operator" prefix: e.g. "<<".
  2811. ///
  2812. /// Given:
  2813. /// \code
  2814. /// class A { int operator*(); };
  2815. /// const A &operator<<(const A &a, const A &b);
  2816. /// A a;
  2817. /// a << a; // <-- This matches
  2818. /// \endcode
  2819. ///
  2820. /// \c cxxOperatorCallExpr(hasOverloadedOperatorName("<<"))) matches the
  2821. /// specified line and
  2822. /// \c cxxRecordDecl(hasMethod(hasOverloadedOperatorName("*")))
  2823. /// matches the declaration of \c A.
  2824. ///
  2825. /// Usable as: Matcher<CXXOperatorCallExpr>, Matcher<FunctionDecl>
  2826. inline internal::PolymorphicMatcher<
  2827. internal::HasOverloadedOperatorNameMatcher,
  2828. AST_POLYMORPHIC_SUPPORTED_TYPES(CXXOperatorCallExpr, FunctionDecl),
  2829. std::vector<std::string>>
  2830. hasOverloadedOperatorName(StringRef Name) {
  2831. return internal::PolymorphicMatcher<
  2832. internal::HasOverloadedOperatorNameMatcher,
  2833. AST_POLYMORPHIC_SUPPORTED_TYPES(CXXOperatorCallExpr, FunctionDecl),
  2834. std::vector<std::string>>({std::string(Name)});
  2835. }
  2836. /// Matches overloaded operator names.
  2837. ///
  2838. /// Matches overloaded operator names specified in strings without the
  2839. /// "operator" prefix: e.g. "<<".
  2840. ///
  2841. /// hasAnyOverloadedOperatorName("+", "-")
  2842. /// Is equivalent to
  2843. /// anyOf(hasOverloadedOperatorName("+"), hasOverloadedOperatorName("-"))
  2844. extern const internal::VariadicFunction<
  2845. internal::PolymorphicMatcher<internal::HasOverloadedOperatorNameMatcher,
  2846. AST_POLYMORPHIC_SUPPORTED_TYPES(
  2847. CXXOperatorCallExpr, FunctionDecl),
  2848. std::vector<std::string>>,
  2849. StringRef, internal::hasAnyOverloadedOperatorNameFunc>
  2850. hasAnyOverloadedOperatorName;
  2851. /// Matches template-dependent, but known, member names.
  2852. ///
  2853. /// In template declarations, dependent members are not resolved and so can
  2854. /// not be matched to particular named declarations.
  2855. ///
  2856. /// This matcher allows to match on the known name of members.
  2857. ///
  2858. /// Given
  2859. /// \code
  2860. /// template <typename T>
  2861. /// struct S {
  2862. /// void mem();
  2863. /// };
  2864. /// template <typename T>
  2865. /// void x() {
  2866. /// S<T> s;
  2867. /// s.mem();
  2868. /// }
  2869. /// \endcode
  2870. /// \c cxxDependentScopeMemberExpr(hasMemberName("mem")) matches `s.mem()`
  2871. AST_MATCHER_P(CXXDependentScopeMemberExpr, hasMemberName, std::string, N) {
  2872. return Node.getMember().getAsString() == N;
  2873. }
  2874. /// Matches template-dependent, but known, member names against an already-bound
  2875. /// node
  2876. ///
  2877. /// In template declarations, dependent members are not resolved and so can
  2878. /// not be matched to particular named declarations.
  2879. ///
  2880. /// This matcher allows to match on the name of already-bound VarDecl, FieldDecl
  2881. /// and CXXMethodDecl nodes.
  2882. ///
  2883. /// Given
  2884. /// \code
  2885. /// template <typename T>
  2886. /// struct S {
  2887. /// void mem();
  2888. /// };
  2889. /// template <typename T>
  2890. /// void x() {
  2891. /// S<T> s;
  2892. /// s.mem();
  2893. /// }
  2894. /// \endcode
  2895. /// The matcher
  2896. /// @code
  2897. /// \c cxxDependentScopeMemberExpr(
  2898. /// hasObjectExpression(declRefExpr(hasType(templateSpecializationType(
  2899. /// hasDeclaration(classTemplateDecl(has(cxxRecordDecl(has(
  2900. /// cxxMethodDecl(hasName("mem")).bind("templMem")
  2901. /// )))))
  2902. /// )))),
  2903. /// memberHasSameNameAsBoundNode("templMem")
  2904. /// )
  2905. /// @endcode
  2906. /// first matches and binds the @c mem member of the @c S template, then
  2907. /// compares its name to the usage in @c s.mem() in the @c x function template
  2908. AST_MATCHER_P(CXXDependentScopeMemberExpr, memberHasSameNameAsBoundNode,
  2909. std::string, BindingID) {
  2910. auto MemberName = Node.getMember().getAsString();
  2911. return Builder->removeBindings(
  2912. [this, MemberName](const BoundNodesMap &Nodes) {
  2913. const auto &BN = Nodes.getNode(this->BindingID);
  2914. if (const auto *ND = BN.get<NamedDecl>()) {
  2915. if (!isa<FieldDecl, CXXMethodDecl, VarDecl>(ND))
  2916. return true;
  2917. return ND->getName() != MemberName;
  2918. }
  2919. return true;
  2920. });
  2921. }
  2922. /// Matches C++ classes that are directly or indirectly derived from a class
  2923. /// matching \c Base, or Objective-C classes that directly or indirectly
  2924. /// subclass a class matching \c Base.
  2925. ///
  2926. /// Note that a class is not considered to be derived from itself.
  2927. ///
  2928. /// Example matches Y, Z, C (Base == hasName("X"))
  2929. /// \code
  2930. /// class X;
  2931. /// class Y : public X {}; // directly derived
  2932. /// class Z : public Y {}; // indirectly derived
  2933. /// typedef X A;
  2934. /// typedef A B;
  2935. /// class C : public B {}; // derived from a typedef of X
  2936. /// \endcode
  2937. ///
  2938. /// In the following example, Bar matches isDerivedFrom(hasName("X")):
  2939. /// \code
  2940. /// class Foo;
  2941. /// typedef Foo X;
  2942. /// class Bar : public Foo {}; // derived from a type that X is a typedef of
  2943. /// \endcode
  2944. ///
  2945. /// In the following example, Bar matches isDerivedFrom(hasName("NSObject"))
  2946. /// \code
  2947. /// @interface NSObject @end
  2948. /// @interface Bar : NSObject @end
  2949. /// \endcode
  2950. ///
  2951. /// Usable as: Matcher<CXXRecordDecl>, Matcher<ObjCInterfaceDecl>
  2952. AST_POLYMORPHIC_MATCHER_P(
  2953. isDerivedFrom,
  2954. AST_POLYMORPHIC_SUPPORTED_TYPES(CXXRecordDecl, ObjCInterfaceDecl),
  2955. internal::Matcher<NamedDecl>, Base) {
  2956. // Check if the node is a C++ struct/union/class.
  2957. if (const auto *RD = dyn_cast<CXXRecordDecl>(&Node))
  2958. return Finder->classIsDerivedFrom(RD, Base, Builder, /*Directly=*/false);
  2959. // The node must be an Objective-C class.
  2960. const auto *InterfaceDecl = cast<ObjCInterfaceDecl>(&Node);
  2961. return Finder->objcClassIsDerivedFrom(InterfaceDecl, Base, Builder,
  2962. /*Directly=*/false);
  2963. }
  2964. /// Overloaded method as shortcut for \c isDerivedFrom(hasName(...)).
  2965. AST_POLYMORPHIC_MATCHER_P_OVERLOAD(
  2966. isDerivedFrom,
  2967. AST_POLYMORPHIC_SUPPORTED_TYPES(CXXRecordDecl, ObjCInterfaceDecl),
  2968. std::string, BaseName, 1) {
  2969. if (BaseName.empty())
  2970. return false;
  2971. const auto M = isDerivedFrom(hasName(BaseName));
  2972. if (const auto *RD = dyn_cast<CXXRecordDecl>(&Node))
  2973. return Matcher<CXXRecordDecl>(M).matches(*RD, Finder, Builder);
  2974. const auto *InterfaceDecl = cast<ObjCInterfaceDecl>(&Node);
  2975. return Matcher<ObjCInterfaceDecl>(M).matches(*InterfaceDecl, Finder, Builder);
  2976. }
  2977. /// Matches C++ classes that have a direct or indirect base matching \p
  2978. /// BaseSpecMatcher.
  2979. ///
  2980. /// Example:
  2981. /// matcher hasAnyBase(hasType(cxxRecordDecl(hasName("SpecialBase"))))
  2982. /// \code
  2983. /// class Foo;
  2984. /// class Bar : Foo {};
  2985. /// class Baz : Bar {};
  2986. /// class SpecialBase;
  2987. /// class Proxy : SpecialBase {}; // matches Proxy
  2988. /// class IndirectlyDerived : Proxy {}; //matches IndirectlyDerived
  2989. /// \endcode
  2990. ///
  2991. // FIXME: Refactor this and isDerivedFrom to reuse implementation.
  2992. AST_MATCHER_P(CXXRecordDecl, hasAnyBase, internal::Matcher<CXXBaseSpecifier>,
  2993. BaseSpecMatcher) {
  2994. return internal::matchesAnyBase(Node, BaseSpecMatcher, Finder, Builder);
  2995. }
  2996. /// Matches C++ classes that have a direct base matching \p BaseSpecMatcher.
  2997. ///
  2998. /// Example:
  2999. /// matcher hasDirectBase(hasType(cxxRecordDecl(hasName("SpecialBase"))))
  3000. /// \code
  3001. /// class Foo;
  3002. /// class Bar : Foo {};
  3003. /// class Baz : Bar {};
  3004. /// class SpecialBase;
  3005. /// class Proxy : SpecialBase {}; // matches Proxy
  3006. /// class IndirectlyDerived : Proxy {}; // doesn't match
  3007. /// \endcode
  3008. AST_MATCHER_P(CXXRecordDecl, hasDirectBase, internal::Matcher<CXXBaseSpecifier>,
  3009. BaseSpecMatcher) {
  3010. return Node.hasDefinition() &&
  3011. llvm::any_of(Node.bases(), [&](const CXXBaseSpecifier &Base) {
  3012. return BaseSpecMatcher.matches(Base, Finder, Builder);
  3013. });
  3014. }
  3015. /// Similar to \c isDerivedFrom(), but also matches classes that directly
  3016. /// match \c Base.
  3017. AST_POLYMORPHIC_MATCHER_P_OVERLOAD(
  3018. isSameOrDerivedFrom,
  3019. AST_POLYMORPHIC_SUPPORTED_TYPES(CXXRecordDecl, ObjCInterfaceDecl),
  3020. internal::Matcher<NamedDecl>, Base, 0) {
  3021. const auto M = anyOf(Base, isDerivedFrom(Base));
  3022. if (const auto *RD = dyn_cast<CXXRecordDecl>(&Node))
  3023. return Matcher<CXXRecordDecl>(M).matches(*RD, Finder, Builder);
  3024. const auto *InterfaceDecl = cast<ObjCInterfaceDecl>(&Node);
  3025. return Matcher<ObjCInterfaceDecl>(M).matches(*InterfaceDecl, Finder, Builder);
  3026. }
  3027. /// Overloaded method as shortcut for
  3028. /// \c isSameOrDerivedFrom(hasName(...)).
  3029. AST_POLYMORPHIC_MATCHER_P_OVERLOAD(
  3030. isSameOrDerivedFrom,
  3031. AST_POLYMORPHIC_SUPPORTED_TYPES(CXXRecordDecl, ObjCInterfaceDecl),
  3032. std::string, BaseName, 1) {
  3033. if (BaseName.empty())
  3034. return false;
  3035. const auto M = isSameOrDerivedFrom(hasName(BaseName));
  3036. if (const auto *RD = dyn_cast<CXXRecordDecl>(&Node))
  3037. return Matcher<CXXRecordDecl>(M).matches(*RD, Finder, Builder);
  3038. const auto *InterfaceDecl = cast<ObjCInterfaceDecl>(&Node);
  3039. return Matcher<ObjCInterfaceDecl>(M).matches(*InterfaceDecl, Finder, Builder);
  3040. }
  3041. /// Matches C++ or Objective-C classes that are directly derived from a class
  3042. /// matching \c Base.
  3043. ///
  3044. /// Note that a class is not considered to be derived from itself.
  3045. ///
  3046. /// Example matches Y, C (Base == hasName("X"))
  3047. /// \code
  3048. /// class X;
  3049. /// class Y : public X {}; // directly derived
  3050. /// class Z : public Y {}; // indirectly derived
  3051. /// typedef X A;
  3052. /// typedef A B;
  3053. /// class C : public B {}; // derived from a typedef of X
  3054. /// \endcode
  3055. ///
  3056. /// In the following example, Bar matches isDerivedFrom(hasName("X")):
  3057. /// \code
  3058. /// class Foo;
  3059. /// typedef Foo X;
  3060. /// class Bar : public Foo {}; // derived from a type that X is a typedef of
  3061. /// \endcode
  3062. AST_POLYMORPHIC_MATCHER_P_OVERLOAD(
  3063. isDirectlyDerivedFrom,
  3064. AST_POLYMORPHIC_SUPPORTED_TYPES(CXXRecordDecl, ObjCInterfaceDecl),
  3065. internal::Matcher<NamedDecl>, Base, 0) {
  3066. // Check if the node is a C++ struct/union/class.
  3067. if (const auto *RD = dyn_cast<CXXRecordDecl>(&Node))
  3068. return Finder->classIsDerivedFrom(RD, Base, Builder, /*Directly=*/true);
  3069. // The node must be an Objective-C class.
  3070. const auto *InterfaceDecl = cast<ObjCInterfaceDecl>(&Node);
  3071. return Finder->objcClassIsDerivedFrom(InterfaceDecl, Base, Builder,
  3072. /*Directly=*/true);
  3073. }
  3074. /// Overloaded method as shortcut for \c isDirectlyDerivedFrom(hasName(...)).
  3075. AST_POLYMORPHIC_MATCHER_P_OVERLOAD(
  3076. isDirectlyDerivedFrom,
  3077. AST_POLYMORPHIC_SUPPORTED_TYPES(CXXRecordDecl, ObjCInterfaceDecl),
  3078. std::string, BaseName, 1) {
  3079. if (BaseName.empty())
  3080. return false;
  3081. const auto M = isDirectlyDerivedFrom(hasName(BaseName));
  3082. if (const auto *RD = dyn_cast<CXXRecordDecl>(&Node))
  3083. return Matcher<CXXRecordDecl>(M).matches(*RD, Finder, Builder);
  3084. const auto *InterfaceDecl = cast<ObjCInterfaceDecl>(&Node);
  3085. return Matcher<ObjCInterfaceDecl>(M).matches(*InterfaceDecl, Finder, Builder);
  3086. }
  3087. /// Matches the first method of a class or struct that satisfies \c
  3088. /// InnerMatcher.
  3089. ///
  3090. /// Given:
  3091. /// \code
  3092. /// class A { void func(); };
  3093. /// class B { void member(); };
  3094. /// \endcode
  3095. ///
  3096. /// \c cxxRecordDecl(hasMethod(hasName("func"))) matches the declaration of
  3097. /// \c A but not \c B.
  3098. AST_MATCHER_P(CXXRecordDecl, hasMethod, internal::Matcher<CXXMethodDecl>,
  3099. InnerMatcher) {
  3100. BoundNodesTreeBuilder Result(*Builder);
  3101. auto MatchIt = matchesFirstInPointerRange(InnerMatcher, Node.method_begin(),
  3102. Node.method_end(), Finder, &Result);
  3103. if (MatchIt == Node.method_end())
  3104. return false;
  3105. if (Finder->isTraversalIgnoringImplicitNodes() && (*MatchIt)->isImplicit())
  3106. return false;
  3107. *Builder = std::move(Result);
  3108. return true;
  3109. }
  3110. /// Matches the generated class of lambda expressions.
  3111. ///
  3112. /// Given:
  3113. /// \code
  3114. /// auto x = []{};
  3115. /// \endcode
  3116. ///
  3117. /// \c cxxRecordDecl(isLambda()) matches the implicit class declaration of
  3118. /// \c decltype(x)
  3119. AST_MATCHER(CXXRecordDecl, isLambda) {
  3120. return Node.isLambda();
  3121. }
  3122. /// Matches AST nodes that have child AST nodes that match the
  3123. /// provided matcher.
  3124. ///
  3125. /// Example matches X, Y
  3126. /// (matcher = cxxRecordDecl(has(cxxRecordDecl(hasName("X")))
  3127. /// \code
  3128. /// class X {}; // Matches X, because X::X is a class of name X inside X.
  3129. /// class Y { class X {}; };
  3130. /// class Z { class Y { class X {}; }; }; // Does not match Z.
  3131. /// \endcode
  3132. ///
  3133. /// ChildT must be an AST base type.
  3134. ///
  3135. /// Usable as: Any Matcher
  3136. /// Note that has is direct matcher, so it also matches things like implicit
  3137. /// casts and paren casts. If you are matching with expr then you should
  3138. /// probably consider using ignoringParenImpCasts like:
  3139. /// has(ignoringParenImpCasts(expr())).
  3140. extern const internal::ArgumentAdaptingMatcherFunc<internal::HasMatcher> has;
  3141. /// Matches AST nodes that have descendant AST nodes that match the
  3142. /// provided matcher.
  3143. ///
  3144. /// Example matches X, Y, Z
  3145. /// (matcher = cxxRecordDecl(hasDescendant(cxxRecordDecl(hasName("X")))))
  3146. /// \code
  3147. /// class X {}; // Matches X, because X::X is a class of name X inside X.
  3148. /// class Y { class X {}; };
  3149. /// class Z { class Y { class X {}; }; };
  3150. /// \endcode
  3151. ///
  3152. /// DescendantT must be an AST base type.
  3153. ///
  3154. /// Usable as: Any Matcher
  3155. extern const internal::ArgumentAdaptingMatcherFunc<
  3156. internal::HasDescendantMatcher>
  3157. hasDescendant;
  3158. /// Matches AST nodes that have child AST nodes that match the
  3159. /// provided matcher.
  3160. ///
  3161. /// Example matches X, Y, Y::X, Z::Y, Z::Y::X
  3162. /// (matcher = cxxRecordDecl(forEach(cxxRecordDecl(hasName("X")))
  3163. /// \code
  3164. /// class X {};
  3165. /// class Y { class X {}; }; // Matches Y, because Y::X is a class of name X
  3166. /// // inside Y.
  3167. /// class Z { class Y { class X {}; }; }; // Does not match Z.
  3168. /// \endcode
  3169. ///
  3170. /// ChildT must be an AST base type.
  3171. ///
  3172. /// As opposed to 'has', 'forEach' will cause a match for each result that
  3173. /// matches instead of only on the first one.
  3174. ///
  3175. /// Usable as: Any Matcher
  3176. extern const internal::ArgumentAdaptingMatcherFunc<internal::ForEachMatcher>
  3177. forEach;
  3178. /// Matches AST nodes that have descendant AST nodes that match the
  3179. /// provided matcher.
  3180. ///
  3181. /// Example matches X, A, A::X, B, B::C, B::C::X
  3182. /// (matcher = cxxRecordDecl(forEachDescendant(cxxRecordDecl(hasName("X")))))
  3183. /// \code
  3184. /// class X {};
  3185. /// class A { class X {}; }; // Matches A, because A::X is a class of name
  3186. /// // X inside A.
  3187. /// class B { class C { class X {}; }; };
  3188. /// \endcode
  3189. ///
  3190. /// DescendantT must be an AST base type.
  3191. ///
  3192. /// As opposed to 'hasDescendant', 'forEachDescendant' will cause a match for
  3193. /// each result that matches instead of only on the first one.
  3194. ///
  3195. /// Note: Recursively combined ForEachDescendant can cause many matches:
  3196. /// cxxRecordDecl(forEachDescendant(cxxRecordDecl(
  3197. /// forEachDescendant(cxxRecordDecl())
  3198. /// )))
  3199. /// will match 10 times (plus injected class name matches) on:
  3200. /// \code
  3201. /// class A { class B { class C { class D { class E {}; }; }; }; };
  3202. /// \endcode
  3203. ///
  3204. /// Usable as: Any Matcher
  3205. extern const internal::ArgumentAdaptingMatcherFunc<
  3206. internal::ForEachDescendantMatcher>
  3207. forEachDescendant;
  3208. /// Matches if the node or any descendant matches.
  3209. ///
  3210. /// Generates results for each match.
  3211. ///
  3212. /// For example, in:
  3213. /// \code
  3214. /// class A { class B {}; class C {}; };
  3215. /// \endcode
  3216. /// The matcher:
  3217. /// \code
  3218. /// cxxRecordDecl(hasName("::A"),
  3219. /// findAll(cxxRecordDecl(isDefinition()).bind("m")))
  3220. /// \endcode
  3221. /// will generate results for \c A, \c B and \c C.
  3222. ///
  3223. /// Usable as: Any Matcher
  3224. template <typename T>
  3225. internal::Matcher<T> findAll(const internal::Matcher<T> &Matcher) {
  3226. return eachOf(Matcher, forEachDescendant(Matcher));
  3227. }
  3228. /// Matches AST nodes that have a parent that matches the provided
  3229. /// matcher.
  3230. ///
  3231. /// Given
  3232. /// \code
  3233. /// void f() { for (;;) { int x = 42; if (true) { int x = 43; } } }
  3234. /// \endcode
  3235. /// \c compoundStmt(hasParent(ifStmt())) matches "{ int x = 43; }".
  3236. ///
  3237. /// Usable as: Any Matcher
  3238. extern const internal::ArgumentAdaptingMatcherFunc<
  3239. internal::HasParentMatcher,
  3240. internal::TypeList<Decl, NestedNameSpecifierLoc, Stmt, TypeLoc, Attr>,
  3241. internal::TypeList<Decl, NestedNameSpecifierLoc, Stmt, TypeLoc, Attr>>
  3242. hasParent;
  3243. /// Matches AST nodes that have an ancestor that matches the provided
  3244. /// matcher.
  3245. ///
  3246. /// Given
  3247. /// \code
  3248. /// void f() { if (true) { int x = 42; } }
  3249. /// void g() { for (;;) { int x = 43; } }
  3250. /// \endcode
  3251. /// \c expr(integerLiteral(hasAncestor(ifStmt()))) matches \c 42, but not 43.
  3252. ///
  3253. /// Usable as: Any Matcher
  3254. extern const internal::ArgumentAdaptingMatcherFunc<
  3255. internal::HasAncestorMatcher,
  3256. internal::TypeList<Decl, NestedNameSpecifierLoc, Stmt, TypeLoc, Attr>,
  3257. internal::TypeList<Decl, NestedNameSpecifierLoc, Stmt, TypeLoc, Attr>>
  3258. hasAncestor;
  3259. /// Matches if the provided matcher does not match.
  3260. ///
  3261. /// Example matches Y (matcher = cxxRecordDecl(unless(hasName("X"))))
  3262. /// \code
  3263. /// class X {};
  3264. /// class Y {};
  3265. /// \endcode
  3266. ///
  3267. /// Usable as: Any Matcher
  3268. extern const internal::VariadicOperatorMatcherFunc<1, 1> unless;
  3269. /// Matches a node if the declaration associated with that node
  3270. /// matches the given matcher.
  3271. ///
  3272. /// The associated declaration is:
  3273. /// - for type nodes, the declaration of the underlying type
  3274. /// - for CallExpr, the declaration of the callee
  3275. /// - for MemberExpr, the declaration of the referenced member
  3276. /// - for CXXConstructExpr, the declaration of the constructor
  3277. /// - for CXXNewExpr, the declaration of the operator new
  3278. /// - for ObjCIvarExpr, the declaration of the ivar
  3279. ///
  3280. /// For type nodes, hasDeclaration will generally match the declaration of the
  3281. /// sugared type. Given
  3282. /// \code
  3283. /// class X {};
  3284. /// typedef X Y;
  3285. /// Y y;
  3286. /// \endcode
  3287. /// in varDecl(hasType(hasDeclaration(decl()))) the decl will match the
  3288. /// typedefDecl. A common use case is to match the underlying, desugared type.
  3289. /// This can be achieved by using the hasUnqualifiedDesugaredType matcher:
  3290. /// \code
  3291. /// varDecl(hasType(hasUnqualifiedDesugaredType(
  3292. /// recordType(hasDeclaration(decl())))))
  3293. /// \endcode
  3294. /// In this matcher, the decl will match the CXXRecordDecl of class X.
  3295. ///
  3296. /// Usable as: Matcher<AddrLabelExpr>, Matcher<CallExpr>,
  3297. /// Matcher<CXXConstructExpr>, Matcher<CXXNewExpr>, Matcher<DeclRefExpr>,
  3298. /// Matcher<EnumType>, Matcher<InjectedClassNameType>, Matcher<LabelStmt>,
  3299. /// Matcher<MemberExpr>, Matcher<QualType>, Matcher<RecordType>,
  3300. /// Matcher<TagType>, Matcher<TemplateSpecializationType>,
  3301. /// Matcher<TemplateTypeParmType>, Matcher<TypedefType>,
  3302. /// Matcher<UnresolvedUsingType>
  3303. inline internal::PolymorphicMatcher<
  3304. internal::HasDeclarationMatcher,
  3305. void(internal::HasDeclarationSupportedTypes), internal::Matcher<Decl>>
  3306. hasDeclaration(const internal::Matcher<Decl> &InnerMatcher) {
  3307. return internal::PolymorphicMatcher<
  3308. internal::HasDeclarationMatcher,
  3309. void(internal::HasDeclarationSupportedTypes), internal::Matcher<Decl>>(
  3310. InnerMatcher);
  3311. }
  3312. /// Matches a \c NamedDecl whose underlying declaration matches the given
  3313. /// matcher.
  3314. ///
  3315. /// Given
  3316. /// \code
  3317. /// namespace N { template<class T> void f(T t); }
  3318. /// template <class T> void g() { using N::f; f(T()); }
  3319. /// \endcode
  3320. /// \c unresolvedLookupExpr(hasAnyDeclaration(
  3321. /// namedDecl(hasUnderlyingDecl(hasName("::N::f")))))
  3322. /// matches the use of \c f in \c g() .
  3323. AST_MATCHER_P(NamedDecl, hasUnderlyingDecl, internal::Matcher<NamedDecl>,
  3324. InnerMatcher) {
  3325. const NamedDecl *UnderlyingDecl = Node.getUnderlyingDecl();
  3326. return UnderlyingDecl != nullptr &&
  3327. InnerMatcher.matches(*UnderlyingDecl, Finder, Builder);
  3328. }
  3329. /// Matches on the implicit object argument of a member call expression, after
  3330. /// stripping off any parentheses or implicit casts.
  3331. ///
  3332. /// Given
  3333. /// \code
  3334. /// class Y { public: void m(); };
  3335. /// Y g();
  3336. /// class X : public Y {};
  3337. /// void z(Y y, X x) { y.m(); (g()).m(); x.m(); }
  3338. /// \endcode
  3339. /// cxxMemberCallExpr(on(hasType(cxxRecordDecl(hasName("Y")))))
  3340. /// matches `y.m()` and `(g()).m()`.
  3341. /// cxxMemberCallExpr(on(hasType(cxxRecordDecl(hasName("X")))))
  3342. /// matches `x.m()`.
  3343. /// cxxMemberCallExpr(on(callExpr()))
  3344. /// matches `(g()).m()`.
  3345. ///
  3346. /// FIXME: Overload to allow directly matching types?
  3347. AST_MATCHER_P(CXXMemberCallExpr, on, internal::Matcher<Expr>,
  3348. InnerMatcher) {
  3349. const Expr *ExprNode = Node.getImplicitObjectArgument()
  3350. ->IgnoreParenImpCasts();
  3351. return (ExprNode != nullptr &&
  3352. InnerMatcher.matches(*ExprNode, Finder, Builder));
  3353. }
  3354. /// Matches on the receiver of an ObjectiveC Message expression.
  3355. ///
  3356. /// Example
  3357. /// matcher = objCMessageExpr(hasReceiverType(asString("UIWebView *")));
  3358. /// matches the [webView ...] message invocation.
  3359. /// \code
  3360. /// NSString *webViewJavaScript = ...
  3361. /// UIWebView *webView = ...
  3362. /// [webView stringByEvaluatingJavaScriptFromString:webViewJavascript];
  3363. /// \endcode
  3364. AST_MATCHER_P(ObjCMessageExpr, hasReceiverType, internal::Matcher<QualType>,
  3365. InnerMatcher) {
  3366. const QualType TypeDecl = Node.getReceiverType();
  3367. return InnerMatcher.matches(TypeDecl, Finder, Builder);
  3368. }
  3369. /// Returns true when the Objective-C method declaration is a class method.
  3370. ///
  3371. /// Example
  3372. /// matcher = objcMethodDecl(isClassMethod())
  3373. /// matches
  3374. /// \code
  3375. /// @interface I + (void)foo; @end
  3376. /// \endcode
  3377. /// but not
  3378. /// \code
  3379. /// @interface I - (void)bar; @end
  3380. /// \endcode
  3381. AST_MATCHER(ObjCMethodDecl, isClassMethod) {
  3382. return Node.isClassMethod();
  3383. }
  3384. /// Returns true when the Objective-C method declaration is an instance method.
  3385. ///
  3386. /// Example
  3387. /// matcher = objcMethodDecl(isInstanceMethod())
  3388. /// matches
  3389. /// \code
  3390. /// @interface I - (void)bar; @end
  3391. /// \endcode
  3392. /// but not
  3393. /// \code
  3394. /// @interface I + (void)foo; @end
  3395. /// \endcode
  3396. AST_MATCHER(ObjCMethodDecl, isInstanceMethod) {
  3397. return Node.isInstanceMethod();
  3398. }
  3399. /// Returns true when the Objective-C message is sent to a class.
  3400. ///
  3401. /// Example
  3402. /// matcher = objcMessageExpr(isClassMessage())
  3403. /// matches
  3404. /// \code
  3405. /// [NSString stringWithFormat:@"format"];
  3406. /// \endcode
  3407. /// but not
  3408. /// \code
  3409. /// NSString *x = @"hello";
  3410. /// [x containsString:@"h"];
  3411. /// \endcode
  3412. AST_MATCHER(ObjCMessageExpr, isClassMessage) {
  3413. return Node.isClassMessage();
  3414. }
  3415. /// Returns true when the Objective-C message is sent to an instance.
  3416. ///
  3417. /// Example
  3418. /// matcher = objcMessageExpr(isInstanceMessage())
  3419. /// matches
  3420. /// \code
  3421. /// NSString *x = @"hello";
  3422. /// [x containsString:@"h"];
  3423. /// \endcode
  3424. /// but not
  3425. /// \code
  3426. /// [NSString stringWithFormat:@"format"];
  3427. /// \endcode
  3428. AST_MATCHER(ObjCMessageExpr, isInstanceMessage) {
  3429. return Node.isInstanceMessage();
  3430. }
  3431. /// Matches if the Objective-C message is sent to an instance,
  3432. /// and the inner matcher matches on that instance.
  3433. ///
  3434. /// For example the method call in
  3435. /// \code
  3436. /// NSString *x = @"hello";
  3437. /// [x containsString:@"h"];
  3438. /// \endcode
  3439. /// is matched by
  3440. /// objcMessageExpr(hasReceiver(declRefExpr(to(varDecl(hasName("x"))))))
  3441. AST_MATCHER_P(ObjCMessageExpr, hasReceiver, internal::Matcher<Expr>,
  3442. InnerMatcher) {
  3443. const Expr *ReceiverNode = Node.getInstanceReceiver();
  3444. return (ReceiverNode != nullptr &&
  3445. InnerMatcher.matches(*ReceiverNode->IgnoreParenImpCasts(), Finder,
  3446. Builder));
  3447. }
  3448. /// Matches when BaseName == Selector.getAsString()
  3449. ///
  3450. /// matcher = objCMessageExpr(hasSelector("loadHTMLString:baseURL:"));
  3451. /// matches the outer message expr in the code below, but NOT the message
  3452. /// invocation for self.bodyView.
  3453. /// \code
  3454. /// [self.bodyView loadHTMLString:html baseURL:NULL];
  3455. /// \endcode
  3456. AST_MATCHER_P(ObjCMessageExpr, hasSelector, std::string, BaseName) {
  3457. Selector Sel = Node.getSelector();
  3458. return BaseName == Sel.getAsString();
  3459. }
  3460. /// Matches when at least one of the supplied string equals to the
  3461. /// Selector.getAsString()
  3462. ///
  3463. /// matcher = objCMessageExpr(hasSelector("methodA:", "methodB:"));
  3464. /// matches both of the expressions below:
  3465. /// \code
  3466. /// [myObj methodA:argA];
  3467. /// [myObj methodB:argB];
  3468. /// \endcode
  3469. extern const internal::VariadicFunction<internal::Matcher<ObjCMessageExpr>,
  3470. StringRef,
  3471. internal::hasAnySelectorFunc>
  3472. hasAnySelector;
  3473. /// Matches ObjC selectors whose name contains
  3474. /// a substring matched by the given RegExp.
  3475. /// matcher = objCMessageExpr(matchesSelector("loadHTMLString\:baseURL?"));
  3476. /// matches the outer message expr in the code below, but NOT the message
  3477. /// invocation for self.bodyView.
  3478. /// \code
  3479. /// [self.bodyView loadHTMLString:html baseURL:NULL];
  3480. /// \endcode
  3481. AST_MATCHER_REGEX(ObjCMessageExpr, matchesSelector, RegExp) {
  3482. std::string SelectorString = Node.getSelector().getAsString();
  3483. return RegExp->match(SelectorString);
  3484. }
  3485. /// Matches when the selector is the empty selector
  3486. ///
  3487. /// Matches only when the selector of the objCMessageExpr is NULL. This may
  3488. /// represent an error condition in the tree!
  3489. AST_MATCHER(ObjCMessageExpr, hasNullSelector) {
  3490. return Node.getSelector().isNull();
  3491. }
  3492. /// Matches when the selector is a Unary Selector
  3493. ///
  3494. /// matcher = objCMessageExpr(matchesSelector(hasUnarySelector());
  3495. /// matches self.bodyView in the code below, but NOT the outer message
  3496. /// invocation of "loadHTMLString:baseURL:".
  3497. /// \code
  3498. /// [self.bodyView loadHTMLString:html baseURL:NULL];
  3499. /// \endcode
  3500. AST_MATCHER(ObjCMessageExpr, hasUnarySelector) {
  3501. return Node.getSelector().isUnarySelector();
  3502. }
  3503. /// Matches when the selector is a keyword selector
  3504. ///
  3505. /// objCMessageExpr(hasKeywordSelector()) matches the generated setFrame
  3506. /// message expression in
  3507. ///
  3508. /// \code
  3509. /// UIWebView *webView = ...;
  3510. /// CGRect bodyFrame = webView.frame;
  3511. /// bodyFrame.size.height = self.bodyContentHeight;
  3512. /// webView.frame = bodyFrame;
  3513. /// // ^---- matches here
  3514. /// \endcode
  3515. AST_MATCHER(ObjCMessageExpr, hasKeywordSelector) {
  3516. return Node.getSelector().isKeywordSelector();
  3517. }
  3518. /// Matches when the selector has the specified number of arguments
  3519. ///
  3520. /// matcher = objCMessageExpr(numSelectorArgs(0));
  3521. /// matches self.bodyView in the code below
  3522. ///
  3523. /// matcher = objCMessageExpr(numSelectorArgs(2));
  3524. /// matches the invocation of "loadHTMLString:baseURL:" but not that
  3525. /// of self.bodyView
  3526. /// \code
  3527. /// [self.bodyView loadHTMLString:html baseURL:NULL];
  3528. /// \endcode
  3529. AST_MATCHER_P(ObjCMessageExpr, numSelectorArgs, unsigned, N) {
  3530. return Node.getSelector().getNumArgs() == N;
  3531. }
  3532. /// Matches if the call expression's callee expression matches.
  3533. ///
  3534. /// Given
  3535. /// \code
  3536. /// class Y { void x() { this->x(); x(); Y y; y.x(); } };
  3537. /// void f() { f(); }
  3538. /// \endcode
  3539. /// callExpr(callee(expr()))
  3540. /// matches this->x(), x(), y.x(), f()
  3541. /// with callee(...)
  3542. /// matching this->x, x, y.x, f respectively
  3543. ///
  3544. /// Note: Callee cannot take the more general internal::Matcher<Expr>
  3545. /// because this introduces ambiguous overloads with calls to Callee taking a
  3546. /// internal::Matcher<Decl>, as the matcher hierarchy is purely
  3547. /// implemented in terms of implicit casts.
  3548. AST_MATCHER_P(CallExpr, callee, internal::Matcher<Stmt>,
  3549. InnerMatcher) {
  3550. const Expr *ExprNode = Node.getCallee();
  3551. return (ExprNode != nullptr &&
  3552. InnerMatcher.matches(*ExprNode, Finder, Builder));
  3553. }
  3554. /// Matches if the call expression's callee's declaration matches the
  3555. /// given matcher.
  3556. ///
  3557. /// Example matches y.x() (matcher = callExpr(callee(
  3558. /// cxxMethodDecl(hasName("x")))))
  3559. /// \code
  3560. /// class Y { public: void x(); };
  3561. /// void z() { Y y; y.x(); }
  3562. /// \endcode
  3563. AST_MATCHER_P_OVERLOAD(CallExpr, callee, internal::Matcher<Decl>, InnerMatcher,
  3564. 1) {
  3565. return callExpr(hasDeclaration(InnerMatcher)).matches(Node, Finder, Builder);
  3566. }
  3567. /// Matches if the expression's or declaration's type matches a type
  3568. /// matcher.
  3569. ///
  3570. /// Example matches x (matcher = expr(hasType(cxxRecordDecl(hasName("X")))))
  3571. /// and z (matcher = varDecl(hasType(cxxRecordDecl(hasName("X")))))
  3572. /// and U (matcher = typedefDecl(hasType(asString("int")))
  3573. /// and friend class X (matcher = friendDecl(hasType("X"))
  3574. /// and public virtual X (matcher = cxxBaseSpecifier(hasType(
  3575. /// asString("class X")))
  3576. /// \code
  3577. /// class X {};
  3578. /// void y(X &x) { x; X z; }
  3579. /// typedef int U;
  3580. /// class Y { friend class X; };
  3581. /// class Z : public virtual X {};
  3582. /// \endcode
  3583. AST_POLYMORPHIC_MATCHER_P_OVERLOAD(
  3584. hasType,
  3585. AST_POLYMORPHIC_SUPPORTED_TYPES(Expr, FriendDecl, TypedefNameDecl,
  3586. ValueDecl, CXXBaseSpecifier),
  3587. internal::Matcher<QualType>, InnerMatcher, 0) {
  3588. QualType QT = internal::getUnderlyingType(Node);
  3589. if (!QT.isNull())
  3590. return InnerMatcher.matches(QT, Finder, Builder);
  3591. return false;
  3592. }
  3593. /// Overloaded to match the declaration of the expression's or value
  3594. /// declaration's type.
  3595. ///
  3596. /// In case of a value declaration (for example a variable declaration),
  3597. /// this resolves one layer of indirection. For example, in the value
  3598. /// declaration "X x;", cxxRecordDecl(hasName("X")) matches the declaration of
  3599. /// X, while varDecl(hasType(cxxRecordDecl(hasName("X")))) matches the
  3600. /// declaration of x.
  3601. ///
  3602. /// Example matches x (matcher = expr(hasType(cxxRecordDecl(hasName("X")))))
  3603. /// and z (matcher = varDecl(hasType(cxxRecordDecl(hasName("X")))))
  3604. /// and friend class X (matcher = friendDecl(hasType("X"))
  3605. /// and public virtual X (matcher = cxxBaseSpecifier(hasType(
  3606. /// cxxRecordDecl(hasName("X"))))
  3607. /// \code
  3608. /// class X {};
  3609. /// void y(X &x) { x; X z; }
  3610. /// class Y { friend class X; };
  3611. /// class Z : public virtual X {};
  3612. /// \endcode
  3613. ///
  3614. /// Example matches class Derived
  3615. /// (matcher = cxxRecordDecl(hasAnyBase(hasType(cxxRecordDecl(hasName("Base"))))))
  3616. /// \code
  3617. /// class Base {};
  3618. /// class Derived : Base {};
  3619. /// \endcode
  3620. ///
  3621. /// Usable as: Matcher<Expr>, Matcher<FriendDecl>, Matcher<ValueDecl>,
  3622. /// Matcher<CXXBaseSpecifier>
  3623. AST_POLYMORPHIC_MATCHER_P_OVERLOAD(
  3624. hasType,
  3625. AST_POLYMORPHIC_SUPPORTED_TYPES(Expr, FriendDecl, ValueDecl,
  3626. CXXBaseSpecifier),
  3627. internal::Matcher<Decl>, InnerMatcher, 1) {
  3628. QualType QT = internal::getUnderlyingType(Node);
  3629. if (!QT.isNull())
  3630. return qualType(hasDeclaration(InnerMatcher)).matches(QT, Finder, Builder);
  3631. return false;
  3632. }
  3633. /// Matches if the type location of a node matches the inner matcher.
  3634. ///
  3635. /// Examples:
  3636. /// \code
  3637. /// int x;
  3638. /// \endcode
  3639. /// declaratorDecl(hasTypeLoc(loc(asString("int"))))
  3640. /// matches int x
  3641. ///
  3642. /// \code
  3643. /// auto x = int(3);
  3644. /// \code
  3645. /// cxxTemporaryObjectExpr(hasTypeLoc(loc(asString("int"))))
  3646. /// matches int(3)
  3647. ///
  3648. /// \code
  3649. /// struct Foo { Foo(int, int); };
  3650. /// auto x = Foo(1, 2);
  3651. /// \code
  3652. /// cxxFunctionalCastExpr(hasTypeLoc(loc(asString("struct Foo"))))
  3653. /// matches Foo(1, 2)
  3654. ///
  3655. /// Usable as: Matcher<BlockDecl>, Matcher<CXXBaseSpecifier>,
  3656. /// Matcher<CXXCtorInitializer>, Matcher<CXXFunctionalCastExpr>,
  3657. /// Matcher<CXXNewExpr>, Matcher<CXXTemporaryObjectExpr>,
  3658. /// Matcher<CXXUnresolvedConstructExpr>,
  3659. /// Matcher<ClassTemplateSpecializationDecl>, Matcher<CompoundLiteralExpr>,
  3660. /// Matcher<DeclaratorDecl>, Matcher<ExplicitCastExpr>,
  3661. /// Matcher<ObjCPropertyDecl>, Matcher<TemplateArgumentLoc>,
  3662. /// Matcher<TypedefNameDecl>
  3663. AST_POLYMORPHIC_MATCHER_P(
  3664. hasTypeLoc,
  3665. AST_POLYMORPHIC_SUPPORTED_TYPES(
  3666. BlockDecl, CXXBaseSpecifier, CXXCtorInitializer, CXXFunctionalCastExpr,
  3667. CXXNewExpr, CXXTemporaryObjectExpr, CXXUnresolvedConstructExpr,
  3668. ClassTemplateSpecializationDecl, CompoundLiteralExpr, DeclaratorDecl,
  3669. ExplicitCastExpr, ObjCPropertyDecl, TemplateArgumentLoc,
  3670. TypedefNameDecl),
  3671. internal::Matcher<TypeLoc>, Inner) {
  3672. TypeSourceInfo *source = internal::GetTypeSourceInfo(Node);
  3673. if (source == nullptr) {
  3674. // This happens for example for implicit destructors.
  3675. return false;
  3676. }
  3677. return Inner.matches(source->getTypeLoc(), Finder, Builder);
  3678. }
  3679. /// Matches if the matched type is represented by the given string.
  3680. ///
  3681. /// Given
  3682. /// \code
  3683. /// class Y { public: void x(); };
  3684. /// void z() { Y* y; y->x(); }
  3685. /// \endcode
  3686. /// cxxMemberCallExpr(on(hasType(asString("class Y *"))))
  3687. /// matches y->x()
  3688. AST_MATCHER_P(QualType, asString, std::string, Name) {
  3689. return Name == Node.getAsString();
  3690. }
  3691. /// Matches if the matched type is a pointer type and the pointee type
  3692. /// matches the specified matcher.
  3693. ///
  3694. /// Example matches y->x()
  3695. /// (matcher = cxxMemberCallExpr(on(hasType(pointsTo
  3696. /// cxxRecordDecl(hasName("Y")))))))
  3697. /// \code
  3698. /// class Y { public: void x(); };
  3699. /// void z() { Y *y; y->x(); }
  3700. /// \endcode
  3701. AST_MATCHER_P(
  3702. QualType, pointsTo, internal::Matcher<QualType>,
  3703. InnerMatcher) {
  3704. return (!Node.isNull() && Node->isAnyPointerType() &&
  3705. InnerMatcher.matches(Node->getPointeeType(), Finder, Builder));
  3706. }
  3707. /// Overloaded to match the pointee type's declaration.
  3708. AST_MATCHER_P_OVERLOAD(QualType, pointsTo, internal::Matcher<Decl>,
  3709. InnerMatcher, 1) {
  3710. return pointsTo(qualType(hasDeclaration(InnerMatcher)))
  3711. .matches(Node, Finder, Builder);
  3712. }
  3713. /// Matches if the matched type matches the unqualified desugared
  3714. /// type of the matched node.
  3715. ///
  3716. /// For example, in:
  3717. /// \code
  3718. /// class A {};
  3719. /// using B = A;
  3720. /// \endcode
  3721. /// The matcher type(hasUnqualifiedDesugaredType(recordType())) matches
  3722. /// both B and A.
  3723. AST_MATCHER_P(Type, hasUnqualifiedDesugaredType, internal::Matcher<Type>,
  3724. InnerMatcher) {
  3725. return InnerMatcher.matches(*Node.getUnqualifiedDesugaredType(), Finder,
  3726. Builder);
  3727. }
  3728. /// Matches if the matched type is a reference type and the referenced
  3729. /// type matches the specified matcher.
  3730. ///
  3731. /// Example matches X &x and const X &y
  3732. /// (matcher = varDecl(hasType(references(cxxRecordDecl(hasName("X"))))))
  3733. /// \code
  3734. /// class X {
  3735. /// void a(X b) {
  3736. /// X &x = b;
  3737. /// const X &y = b;
  3738. /// }
  3739. /// };
  3740. /// \endcode
  3741. AST_MATCHER_P(QualType, references, internal::Matcher<QualType>,
  3742. InnerMatcher) {
  3743. return (!Node.isNull() && Node->isReferenceType() &&
  3744. InnerMatcher.matches(Node->getPointeeType(), Finder, Builder));
  3745. }
  3746. /// Matches QualTypes whose canonical type matches InnerMatcher.
  3747. ///
  3748. /// Given:
  3749. /// \code
  3750. /// typedef int &int_ref;
  3751. /// int a;
  3752. /// int_ref b = a;
  3753. /// \endcode
  3754. ///
  3755. /// \c varDecl(hasType(qualType(referenceType()))))) will not match the
  3756. /// declaration of b but \c
  3757. /// varDecl(hasType(qualType(hasCanonicalType(referenceType())))))) does.
  3758. AST_MATCHER_P(QualType, hasCanonicalType, internal::Matcher<QualType>,
  3759. InnerMatcher) {
  3760. if (Node.isNull())
  3761. return false;
  3762. return InnerMatcher.matches(Node.getCanonicalType(), Finder, Builder);
  3763. }
  3764. /// Overloaded to match the referenced type's declaration.
  3765. AST_MATCHER_P_OVERLOAD(QualType, references, internal::Matcher<Decl>,
  3766. InnerMatcher, 1) {
  3767. return references(qualType(hasDeclaration(InnerMatcher)))
  3768. .matches(Node, Finder, Builder);
  3769. }
  3770. /// Matches on the implicit object argument of a member call expression. Unlike
  3771. /// `on`, matches the argument directly without stripping away anything.
  3772. ///
  3773. /// Given
  3774. /// \code
  3775. /// class Y { public: void m(); };
  3776. /// Y g();
  3777. /// class X : public Y { void g(); };
  3778. /// void z(Y y, X x) { y.m(); x.m(); x.g(); (g()).m(); }
  3779. /// \endcode
  3780. /// cxxMemberCallExpr(onImplicitObjectArgument(hasType(
  3781. /// cxxRecordDecl(hasName("Y")))))
  3782. /// matches `y.m()`, `x.m()` and (g()).m(), but not `x.g()`.
  3783. /// cxxMemberCallExpr(on(callExpr()))
  3784. /// does not match `(g()).m()`, because the parens are not ignored.
  3785. ///
  3786. /// FIXME: Overload to allow directly matching types?
  3787. AST_MATCHER_P(CXXMemberCallExpr, onImplicitObjectArgument,
  3788. internal::Matcher<Expr>, InnerMatcher) {
  3789. const Expr *ExprNode = Node.getImplicitObjectArgument();
  3790. return (ExprNode != nullptr &&
  3791. InnerMatcher.matches(*ExprNode, Finder, Builder));
  3792. }
  3793. /// Matches if the type of the expression's implicit object argument either
  3794. /// matches the InnerMatcher, or is a pointer to a type that matches the
  3795. /// InnerMatcher.
  3796. ///
  3797. /// Given
  3798. /// \code
  3799. /// class Y { public: void m(); };
  3800. /// class X : public Y { void g(); };
  3801. /// void z() { Y y; y.m(); Y *p; p->m(); X x; x.m(); x.g(); }
  3802. /// \endcode
  3803. /// cxxMemberCallExpr(thisPointerType(hasDeclaration(
  3804. /// cxxRecordDecl(hasName("Y")))))
  3805. /// matches `y.m()`, `p->m()` and `x.m()`.
  3806. /// cxxMemberCallExpr(thisPointerType(hasDeclaration(
  3807. /// cxxRecordDecl(hasName("X")))))
  3808. /// matches `x.g()`.
  3809. AST_MATCHER_P_OVERLOAD(CXXMemberCallExpr, thisPointerType,
  3810. internal::Matcher<QualType>, InnerMatcher, 0) {
  3811. return onImplicitObjectArgument(
  3812. anyOf(hasType(InnerMatcher), hasType(pointsTo(InnerMatcher))))
  3813. .matches(Node, Finder, Builder);
  3814. }
  3815. /// Overloaded to match the type's declaration.
  3816. AST_MATCHER_P_OVERLOAD(CXXMemberCallExpr, thisPointerType,
  3817. internal::Matcher<Decl>, InnerMatcher, 1) {
  3818. return onImplicitObjectArgument(
  3819. anyOf(hasType(InnerMatcher), hasType(pointsTo(InnerMatcher))))
  3820. .matches(Node, Finder, Builder);
  3821. }
  3822. /// Matches a DeclRefExpr that refers to a declaration that matches the
  3823. /// specified matcher.
  3824. ///
  3825. /// Example matches x in if(x)
  3826. /// (matcher = declRefExpr(to(varDecl(hasName("x")))))
  3827. /// \code
  3828. /// bool x;
  3829. /// if (x) {}
  3830. /// \endcode
  3831. AST_MATCHER_P(DeclRefExpr, to, internal::Matcher<Decl>,
  3832. InnerMatcher) {
  3833. const Decl *DeclNode = Node.getDecl();
  3834. return (DeclNode != nullptr &&
  3835. InnerMatcher.matches(*DeclNode, Finder, Builder));
  3836. }
  3837. /// Matches if a node refers to a declaration through a specific
  3838. /// using shadow declaration.
  3839. ///
  3840. /// Examples:
  3841. /// \code
  3842. /// namespace a { int f(); }
  3843. /// using a::f;
  3844. /// int x = f();
  3845. /// \endcode
  3846. /// declRefExpr(throughUsingDecl(anything()))
  3847. /// matches \c f
  3848. ///
  3849. /// \code
  3850. /// namespace a { class X{}; }
  3851. /// using a::X;
  3852. /// X x;
  3853. /// \code
  3854. /// typeLoc(loc(usingType(throughUsingDecl(anything()))))
  3855. /// matches \c X
  3856. ///
  3857. /// Usable as: Matcher<DeclRefExpr>, Matcher<UsingType>
  3858. AST_POLYMORPHIC_MATCHER_P(throughUsingDecl,
  3859. AST_POLYMORPHIC_SUPPORTED_TYPES(DeclRefExpr,
  3860. UsingType),
  3861. internal::Matcher<UsingShadowDecl>, Inner) {
  3862. const NamedDecl *FoundDecl = Node.getFoundDecl();
  3863. if (const UsingShadowDecl *UsingDecl = dyn_cast<UsingShadowDecl>(FoundDecl))
  3864. return Inner.matches(*UsingDecl, Finder, Builder);
  3865. return false;
  3866. }
  3867. /// Matches an \c OverloadExpr if any of the declarations in the set of
  3868. /// overloads matches the given matcher.
  3869. ///
  3870. /// Given
  3871. /// \code
  3872. /// template <typename T> void foo(T);
  3873. /// template <typename T> void bar(T);
  3874. /// template <typename T> void baz(T t) {
  3875. /// foo(t);
  3876. /// bar(t);
  3877. /// }
  3878. /// \endcode
  3879. /// unresolvedLookupExpr(hasAnyDeclaration(
  3880. /// functionTemplateDecl(hasName("foo"))))
  3881. /// matches \c foo in \c foo(t); but not \c bar in \c bar(t);
  3882. AST_MATCHER_P(OverloadExpr, hasAnyDeclaration, internal::Matcher<Decl>,
  3883. InnerMatcher) {
  3884. return matchesFirstInPointerRange(InnerMatcher, Node.decls_begin(),
  3885. Node.decls_end(), Finder,
  3886. Builder) != Node.decls_end();
  3887. }
  3888. /// Matches the Decl of a DeclStmt which has a single declaration.
  3889. ///
  3890. /// Given
  3891. /// \code
  3892. /// int a, b;
  3893. /// int c;
  3894. /// \endcode
  3895. /// declStmt(hasSingleDecl(anything()))
  3896. /// matches 'int c;' but not 'int a, b;'.
  3897. AST_MATCHER_P(DeclStmt, hasSingleDecl, internal::Matcher<Decl>, InnerMatcher) {
  3898. if (Node.isSingleDecl()) {
  3899. const Decl *FoundDecl = Node.getSingleDecl();
  3900. return InnerMatcher.matches(*FoundDecl, Finder, Builder);
  3901. }
  3902. return false;
  3903. }
  3904. /// Matches a variable declaration that has an initializer expression
  3905. /// that matches the given matcher.
  3906. ///
  3907. /// Example matches x (matcher = varDecl(hasInitializer(callExpr())))
  3908. /// \code
  3909. /// bool y() { return true; }
  3910. /// bool x = y();
  3911. /// \endcode
  3912. AST_MATCHER_P(
  3913. VarDecl, hasInitializer, internal::Matcher<Expr>,
  3914. InnerMatcher) {
  3915. const Expr *Initializer = Node.getAnyInitializer();
  3916. return (Initializer != nullptr &&
  3917. InnerMatcher.matches(*Initializer, Finder, Builder));
  3918. }
  3919. /// Matches a variable serving as the implicit variable for a lambda init-
  3920. /// capture.
  3921. ///
  3922. /// Example matches x (matcher = varDecl(isInitCapture()))
  3923. /// \code
  3924. /// auto f = [x=3]() { return x; };
  3925. /// \endcode
  3926. AST_MATCHER(VarDecl, isInitCapture) { return Node.isInitCapture(); }
  3927. /// Matches each lambda capture in a lambda expression.
  3928. ///
  3929. /// Given
  3930. /// \code
  3931. /// int main() {
  3932. /// int x, y;
  3933. /// float z;
  3934. /// auto f = [=]() { return x + y + z; };
  3935. /// }
  3936. /// \endcode
  3937. /// lambdaExpr(forEachLambdaCapture(
  3938. /// lambdaCapture(capturesVar(varDecl(hasType(isInteger()))))))
  3939. /// will trigger two matches, binding for 'x' and 'y' respectively.
  3940. AST_MATCHER_P(LambdaExpr, forEachLambdaCapture,
  3941. internal::Matcher<LambdaCapture>, InnerMatcher) {
  3942. BoundNodesTreeBuilder Result;
  3943. bool Matched = false;
  3944. for (const auto &Capture : Node.captures()) {
  3945. if (Finder->isTraversalIgnoringImplicitNodes() && Capture.isImplicit())
  3946. continue;
  3947. BoundNodesTreeBuilder CaptureBuilder(*Builder);
  3948. if (InnerMatcher.matches(Capture, Finder, &CaptureBuilder)) {
  3949. Matched = true;
  3950. Result.addMatch(CaptureBuilder);
  3951. }
  3952. }
  3953. *Builder = std::move(Result);
  3954. return Matched;
  3955. }
  3956. /// \brief Matches a static variable with local scope.
  3957. ///
  3958. /// Example matches y (matcher = varDecl(isStaticLocal()))
  3959. /// \code
  3960. /// void f() {
  3961. /// int x;
  3962. /// static int y;
  3963. /// }
  3964. /// static int z;
  3965. /// \endcode
  3966. AST_MATCHER(VarDecl, isStaticLocal) {
  3967. return Node.isStaticLocal();
  3968. }
  3969. /// Matches a variable declaration that has function scope and is a
  3970. /// non-static local variable.
  3971. ///
  3972. /// Example matches x (matcher = varDecl(hasLocalStorage())
  3973. /// \code
  3974. /// void f() {
  3975. /// int x;
  3976. /// static int y;
  3977. /// }
  3978. /// int z;
  3979. /// \endcode
  3980. AST_MATCHER(VarDecl, hasLocalStorage) {
  3981. return Node.hasLocalStorage();
  3982. }
  3983. /// Matches a variable declaration that does not have local storage.
  3984. ///
  3985. /// Example matches y and z (matcher = varDecl(hasGlobalStorage())
  3986. /// \code
  3987. /// void f() {
  3988. /// int x;
  3989. /// static int y;
  3990. /// }
  3991. /// int z;
  3992. /// \endcode
  3993. AST_MATCHER(VarDecl, hasGlobalStorage) {
  3994. return Node.hasGlobalStorage();
  3995. }
  3996. /// Matches a variable declaration that has automatic storage duration.
  3997. ///
  3998. /// Example matches x, but not y, z, or a.
  3999. /// (matcher = varDecl(hasAutomaticStorageDuration())
  4000. /// \code
  4001. /// void f() {
  4002. /// int x;
  4003. /// static int y;
  4004. /// thread_local int z;
  4005. /// }
  4006. /// int a;
  4007. /// \endcode
  4008. AST_MATCHER(VarDecl, hasAutomaticStorageDuration) {
  4009. return Node.getStorageDuration() == SD_Automatic;
  4010. }
  4011. /// Matches a variable declaration that has static storage duration.
  4012. /// It includes the variable declared at namespace scope and those declared
  4013. /// with "static" and "extern" storage class specifiers.
  4014. ///
  4015. /// \code
  4016. /// void f() {
  4017. /// int x;
  4018. /// static int y;
  4019. /// thread_local int z;
  4020. /// }
  4021. /// int a;
  4022. /// static int b;
  4023. /// extern int c;
  4024. /// varDecl(hasStaticStorageDuration())
  4025. /// matches the function declaration y, a, b and c.
  4026. /// \endcode
  4027. AST_MATCHER(VarDecl, hasStaticStorageDuration) {
  4028. return Node.getStorageDuration() == SD_Static;
  4029. }
  4030. /// Matches a variable declaration that has thread storage duration.
  4031. ///
  4032. /// Example matches z, but not x, z, or a.
  4033. /// (matcher = varDecl(hasThreadStorageDuration())
  4034. /// \code
  4035. /// void f() {
  4036. /// int x;
  4037. /// static int y;
  4038. /// thread_local int z;
  4039. /// }
  4040. /// int a;
  4041. /// \endcode
  4042. AST_MATCHER(VarDecl, hasThreadStorageDuration) {
  4043. return Node.getStorageDuration() == SD_Thread;
  4044. }
  4045. /// Matches a variable declaration that is an exception variable from
  4046. /// a C++ catch block, or an Objective-C \@catch statement.
  4047. ///
  4048. /// Example matches x (matcher = varDecl(isExceptionVariable())
  4049. /// \code
  4050. /// void f(int y) {
  4051. /// try {
  4052. /// } catch (int x) {
  4053. /// }
  4054. /// }
  4055. /// \endcode
  4056. AST_MATCHER(VarDecl, isExceptionVariable) {
  4057. return Node.isExceptionVariable();
  4058. }
  4059. /// Checks that a call expression or a constructor call expression has
  4060. /// a specific number of arguments (including absent default arguments).
  4061. ///
  4062. /// Example matches f(0, 0) (matcher = callExpr(argumentCountIs(2)))
  4063. /// \code
  4064. /// void f(int x, int y);
  4065. /// f(0, 0);
  4066. /// \endcode
  4067. AST_POLYMORPHIC_MATCHER_P(argumentCountIs,
  4068. AST_POLYMORPHIC_SUPPORTED_TYPES(
  4069. CallExpr, CXXConstructExpr,
  4070. CXXUnresolvedConstructExpr, ObjCMessageExpr),
  4071. unsigned, N) {
  4072. unsigned NumArgs = Node.getNumArgs();
  4073. if (!Finder->isTraversalIgnoringImplicitNodes())
  4074. return NumArgs == N;
  4075. while (NumArgs) {
  4076. if (!isa<CXXDefaultArgExpr>(Node.getArg(NumArgs - 1)))
  4077. break;
  4078. --NumArgs;
  4079. }
  4080. return NumArgs == N;
  4081. }
  4082. /// Matches the n'th argument of a call expression or a constructor
  4083. /// call expression.
  4084. ///
  4085. /// Example matches y in x(y)
  4086. /// (matcher = callExpr(hasArgument(0, declRefExpr())))
  4087. /// \code
  4088. /// void x(int) { int y; x(y); }
  4089. /// \endcode
  4090. AST_POLYMORPHIC_MATCHER_P2(hasArgument,
  4091. AST_POLYMORPHIC_SUPPORTED_TYPES(
  4092. CallExpr, CXXConstructExpr,
  4093. CXXUnresolvedConstructExpr, ObjCMessageExpr),
  4094. unsigned, N, internal::Matcher<Expr>, InnerMatcher) {
  4095. if (N >= Node.getNumArgs())
  4096. return false;
  4097. const Expr *Arg = Node.getArg(N);
  4098. if (Finder->isTraversalIgnoringImplicitNodes() && isa<CXXDefaultArgExpr>(Arg))
  4099. return false;
  4100. return InnerMatcher.matches(*Arg->IgnoreParenImpCasts(), Finder, Builder);
  4101. }
  4102. /// Matches the n'th item of an initializer list expression.
  4103. ///
  4104. /// Example matches y.
  4105. /// (matcher = initListExpr(hasInit(0, expr())))
  4106. /// \code
  4107. /// int x{y}.
  4108. /// \endcode
  4109. AST_MATCHER_P2(InitListExpr, hasInit, unsigned, N,
  4110. ast_matchers::internal::Matcher<Expr>, InnerMatcher) {
  4111. return N < Node.getNumInits() &&
  4112. InnerMatcher.matches(*Node.getInit(N), Finder, Builder);
  4113. }
  4114. /// Matches declaration statements that contain a specific number of
  4115. /// declarations.
  4116. ///
  4117. /// Example: Given
  4118. /// \code
  4119. /// int a, b;
  4120. /// int c;
  4121. /// int d = 2, e;
  4122. /// \endcode
  4123. /// declCountIs(2)
  4124. /// matches 'int a, b;' and 'int d = 2, e;', but not 'int c;'.
  4125. AST_MATCHER_P(DeclStmt, declCountIs, unsigned, N) {
  4126. return std::distance(Node.decl_begin(), Node.decl_end()) == (ptrdiff_t)N;
  4127. }
  4128. /// Matches the n'th declaration of a declaration statement.
  4129. ///
  4130. /// Note that this does not work for global declarations because the AST
  4131. /// breaks up multiple-declaration DeclStmt's into multiple single-declaration
  4132. /// DeclStmt's.
  4133. /// Example: Given non-global declarations
  4134. /// \code
  4135. /// int a, b = 0;
  4136. /// int c;
  4137. /// int d = 2, e;
  4138. /// \endcode
  4139. /// declStmt(containsDeclaration(
  4140. /// 0, varDecl(hasInitializer(anything()))))
  4141. /// matches only 'int d = 2, e;', and
  4142. /// declStmt(containsDeclaration(1, varDecl()))
  4143. /// \code
  4144. /// matches 'int a, b = 0' as well as 'int d = 2, e;'
  4145. /// but 'int c;' is not matched.
  4146. /// \endcode
  4147. AST_MATCHER_P2(DeclStmt, containsDeclaration, unsigned, N,
  4148. internal::Matcher<Decl>, InnerMatcher) {
  4149. const unsigned NumDecls = std::distance(Node.decl_begin(), Node.decl_end());
  4150. if (N >= NumDecls)
  4151. return false;
  4152. DeclStmt::const_decl_iterator Iterator = Node.decl_begin();
  4153. std::advance(Iterator, N);
  4154. return InnerMatcher.matches(**Iterator, Finder, Builder);
  4155. }
  4156. /// Matches a C++ catch statement that has a catch-all handler.
  4157. ///
  4158. /// Given
  4159. /// \code
  4160. /// try {
  4161. /// // ...
  4162. /// } catch (int) {
  4163. /// // ...
  4164. /// } catch (...) {
  4165. /// // ...
  4166. /// }
  4167. /// \endcode
  4168. /// cxxCatchStmt(isCatchAll()) matches catch(...) but not catch(int).
  4169. AST_MATCHER(CXXCatchStmt, isCatchAll) {
  4170. return Node.getExceptionDecl() == nullptr;
  4171. }
  4172. /// Matches a constructor initializer.
  4173. ///
  4174. /// Given
  4175. /// \code
  4176. /// struct Foo {
  4177. /// Foo() : foo_(1) { }
  4178. /// int foo_;
  4179. /// };
  4180. /// \endcode
  4181. /// cxxRecordDecl(has(cxxConstructorDecl(
  4182. /// hasAnyConstructorInitializer(anything())
  4183. /// )))
  4184. /// record matches Foo, hasAnyConstructorInitializer matches foo_(1)
  4185. AST_MATCHER_P(CXXConstructorDecl, hasAnyConstructorInitializer,
  4186. internal::Matcher<CXXCtorInitializer>, InnerMatcher) {
  4187. auto MatchIt = matchesFirstInPointerRange(InnerMatcher, Node.init_begin(),
  4188. Node.init_end(), Finder, Builder);
  4189. if (MatchIt == Node.init_end())
  4190. return false;
  4191. return (*MatchIt)->isWritten() || !Finder->isTraversalIgnoringImplicitNodes();
  4192. }
  4193. /// Matches the field declaration of a constructor initializer.
  4194. ///
  4195. /// Given
  4196. /// \code
  4197. /// struct Foo {
  4198. /// Foo() : foo_(1) { }
  4199. /// int foo_;
  4200. /// };
  4201. /// \endcode
  4202. /// cxxRecordDecl(has(cxxConstructorDecl(hasAnyConstructorInitializer(
  4203. /// forField(hasName("foo_"))))))
  4204. /// matches Foo
  4205. /// with forField matching foo_
  4206. AST_MATCHER_P(CXXCtorInitializer, forField,
  4207. internal::Matcher<FieldDecl>, InnerMatcher) {
  4208. const FieldDecl *NodeAsDecl = Node.getAnyMember();
  4209. return (NodeAsDecl != nullptr &&
  4210. InnerMatcher.matches(*NodeAsDecl, Finder, Builder));
  4211. }
  4212. /// Matches the initializer expression of a constructor initializer.
  4213. ///
  4214. /// Given
  4215. /// \code
  4216. /// struct Foo {
  4217. /// Foo() : foo_(1) { }
  4218. /// int foo_;
  4219. /// };
  4220. /// \endcode
  4221. /// cxxRecordDecl(has(cxxConstructorDecl(hasAnyConstructorInitializer(
  4222. /// withInitializer(integerLiteral(equals(1)))))))
  4223. /// matches Foo
  4224. /// with withInitializer matching (1)
  4225. AST_MATCHER_P(CXXCtorInitializer, withInitializer,
  4226. internal::Matcher<Expr>, InnerMatcher) {
  4227. const Expr* NodeAsExpr = Node.getInit();
  4228. return (NodeAsExpr != nullptr &&
  4229. InnerMatcher.matches(*NodeAsExpr, Finder, Builder));
  4230. }
  4231. /// Matches a constructor initializer if it is explicitly written in
  4232. /// code (as opposed to implicitly added by the compiler).
  4233. ///
  4234. /// Given
  4235. /// \code
  4236. /// struct Foo {
  4237. /// Foo() { }
  4238. /// Foo(int) : foo_("A") { }
  4239. /// string foo_;
  4240. /// };
  4241. /// \endcode
  4242. /// cxxConstructorDecl(hasAnyConstructorInitializer(isWritten()))
  4243. /// will match Foo(int), but not Foo()
  4244. AST_MATCHER(CXXCtorInitializer, isWritten) {
  4245. return Node.isWritten();
  4246. }
  4247. /// Matches a constructor initializer if it is initializing a base, as
  4248. /// opposed to a member.
  4249. ///
  4250. /// Given
  4251. /// \code
  4252. /// struct B {};
  4253. /// struct D : B {
  4254. /// int I;
  4255. /// D(int i) : I(i) {}
  4256. /// };
  4257. /// struct E : B {
  4258. /// E() : B() {}
  4259. /// };
  4260. /// \endcode
  4261. /// cxxConstructorDecl(hasAnyConstructorInitializer(isBaseInitializer()))
  4262. /// will match E(), but not match D(int).
  4263. AST_MATCHER(CXXCtorInitializer, isBaseInitializer) {
  4264. return Node.isBaseInitializer();
  4265. }
  4266. /// Matches a constructor initializer if it is initializing a member, as
  4267. /// opposed to a base.
  4268. ///
  4269. /// Given
  4270. /// \code
  4271. /// struct B {};
  4272. /// struct D : B {
  4273. /// int I;
  4274. /// D(int i) : I(i) {}
  4275. /// };
  4276. /// struct E : B {
  4277. /// E() : B() {}
  4278. /// };
  4279. /// \endcode
  4280. /// cxxConstructorDecl(hasAnyConstructorInitializer(isMemberInitializer()))
  4281. /// will match D(int), but not match E().
  4282. AST_MATCHER(CXXCtorInitializer, isMemberInitializer) {
  4283. return Node.isMemberInitializer();
  4284. }
  4285. /// Matches any argument of a call expression or a constructor call
  4286. /// expression, or an ObjC-message-send expression.
  4287. ///
  4288. /// Given
  4289. /// \code
  4290. /// void x(int, int, int) { int y; x(1, y, 42); }
  4291. /// \endcode
  4292. /// callExpr(hasAnyArgument(declRefExpr()))
  4293. /// matches x(1, y, 42)
  4294. /// with hasAnyArgument(...)
  4295. /// matching y
  4296. ///
  4297. /// For ObjectiveC, given
  4298. /// \code
  4299. /// @interface I - (void) f:(int) y; @end
  4300. /// void foo(I *i) { [i f:12]; }
  4301. /// \endcode
  4302. /// objcMessageExpr(hasAnyArgument(integerLiteral(equals(12))))
  4303. /// matches [i f:12]
  4304. AST_POLYMORPHIC_MATCHER_P(hasAnyArgument,
  4305. AST_POLYMORPHIC_SUPPORTED_TYPES(
  4306. CallExpr, CXXConstructExpr,
  4307. CXXUnresolvedConstructExpr, ObjCMessageExpr),
  4308. internal::Matcher<Expr>, InnerMatcher) {
  4309. for (const Expr *Arg : Node.arguments()) {
  4310. if (Finder->isTraversalIgnoringImplicitNodes() &&
  4311. isa<CXXDefaultArgExpr>(Arg))
  4312. break;
  4313. BoundNodesTreeBuilder Result(*Builder);
  4314. if (InnerMatcher.matches(*Arg, Finder, &Result)) {
  4315. *Builder = std::move(Result);
  4316. return true;
  4317. }
  4318. }
  4319. return false;
  4320. }
  4321. /// Matches lambda captures.
  4322. ///
  4323. /// Given
  4324. /// \code
  4325. /// int main() {
  4326. /// int x;
  4327. /// auto f = [x](){};
  4328. /// auto g = [x = 1](){};
  4329. /// }
  4330. /// \endcode
  4331. /// In the matcher `lambdaExpr(hasAnyCapture(lambdaCapture()))`,
  4332. /// `lambdaCapture()` matches `x` and `x=1`.
  4333. extern const internal::VariadicAllOfMatcher<LambdaCapture> lambdaCapture;
  4334. /// Matches any capture in a lambda expression.
  4335. ///
  4336. /// Given
  4337. /// \code
  4338. /// void foo() {
  4339. /// int t = 5;
  4340. /// auto f = [=](){ return t; };
  4341. /// }
  4342. /// \endcode
  4343. /// lambdaExpr(hasAnyCapture(lambdaCapture())) and
  4344. /// lambdaExpr(hasAnyCapture(lambdaCapture(refersToVarDecl(hasName("t")))))
  4345. /// both match `[=](){ return t; }`.
  4346. AST_MATCHER_P(LambdaExpr, hasAnyCapture, internal::Matcher<LambdaCapture>,
  4347. InnerMatcher) {
  4348. for (const LambdaCapture &Capture : Node.captures()) {
  4349. clang::ast_matchers::internal::BoundNodesTreeBuilder Result(*Builder);
  4350. if (InnerMatcher.matches(Capture, Finder, &Result)) {
  4351. *Builder = std::move(Result);
  4352. return true;
  4353. }
  4354. }
  4355. return false;
  4356. }
  4357. /// Matches a `LambdaCapture` that refers to the specified `VarDecl`. The
  4358. /// `VarDecl` can be a separate variable that is captured by value or
  4359. /// reference, or a synthesized variable if the capture has an initializer.
  4360. ///
  4361. /// Given
  4362. /// \code
  4363. /// void foo() {
  4364. /// int x;
  4365. /// auto f = [x](){};
  4366. /// auto g = [x = 1](){};
  4367. /// }
  4368. /// \endcode
  4369. /// In the matcher
  4370. /// lambdaExpr(hasAnyCapture(lambdaCapture(capturesVar(hasName("x")))),
  4371. /// capturesVar(hasName("x")) matches `x` and `x = 1`.
  4372. AST_MATCHER_P(LambdaCapture, capturesVar, internal::Matcher<VarDecl>,
  4373. InnerMatcher) {
  4374. auto *capturedVar = Node.getCapturedVar();
  4375. return capturedVar && InnerMatcher.matches(*capturedVar, Finder, Builder);
  4376. }
  4377. /// Matches a `LambdaCapture` that refers to 'this'.
  4378. ///
  4379. /// Given
  4380. /// \code
  4381. /// class C {
  4382. /// int cc;
  4383. /// int f() {
  4384. /// auto l = [this]() { return cc; };
  4385. /// return l();
  4386. /// }
  4387. /// };
  4388. /// \endcode
  4389. /// lambdaExpr(hasAnyCapture(lambdaCapture(capturesThis())))
  4390. /// matches `[this]() { return cc; }`.
  4391. AST_MATCHER(LambdaCapture, capturesThis) { return Node.capturesThis(); }
  4392. /// Matches a constructor call expression which uses list initialization.
  4393. AST_MATCHER(CXXConstructExpr, isListInitialization) {
  4394. return Node.isListInitialization();
  4395. }
  4396. /// Matches a constructor call expression which requires
  4397. /// zero initialization.
  4398. ///
  4399. /// Given
  4400. /// \code
  4401. /// void foo() {
  4402. /// struct point { double x; double y; };
  4403. /// point pt[2] = { { 1.0, 2.0 } };
  4404. /// }
  4405. /// \endcode
  4406. /// initListExpr(has(cxxConstructExpr(requiresZeroInitialization()))
  4407. /// will match the implicit array filler for pt[1].
  4408. AST_MATCHER(CXXConstructExpr, requiresZeroInitialization) {
  4409. return Node.requiresZeroInitialization();
  4410. }
  4411. /// Matches the n'th parameter of a function or an ObjC method
  4412. /// declaration or a block.
  4413. ///
  4414. /// Given
  4415. /// \code
  4416. /// class X { void f(int x) {} };
  4417. /// \endcode
  4418. /// cxxMethodDecl(hasParameter(0, hasType(varDecl())))
  4419. /// matches f(int x) {}
  4420. /// with hasParameter(...)
  4421. /// matching int x
  4422. ///
  4423. /// For ObjectiveC, given
  4424. /// \code
  4425. /// @interface I - (void) f:(int) y; @end
  4426. /// \endcode
  4427. //
  4428. /// the matcher objcMethodDecl(hasParameter(0, hasName("y")))
  4429. /// matches the declaration of method f with hasParameter
  4430. /// matching y.
  4431. AST_POLYMORPHIC_MATCHER_P2(hasParameter,
  4432. AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl,
  4433. ObjCMethodDecl,
  4434. BlockDecl),
  4435. unsigned, N, internal::Matcher<ParmVarDecl>,
  4436. InnerMatcher) {
  4437. return (N < Node.parameters().size()
  4438. && InnerMatcher.matches(*Node.parameters()[N], Finder, Builder));
  4439. }
  4440. /// Matches all arguments and their respective ParmVarDecl.
  4441. ///
  4442. /// Given
  4443. /// \code
  4444. /// void f(int i);
  4445. /// int y;
  4446. /// f(y);
  4447. /// \endcode
  4448. /// callExpr(
  4449. /// forEachArgumentWithParam(
  4450. /// declRefExpr(to(varDecl(hasName("y")))),
  4451. /// parmVarDecl(hasType(isInteger()))
  4452. /// ))
  4453. /// matches f(y);
  4454. /// with declRefExpr(...)
  4455. /// matching int y
  4456. /// and parmVarDecl(...)
  4457. /// matching int i
  4458. AST_POLYMORPHIC_MATCHER_P2(forEachArgumentWithParam,
  4459. AST_POLYMORPHIC_SUPPORTED_TYPES(CallExpr,
  4460. CXXConstructExpr),
  4461. internal::Matcher<Expr>, ArgMatcher,
  4462. internal::Matcher<ParmVarDecl>, ParamMatcher) {
  4463. BoundNodesTreeBuilder Result;
  4464. // The first argument of an overloaded member operator is the implicit object
  4465. // argument of the method which should not be matched against a parameter, so
  4466. // we skip over it here.
  4467. BoundNodesTreeBuilder Matches;
  4468. unsigned ArgIndex = cxxOperatorCallExpr(callee(cxxMethodDecl()))
  4469. .matches(Node, Finder, &Matches)
  4470. ? 1
  4471. : 0;
  4472. int ParamIndex = 0;
  4473. bool Matched = false;
  4474. for (; ArgIndex < Node.getNumArgs(); ++ArgIndex) {
  4475. BoundNodesTreeBuilder ArgMatches(*Builder);
  4476. if (ArgMatcher.matches(*(Node.getArg(ArgIndex)->IgnoreParenCasts()),
  4477. Finder, &ArgMatches)) {
  4478. BoundNodesTreeBuilder ParamMatches(ArgMatches);
  4479. if (expr(anyOf(cxxConstructExpr(hasDeclaration(cxxConstructorDecl(
  4480. hasParameter(ParamIndex, ParamMatcher)))),
  4481. callExpr(callee(functionDecl(
  4482. hasParameter(ParamIndex, ParamMatcher))))))
  4483. .matches(Node, Finder, &ParamMatches)) {
  4484. Result.addMatch(ParamMatches);
  4485. Matched = true;
  4486. }
  4487. }
  4488. ++ParamIndex;
  4489. }
  4490. *Builder = std::move(Result);
  4491. return Matched;
  4492. }
  4493. /// Matches all arguments and their respective types for a \c CallExpr or
  4494. /// \c CXXConstructExpr. It is very similar to \c forEachArgumentWithParam but
  4495. /// it works on calls through function pointers as well.
  4496. ///
  4497. /// The difference is, that function pointers do not provide access to a
  4498. /// \c ParmVarDecl, but only the \c QualType for each argument.
  4499. ///
  4500. /// Given
  4501. /// \code
  4502. /// void f(int i);
  4503. /// int y;
  4504. /// f(y);
  4505. /// void (*f_ptr)(int) = f;
  4506. /// f_ptr(y);
  4507. /// \endcode
  4508. /// callExpr(
  4509. /// forEachArgumentWithParamType(
  4510. /// declRefExpr(to(varDecl(hasName("y")))),
  4511. /// qualType(isInteger()).bind("type)
  4512. /// ))
  4513. /// matches f(y) and f_ptr(y)
  4514. /// with declRefExpr(...)
  4515. /// matching int y
  4516. /// and qualType(...)
  4517. /// matching int
  4518. AST_POLYMORPHIC_MATCHER_P2(forEachArgumentWithParamType,
  4519. AST_POLYMORPHIC_SUPPORTED_TYPES(CallExpr,
  4520. CXXConstructExpr),
  4521. internal::Matcher<Expr>, ArgMatcher,
  4522. internal::Matcher<QualType>, ParamMatcher) {
  4523. BoundNodesTreeBuilder Result;
  4524. // The first argument of an overloaded member operator is the implicit object
  4525. // argument of the method which should not be matched against a parameter, so
  4526. // we skip over it here.
  4527. BoundNodesTreeBuilder Matches;
  4528. unsigned ArgIndex = cxxOperatorCallExpr(callee(cxxMethodDecl()))
  4529. .matches(Node, Finder, &Matches)
  4530. ? 1
  4531. : 0;
  4532. const FunctionProtoType *FProto = nullptr;
  4533. if (const auto *Call = dyn_cast<CallExpr>(&Node)) {
  4534. if (const auto *Value =
  4535. dyn_cast_or_null<ValueDecl>(Call->getCalleeDecl())) {
  4536. QualType QT = Value->getType().getCanonicalType();
  4537. // This does not necessarily lead to a `FunctionProtoType`,
  4538. // e.g. K&R functions do not have a function prototype.
  4539. if (QT->isFunctionPointerType())
  4540. FProto = QT->getPointeeType()->getAs<FunctionProtoType>();
  4541. if (QT->isMemberFunctionPointerType()) {
  4542. const auto *MP = QT->getAs<MemberPointerType>();
  4543. assert(MP && "Must be member-pointer if its a memberfunctionpointer");
  4544. FProto = MP->getPointeeType()->getAs<FunctionProtoType>();
  4545. assert(FProto &&
  4546. "The call must have happened through a member function "
  4547. "pointer");
  4548. }
  4549. }
  4550. }
  4551. unsigned ParamIndex = 0;
  4552. bool Matched = false;
  4553. unsigned NumArgs = Node.getNumArgs();
  4554. if (FProto && FProto->isVariadic())
  4555. NumArgs = std::min(NumArgs, FProto->getNumParams());
  4556. for (; ArgIndex < NumArgs; ++ArgIndex, ++ParamIndex) {
  4557. BoundNodesTreeBuilder ArgMatches(*Builder);
  4558. if (ArgMatcher.matches(*(Node.getArg(ArgIndex)->IgnoreParenCasts()), Finder,
  4559. &ArgMatches)) {
  4560. BoundNodesTreeBuilder ParamMatches(ArgMatches);
  4561. // This test is cheaper compared to the big matcher in the next if.
  4562. // Therefore, please keep this order.
  4563. if (FProto && FProto->getNumParams() > ParamIndex) {
  4564. QualType ParamType = FProto->getParamType(ParamIndex);
  4565. if (ParamMatcher.matches(ParamType, Finder, &ParamMatches)) {
  4566. Result.addMatch(ParamMatches);
  4567. Matched = true;
  4568. continue;
  4569. }
  4570. }
  4571. if (expr(anyOf(cxxConstructExpr(hasDeclaration(cxxConstructorDecl(
  4572. hasParameter(ParamIndex, hasType(ParamMatcher))))),
  4573. callExpr(callee(functionDecl(
  4574. hasParameter(ParamIndex, hasType(ParamMatcher)))))))
  4575. .matches(Node, Finder, &ParamMatches)) {
  4576. Result.addMatch(ParamMatches);
  4577. Matched = true;
  4578. continue;
  4579. }
  4580. }
  4581. }
  4582. *Builder = std::move(Result);
  4583. return Matched;
  4584. }
  4585. /// Matches the ParmVarDecl nodes that are at the N'th position in the parameter
  4586. /// list. The parameter list could be that of either a block, function, or
  4587. /// objc-method.
  4588. ///
  4589. ///
  4590. /// Given
  4591. ///
  4592. /// \code
  4593. /// void f(int a, int b, int c) {
  4594. /// }
  4595. /// \endcode
  4596. ///
  4597. /// ``parmVarDecl(isAtPosition(0))`` matches ``int a``.
  4598. ///
  4599. /// ``parmVarDecl(isAtPosition(1))`` matches ``int b``.
  4600. AST_MATCHER_P(ParmVarDecl, isAtPosition, unsigned, N) {
  4601. const clang::DeclContext *Context = Node.getParentFunctionOrMethod();
  4602. if (const auto *Decl = dyn_cast_or_null<FunctionDecl>(Context))
  4603. return N < Decl->param_size() && Decl->getParamDecl(N) == &Node;
  4604. if (const auto *Decl = dyn_cast_or_null<BlockDecl>(Context))
  4605. return N < Decl->param_size() && Decl->getParamDecl(N) == &Node;
  4606. if (const auto *Decl = dyn_cast_or_null<ObjCMethodDecl>(Context))
  4607. return N < Decl->param_size() && Decl->getParamDecl(N) == &Node;
  4608. return false;
  4609. }
  4610. /// Matches any parameter of a function or an ObjC method declaration or a
  4611. /// block.
  4612. ///
  4613. /// Does not match the 'this' parameter of a method.
  4614. ///
  4615. /// Given
  4616. /// \code
  4617. /// class X { void f(int x, int y, int z) {} };
  4618. /// \endcode
  4619. /// cxxMethodDecl(hasAnyParameter(hasName("y")))
  4620. /// matches f(int x, int y, int z) {}
  4621. /// with hasAnyParameter(...)
  4622. /// matching int y
  4623. ///
  4624. /// For ObjectiveC, given
  4625. /// \code
  4626. /// @interface I - (void) f:(int) y; @end
  4627. /// \endcode
  4628. //
  4629. /// the matcher objcMethodDecl(hasAnyParameter(hasName("y")))
  4630. /// matches the declaration of method f with hasParameter
  4631. /// matching y.
  4632. ///
  4633. /// For blocks, given
  4634. /// \code
  4635. /// b = ^(int y) { printf("%d", y) };
  4636. /// \endcode
  4637. ///
  4638. /// the matcher blockDecl(hasAnyParameter(hasName("y")))
  4639. /// matches the declaration of the block b with hasParameter
  4640. /// matching y.
  4641. AST_POLYMORPHIC_MATCHER_P(hasAnyParameter,
  4642. AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl,
  4643. ObjCMethodDecl,
  4644. BlockDecl),
  4645. internal::Matcher<ParmVarDecl>,
  4646. InnerMatcher) {
  4647. return matchesFirstInPointerRange(InnerMatcher, Node.param_begin(),
  4648. Node.param_end(), Finder,
  4649. Builder) != Node.param_end();
  4650. }
  4651. /// Matches \c FunctionDecls and \c FunctionProtoTypes that have a
  4652. /// specific parameter count.
  4653. ///
  4654. /// Given
  4655. /// \code
  4656. /// void f(int i) {}
  4657. /// void g(int i, int j) {}
  4658. /// void h(int i, int j);
  4659. /// void j(int i);
  4660. /// void k(int x, int y, int z, ...);
  4661. /// \endcode
  4662. /// functionDecl(parameterCountIs(2))
  4663. /// matches \c g and \c h
  4664. /// functionProtoType(parameterCountIs(2))
  4665. /// matches \c g and \c h
  4666. /// functionProtoType(parameterCountIs(3))
  4667. /// matches \c k
  4668. AST_POLYMORPHIC_MATCHER_P(parameterCountIs,
  4669. AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl,
  4670. FunctionProtoType),
  4671. unsigned, N) {
  4672. return Node.getNumParams() == N;
  4673. }
  4674. /// Matches \c FunctionDecls that have a noreturn attribute.
  4675. ///
  4676. /// Given
  4677. /// \code
  4678. /// void nope();
  4679. /// [[noreturn]] void a();
  4680. /// __attribute__((noreturn)) void b();
  4681. /// struct c { [[noreturn]] c(); };
  4682. /// \endcode
  4683. /// functionDecl(isNoReturn())
  4684. /// matches all of those except
  4685. /// \code
  4686. /// void nope();
  4687. /// \endcode
  4688. AST_MATCHER(FunctionDecl, isNoReturn) { return Node.isNoReturn(); }
  4689. /// Matches the return type of a function declaration.
  4690. ///
  4691. /// Given:
  4692. /// \code
  4693. /// class X { int f() { return 1; } };
  4694. /// \endcode
  4695. /// cxxMethodDecl(returns(asString("int")))
  4696. /// matches int f() { return 1; }
  4697. AST_MATCHER_P(FunctionDecl, returns,
  4698. internal::Matcher<QualType>, InnerMatcher) {
  4699. return InnerMatcher.matches(Node.getReturnType(), Finder, Builder);
  4700. }
  4701. /// Matches extern "C" function or variable declarations.
  4702. ///
  4703. /// Given:
  4704. /// \code
  4705. /// extern "C" void f() {}
  4706. /// extern "C" { void g() {} }
  4707. /// void h() {}
  4708. /// extern "C" int x = 1;
  4709. /// extern "C" int y = 2;
  4710. /// int z = 3;
  4711. /// \endcode
  4712. /// functionDecl(isExternC())
  4713. /// matches the declaration of f and g, but not the declaration of h.
  4714. /// varDecl(isExternC())
  4715. /// matches the declaration of x and y, but not the declaration of z.
  4716. AST_POLYMORPHIC_MATCHER(isExternC, AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl,
  4717. VarDecl)) {
  4718. return Node.isExternC();
  4719. }
  4720. /// Matches variable/function declarations that have "static" storage
  4721. /// class specifier ("static" keyword) written in the source.
  4722. ///
  4723. /// Given:
  4724. /// \code
  4725. /// static void f() {}
  4726. /// static int i = 0;
  4727. /// extern int j;
  4728. /// int k;
  4729. /// \endcode
  4730. /// functionDecl(isStaticStorageClass())
  4731. /// matches the function declaration f.
  4732. /// varDecl(isStaticStorageClass())
  4733. /// matches the variable declaration i.
  4734. AST_POLYMORPHIC_MATCHER(isStaticStorageClass,
  4735. AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl,
  4736. VarDecl)) {
  4737. return Node.getStorageClass() == SC_Static;
  4738. }
  4739. /// Matches deleted function declarations.
  4740. ///
  4741. /// Given:
  4742. /// \code
  4743. /// void Func();
  4744. /// void DeletedFunc() = delete;
  4745. /// \endcode
  4746. /// functionDecl(isDeleted())
  4747. /// matches the declaration of DeletedFunc, but not Func.
  4748. AST_MATCHER(FunctionDecl, isDeleted) {
  4749. return Node.isDeleted();
  4750. }
  4751. /// Matches defaulted function declarations.
  4752. ///
  4753. /// Given:
  4754. /// \code
  4755. /// class A { ~A(); };
  4756. /// class B { ~B() = default; };
  4757. /// \endcode
  4758. /// functionDecl(isDefaulted())
  4759. /// matches the declaration of ~B, but not ~A.
  4760. AST_MATCHER(FunctionDecl, isDefaulted) {
  4761. return Node.isDefaulted();
  4762. }
  4763. /// Matches weak function declarations.
  4764. ///
  4765. /// Given:
  4766. /// \code
  4767. /// void foo() __attribute__((__weakref__("__foo")));
  4768. /// void bar();
  4769. /// \endcode
  4770. /// functionDecl(isWeak())
  4771. /// matches the weak declaration "foo", but not "bar".
  4772. AST_MATCHER(FunctionDecl, isWeak) { return Node.isWeak(); }
  4773. /// Matches functions that have a dynamic exception specification.
  4774. ///
  4775. /// Given:
  4776. /// \code
  4777. /// void f();
  4778. /// void g() noexcept;
  4779. /// void h() noexcept(true);
  4780. /// void i() noexcept(false);
  4781. /// void j() throw();
  4782. /// void k() throw(int);
  4783. /// void l() throw(...);
  4784. /// \endcode
  4785. /// functionDecl(hasDynamicExceptionSpec()) and
  4786. /// functionProtoType(hasDynamicExceptionSpec())
  4787. /// match the declarations of j, k, and l, but not f, g, h, or i.
  4788. AST_POLYMORPHIC_MATCHER(hasDynamicExceptionSpec,
  4789. AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl,
  4790. FunctionProtoType)) {
  4791. if (const FunctionProtoType *FnTy = internal::getFunctionProtoType(Node))
  4792. return FnTy->hasDynamicExceptionSpec();
  4793. return false;
  4794. }
  4795. /// Matches functions that have a non-throwing exception specification.
  4796. ///
  4797. /// Given:
  4798. /// \code
  4799. /// void f();
  4800. /// void g() noexcept;
  4801. /// void h() throw();
  4802. /// void i() throw(int);
  4803. /// void j() noexcept(false);
  4804. /// \endcode
  4805. /// functionDecl(isNoThrow()) and functionProtoType(isNoThrow())
  4806. /// match the declarations of g, and h, but not f, i or j.
  4807. AST_POLYMORPHIC_MATCHER(isNoThrow,
  4808. AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl,
  4809. FunctionProtoType)) {
  4810. const FunctionProtoType *FnTy = internal::getFunctionProtoType(Node);
  4811. // If the function does not have a prototype, then it is assumed to be a
  4812. // throwing function (as it would if the function did not have any exception
  4813. // specification).
  4814. if (!FnTy)
  4815. return false;
  4816. // Assume the best for any unresolved exception specification.
  4817. if (isUnresolvedExceptionSpec(FnTy->getExceptionSpecType()))
  4818. return true;
  4819. return FnTy->isNothrow();
  4820. }
  4821. /// Matches consteval function declarations and if consteval/if ! consteval
  4822. /// statements.
  4823. ///
  4824. /// Given:
  4825. /// \code
  4826. /// consteval int a();
  4827. /// void b() { if consteval {} }
  4828. /// void c() { if ! consteval {} }
  4829. /// void d() { if ! consteval {} else {} }
  4830. /// \endcode
  4831. /// functionDecl(isConsteval())
  4832. /// matches the declaration of "int a()".
  4833. /// ifStmt(isConsteval())
  4834. /// matches the if statement in "void b()", "void c()", "void d()".
  4835. AST_POLYMORPHIC_MATCHER(isConsteval,
  4836. AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, IfStmt)) {
  4837. return Node.isConsteval();
  4838. }
  4839. /// Matches constexpr variable and function declarations,
  4840. /// and if constexpr.
  4841. ///
  4842. /// Given:
  4843. /// \code
  4844. /// constexpr int foo = 42;
  4845. /// constexpr int bar();
  4846. /// void baz() { if constexpr(1 > 0) {} }
  4847. /// \endcode
  4848. /// varDecl(isConstexpr())
  4849. /// matches the declaration of foo.
  4850. /// functionDecl(isConstexpr())
  4851. /// matches the declaration of bar.
  4852. /// ifStmt(isConstexpr())
  4853. /// matches the if statement in baz.
  4854. AST_POLYMORPHIC_MATCHER(isConstexpr,
  4855. AST_POLYMORPHIC_SUPPORTED_TYPES(VarDecl,
  4856. FunctionDecl,
  4857. IfStmt)) {
  4858. return Node.isConstexpr();
  4859. }
  4860. /// Matches constinit variable declarations.
  4861. ///
  4862. /// Given:
  4863. /// \code
  4864. /// constinit int foo = 42;
  4865. /// constinit const char* bar = "bar";
  4866. /// int baz = 42;
  4867. /// [[clang::require_constant_initialization]] int xyz = 42;
  4868. /// \endcode
  4869. /// varDecl(isConstinit())
  4870. /// matches the declaration of `foo` and `bar`, but not `baz` and `xyz`.
  4871. AST_MATCHER(VarDecl, isConstinit) {
  4872. if (const auto *CIA = Node.getAttr<ConstInitAttr>())
  4873. return CIA->isConstinit();
  4874. return false;
  4875. }
  4876. /// Matches selection statements with initializer.
  4877. ///
  4878. /// Given:
  4879. /// \code
  4880. /// void foo() {
  4881. /// if (int i = foobar(); i > 0) {}
  4882. /// switch (int i = foobar(); i) {}
  4883. /// for (auto& a = get_range(); auto& x : a) {}
  4884. /// }
  4885. /// void bar() {
  4886. /// if (foobar() > 0) {}
  4887. /// switch (foobar()) {}
  4888. /// for (auto& x : get_range()) {}
  4889. /// }
  4890. /// \endcode
  4891. /// ifStmt(hasInitStatement(anything()))
  4892. /// matches the if statement in foo but not in bar.
  4893. /// switchStmt(hasInitStatement(anything()))
  4894. /// matches the switch statement in foo but not in bar.
  4895. /// cxxForRangeStmt(hasInitStatement(anything()))
  4896. /// matches the range for statement in foo but not in bar.
  4897. AST_POLYMORPHIC_MATCHER_P(hasInitStatement,
  4898. AST_POLYMORPHIC_SUPPORTED_TYPES(IfStmt, SwitchStmt,
  4899. CXXForRangeStmt),
  4900. internal::Matcher<Stmt>, InnerMatcher) {
  4901. const Stmt *Init = Node.getInit();
  4902. return Init != nullptr && InnerMatcher.matches(*Init, Finder, Builder);
  4903. }
  4904. /// Matches the condition expression of an if statement, for loop,
  4905. /// switch statement or conditional operator.
  4906. ///
  4907. /// Example matches true (matcher = hasCondition(cxxBoolLiteral(equals(true))))
  4908. /// \code
  4909. /// if (true) {}
  4910. /// \endcode
  4911. AST_POLYMORPHIC_MATCHER_P(
  4912. hasCondition,
  4913. AST_POLYMORPHIC_SUPPORTED_TYPES(IfStmt, ForStmt, WhileStmt, DoStmt,
  4914. SwitchStmt, AbstractConditionalOperator),
  4915. internal::Matcher<Expr>, InnerMatcher) {
  4916. const Expr *const Condition = Node.getCond();
  4917. return (Condition != nullptr &&
  4918. InnerMatcher.matches(*Condition, Finder, Builder));
  4919. }
  4920. /// Matches the then-statement of an if statement.
  4921. ///
  4922. /// Examples matches the if statement
  4923. /// (matcher = ifStmt(hasThen(cxxBoolLiteral(equals(true)))))
  4924. /// \code
  4925. /// if (false) true; else false;
  4926. /// \endcode
  4927. AST_MATCHER_P(IfStmt, hasThen, internal::Matcher<Stmt>, InnerMatcher) {
  4928. const Stmt *const Then = Node.getThen();
  4929. return (Then != nullptr && InnerMatcher.matches(*Then, Finder, Builder));
  4930. }
  4931. /// Matches the else-statement of an if statement.
  4932. ///
  4933. /// Examples matches the if statement
  4934. /// (matcher = ifStmt(hasElse(cxxBoolLiteral(equals(true)))))
  4935. /// \code
  4936. /// if (false) false; else true;
  4937. /// \endcode
  4938. AST_MATCHER_P(IfStmt, hasElse, internal::Matcher<Stmt>, InnerMatcher) {
  4939. const Stmt *const Else = Node.getElse();
  4940. return (Else != nullptr && InnerMatcher.matches(*Else, Finder, Builder));
  4941. }
  4942. /// Matches if a node equals a previously bound node.
  4943. ///
  4944. /// Matches a node if it equals the node previously bound to \p ID.
  4945. ///
  4946. /// Given
  4947. /// \code
  4948. /// class X { int a; int b; };
  4949. /// \endcode
  4950. /// cxxRecordDecl(
  4951. /// has(fieldDecl(hasName("a"), hasType(type().bind("t")))),
  4952. /// has(fieldDecl(hasName("b"), hasType(type(equalsBoundNode("t"))))))
  4953. /// matches the class \c X, as \c a and \c b have the same type.
  4954. ///
  4955. /// Note that when multiple matches are involved via \c forEach* matchers,
  4956. /// \c equalsBoundNodes acts as a filter.
  4957. /// For example:
  4958. /// compoundStmt(
  4959. /// forEachDescendant(varDecl().bind("d")),
  4960. /// forEachDescendant(declRefExpr(to(decl(equalsBoundNode("d"))))))
  4961. /// will trigger a match for each combination of variable declaration
  4962. /// and reference to that variable declaration within a compound statement.
  4963. AST_POLYMORPHIC_MATCHER_P(equalsBoundNode,
  4964. AST_POLYMORPHIC_SUPPORTED_TYPES(Stmt, Decl, Type,
  4965. QualType),
  4966. std::string, ID) {
  4967. // FIXME: Figure out whether it makes sense to allow this
  4968. // on any other node types.
  4969. // For *Loc it probably does not make sense, as those seem
  4970. // unique. For NestedNameSepcifier it might make sense, as
  4971. // those also have pointer identity, but I'm not sure whether
  4972. // they're ever reused.
  4973. internal::NotEqualsBoundNodePredicate Predicate;
  4974. Predicate.ID = ID;
  4975. Predicate.Node = DynTypedNode::create(Node);
  4976. return Builder->removeBindings(Predicate);
  4977. }
  4978. /// Matches the condition variable statement in an if statement.
  4979. ///
  4980. /// Given
  4981. /// \code
  4982. /// if (A* a = GetAPointer()) {}
  4983. /// \endcode
  4984. /// hasConditionVariableStatement(...)
  4985. /// matches 'A* a = GetAPointer()'.
  4986. AST_MATCHER_P(IfStmt, hasConditionVariableStatement,
  4987. internal::Matcher<DeclStmt>, InnerMatcher) {
  4988. const DeclStmt* const DeclarationStatement =
  4989. Node.getConditionVariableDeclStmt();
  4990. return DeclarationStatement != nullptr &&
  4991. InnerMatcher.matches(*DeclarationStatement, Finder, Builder);
  4992. }
  4993. /// Matches the index expression of an array subscript expression.
  4994. ///
  4995. /// Given
  4996. /// \code
  4997. /// int i[5];
  4998. /// void f() { i[1] = 42; }
  4999. /// \endcode
  5000. /// arraySubscriptExpression(hasIndex(integerLiteral()))
  5001. /// matches \c i[1] with the \c integerLiteral() matching \c 1
  5002. AST_MATCHER_P(ArraySubscriptExpr, hasIndex,
  5003. internal::Matcher<Expr>, InnerMatcher) {
  5004. if (const Expr* Expression = Node.getIdx())
  5005. return InnerMatcher.matches(*Expression, Finder, Builder);
  5006. return false;
  5007. }
  5008. /// Matches the base expression of an array subscript expression.
  5009. ///
  5010. /// Given
  5011. /// \code
  5012. /// int i[5];
  5013. /// void f() { i[1] = 42; }
  5014. /// \endcode
  5015. /// arraySubscriptExpression(hasBase(implicitCastExpr(
  5016. /// hasSourceExpression(declRefExpr()))))
  5017. /// matches \c i[1] with the \c declRefExpr() matching \c i
  5018. AST_MATCHER_P(ArraySubscriptExpr, hasBase,
  5019. internal::Matcher<Expr>, InnerMatcher) {
  5020. if (const Expr* Expression = Node.getBase())
  5021. return InnerMatcher.matches(*Expression, Finder, Builder);
  5022. return false;
  5023. }
  5024. /// Matches a 'for', 'while', 'do while' statement or a function
  5025. /// definition that has a given body. Note that in case of functions
  5026. /// this matcher only matches the definition itself and not the other
  5027. /// declarations of the same function.
  5028. ///
  5029. /// Given
  5030. /// \code
  5031. /// for (;;) {}
  5032. /// \endcode
  5033. /// hasBody(compoundStmt())
  5034. /// matches 'for (;;) {}'
  5035. /// with compoundStmt()
  5036. /// matching '{}'
  5037. ///
  5038. /// Given
  5039. /// \code
  5040. /// void f();
  5041. /// void f() {}
  5042. /// \endcode
  5043. /// hasBody(functionDecl())
  5044. /// matches 'void f() {}'
  5045. /// with compoundStmt()
  5046. /// matching '{}'
  5047. /// but does not match 'void f();'
  5048. AST_POLYMORPHIC_MATCHER_P(hasBody,
  5049. AST_POLYMORPHIC_SUPPORTED_TYPES(DoStmt, ForStmt,
  5050. WhileStmt,
  5051. CXXForRangeStmt,
  5052. FunctionDecl),
  5053. internal::Matcher<Stmt>, InnerMatcher) {
  5054. if (Finder->isTraversalIgnoringImplicitNodes() && isDefaultedHelper(&Node))
  5055. return false;
  5056. const Stmt *const Statement = internal::GetBodyMatcher<NodeType>::get(Node);
  5057. return (Statement != nullptr &&
  5058. InnerMatcher.matches(*Statement, Finder, Builder));
  5059. }
  5060. /// Matches a function declaration that has a given body present in the AST.
  5061. /// Note that this matcher matches all the declarations of a function whose
  5062. /// body is present in the AST.
  5063. ///
  5064. /// Given
  5065. /// \code
  5066. /// void f();
  5067. /// void f() {}
  5068. /// void g();
  5069. /// \endcode
  5070. /// functionDecl(hasAnyBody(compoundStmt()))
  5071. /// matches both 'void f();'
  5072. /// and 'void f() {}'
  5073. /// with compoundStmt()
  5074. /// matching '{}'
  5075. /// but does not match 'void g();'
  5076. AST_MATCHER_P(FunctionDecl, hasAnyBody,
  5077. internal::Matcher<Stmt>, InnerMatcher) {
  5078. const Stmt *const Statement = Node.getBody();
  5079. return (Statement != nullptr &&
  5080. InnerMatcher.matches(*Statement, Finder, Builder));
  5081. }
  5082. /// Matches compound statements where at least one substatement matches
  5083. /// a given matcher. Also matches StmtExprs that have CompoundStmt as children.
  5084. ///
  5085. /// Given
  5086. /// \code
  5087. /// { {}; 1+2; }
  5088. /// \endcode
  5089. /// hasAnySubstatement(compoundStmt())
  5090. /// matches '{ {}; 1+2; }'
  5091. /// with compoundStmt()
  5092. /// matching '{}'
  5093. AST_POLYMORPHIC_MATCHER_P(hasAnySubstatement,
  5094. AST_POLYMORPHIC_SUPPORTED_TYPES(CompoundStmt,
  5095. StmtExpr),
  5096. internal::Matcher<Stmt>, InnerMatcher) {
  5097. const CompoundStmt *CS = CompoundStmtMatcher<NodeType>::get(Node);
  5098. return CS && matchesFirstInPointerRange(InnerMatcher, CS->body_begin(),
  5099. CS->body_end(), Finder,
  5100. Builder) != CS->body_end();
  5101. }
  5102. /// Checks that a compound statement contains a specific number of
  5103. /// child statements.
  5104. ///
  5105. /// Example: Given
  5106. /// \code
  5107. /// { for (;;) {} }
  5108. /// \endcode
  5109. /// compoundStmt(statementCountIs(0)))
  5110. /// matches '{}'
  5111. /// but does not match the outer compound statement.
  5112. AST_MATCHER_P(CompoundStmt, statementCountIs, unsigned, N) {
  5113. return Node.size() == N;
  5114. }
  5115. /// Matches literals that are equal to the given value of type ValueT.
  5116. ///
  5117. /// Given
  5118. /// \code
  5119. /// f('\0', false, 3.14, 42);
  5120. /// \endcode
  5121. /// characterLiteral(equals(0))
  5122. /// matches '\0'
  5123. /// cxxBoolLiteral(equals(false)) and cxxBoolLiteral(equals(0))
  5124. /// match false
  5125. /// floatLiteral(equals(3.14)) and floatLiteral(equals(314e-2))
  5126. /// match 3.14
  5127. /// integerLiteral(equals(42))
  5128. /// matches 42
  5129. ///
  5130. /// Note that you cannot directly match a negative numeric literal because the
  5131. /// minus sign is not part of the literal: It is a unary operator whose operand
  5132. /// is the positive numeric literal. Instead, you must use a unaryOperator()
  5133. /// matcher to match the minus sign:
  5134. ///
  5135. /// unaryOperator(hasOperatorName("-"),
  5136. /// hasUnaryOperand(integerLiteral(equals(13))))
  5137. ///
  5138. /// Usable as: Matcher<CharacterLiteral>, Matcher<CXXBoolLiteralExpr>,
  5139. /// Matcher<FloatingLiteral>, Matcher<IntegerLiteral>
  5140. template <typename ValueT>
  5141. internal::PolymorphicMatcher<internal::ValueEqualsMatcher,
  5142. void(internal::AllNodeBaseTypes), ValueT>
  5143. equals(const ValueT &Value) {
  5144. return internal::PolymorphicMatcher<internal::ValueEqualsMatcher,
  5145. void(internal::AllNodeBaseTypes), ValueT>(
  5146. Value);
  5147. }
  5148. AST_POLYMORPHIC_MATCHER_P_OVERLOAD(equals,
  5149. AST_POLYMORPHIC_SUPPORTED_TYPES(CharacterLiteral,
  5150. CXXBoolLiteralExpr,
  5151. IntegerLiteral),
  5152. bool, Value, 0) {
  5153. return internal::ValueEqualsMatcher<NodeType, ParamT>(Value)
  5154. .matchesNode(Node);
  5155. }
  5156. AST_POLYMORPHIC_MATCHER_P_OVERLOAD(equals,
  5157. AST_POLYMORPHIC_SUPPORTED_TYPES(CharacterLiteral,
  5158. CXXBoolLiteralExpr,
  5159. IntegerLiteral),
  5160. unsigned, Value, 1) {
  5161. return internal::ValueEqualsMatcher<NodeType, ParamT>(Value)
  5162. .matchesNode(Node);
  5163. }
  5164. AST_POLYMORPHIC_MATCHER_P_OVERLOAD(equals,
  5165. AST_POLYMORPHIC_SUPPORTED_TYPES(CharacterLiteral,
  5166. CXXBoolLiteralExpr,
  5167. FloatingLiteral,
  5168. IntegerLiteral),
  5169. double, Value, 2) {
  5170. return internal::ValueEqualsMatcher<NodeType, ParamT>(Value)
  5171. .matchesNode(Node);
  5172. }
  5173. /// Matches the operator Name of operator expressions (binary or
  5174. /// unary).
  5175. ///
  5176. /// Example matches a || b (matcher = binaryOperator(hasOperatorName("||")))
  5177. /// \code
  5178. /// !(a || b)
  5179. /// \endcode
  5180. AST_POLYMORPHIC_MATCHER_P(
  5181. hasOperatorName,
  5182. AST_POLYMORPHIC_SUPPORTED_TYPES(BinaryOperator, CXXOperatorCallExpr,
  5183. CXXRewrittenBinaryOperator, UnaryOperator),
  5184. std::string, Name) {
  5185. if (Optional<StringRef> OpName = internal::getOpName(Node))
  5186. return *OpName == Name;
  5187. return false;
  5188. }
  5189. /// Matches operator expressions (binary or unary) that have any of the
  5190. /// specified names.
  5191. ///
  5192. /// hasAnyOperatorName("+", "-")
  5193. /// Is equivalent to
  5194. /// anyOf(hasOperatorName("+"), hasOperatorName("-"))
  5195. extern const internal::VariadicFunction<
  5196. internal::PolymorphicMatcher<internal::HasAnyOperatorNameMatcher,
  5197. AST_POLYMORPHIC_SUPPORTED_TYPES(
  5198. BinaryOperator, CXXOperatorCallExpr,
  5199. CXXRewrittenBinaryOperator, UnaryOperator),
  5200. std::vector<std::string>>,
  5201. StringRef, internal::hasAnyOperatorNameFunc>
  5202. hasAnyOperatorName;
  5203. /// Matches all kinds of assignment operators.
  5204. ///
  5205. /// Example 1: matches a += b (matcher = binaryOperator(isAssignmentOperator()))
  5206. /// \code
  5207. /// if (a == b)
  5208. /// a += b;
  5209. /// \endcode
  5210. ///
  5211. /// Example 2: matches s1 = s2
  5212. /// (matcher = cxxOperatorCallExpr(isAssignmentOperator()))
  5213. /// \code
  5214. /// struct S { S& operator=(const S&); };
  5215. /// void x() { S s1, s2; s1 = s2; }
  5216. /// \endcode
  5217. AST_POLYMORPHIC_MATCHER(
  5218. isAssignmentOperator,
  5219. AST_POLYMORPHIC_SUPPORTED_TYPES(BinaryOperator, CXXOperatorCallExpr,
  5220. CXXRewrittenBinaryOperator)) {
  5221. return Node.isAssignmentOp();
  5222. }
  5223. /// Matches comparison operators.
  5224. ///
  5225. /// Example 1: matches a == b (matcher = binaryOperator(isComparisonOperator()))
  5226. /// \code
  5227. /// if (a == b)
  5228. /// a += b;
  5229. /// \endcode
  5230. ///
  5231. /// Example 2: matches s1 < s2
  5232. /// (matcher = cxxOperatorCallExpr(isComparisonOperator()))
  5233. /// \code
  5234. /// struct S { bool operator<(const S& other); };
  5235. /// void x(S s1, S s2) { bool b1 = s1 < s2; }
  5236. /// \endcode
  5237. AST_POLYMORPHIC_MATCHER(
  5238. isComparisonOperator,
  5239. AST_POLYMORPHIC_SUPPORTED_TYPES(BinaryOperator, CXXOperatorCallExpr,
  5240. CXXRewrittenBinaryOperator)) {
  5241. return Node.isComparisonOp();
  5242. }
  5243. /// Matches the left hand side of binary operator expressions.
  5244. ///
  5245. /// Example matches a (matcher = binaryOperator(hasLHS()))
  5246. /// \code
  5247. /// a || b
  5248. /// \endcode
  5249. AST_POLYMORPHIC_MATCHER_P(hasLHS,
  5250. AST_POLYMORPHIC_SUPPORTED_TYPES(
  5251. BinaryOperator, CXXOperatorCallExpr,
  5252. CXXRewrittenBinaryOperator, ArraySubscriptExpr),
  5253. internal::Matcher<Expr>, InnerMatcher) {
  5254. const Expr *LeftHandSide = internal::getLHS(Node);
  5255. return (LeftHandSide != nullptr &&
  5256. InnerMatcher.matches(*LeftHandSide, Finder, Builder));
  5257. }
  5258. /// Matches the right hand side of binary operator expressions.
  5259. ///
  5260. /// Example matches b (matcher = binaryOperator(hasRHS()))
  5261. /// \code
  5262. /// a || b
  5263. /// \endcode
  5264. AST_POLYMORPHIC_MATCHER_P(hasRHS,
  5265. AST_POLYMORPHIC_SUPPORTED_TYPES(
  5266. BinaryOperator, CXXOperatorCallExpr,
  5267. CXXRewrittenBinaryOperator, ArraySubscriptExpr),
  5268. internal::Matcher<Expr>, InnerMatcher) {
  5269. const Expr *RightHandSide = internal::getRHS(Node);
  5270. return (RightHandSide != nullptr &&
  5271. InnerMatcher.matches(*RightHandSide, Finder, Builder));
  5272. }
  5273. /// Matches if either the left hand side or the right hand side of a
  5274. /// binary operator matches.
  5275. AST_POLYMORPHIC_MATCHER_P(
  5276. hasEitherOperand,
  5277. AST_POLYMORPHIC_SUPPORTED_TYPES(BinaryOperator, CXXOperatorCallExpr,
  5278. CXXRewrittenBinaryOperator),
  5279. internal::Matcher<Expr>, InnerMatcher) {
  5280. return internal::VariadicDynCastAllOfMatcher<Stmt, NodeType>()(
  5281. anyOf(hasLHS(InnerMatcher), hasRHS(InnerMatcher)))
  5282. .matches(Node, Finder, Builder);
  5283. }
  5284. /// Matches if both matchers match with opposite sides of the binary operator.
  5285. ///
  5286. /// Example matcher = binaryOperator(hasOperands(integerLiteral(equals(1),
  5287. /// integerLiteral(equals(2)))
  5288. /// \code
  5289. /// 1 + 2 // Match
  5290. /// 2 + 1 // Match
  5291. /// 1 + 1 // No match
  5292. /// 2 + 2 // No match
  5293. /// \endcode
  5294. AST_POLYMORPHIC_MATCHER_P2(
  5295. hasOperands,
  5296. AST_POLYMORPHIC_SUPPORTED_TYPES(BinaryOperator, CXXOperatorCallExpr,
  5297. CXXRewrittenBinaryOperator),
  5298. internal::Matcher<Expr>, Matcher1, internal::Matcher<Expr>, Matcher2) {
  5299. return internal::VariadicDynCastAllOfMatcher<Stmt, NodeType>()(
  5300. anyOf(allOf(hasLHS(Matcher1), hasRHS(Matcher2)),
  5301. allOf(hasLHS(Matcher2), hasRHS(Matcher1))))
  5302. .matches(Node, Finder, Builder);
  5303. }
  5304. /// Matches if the operand of a unary operator matches.
  5305. ///
  5306. /// Example matches true (matcher = hasUnaryOperand(
  5307. /// cxxBoolLiteral(equals(true))))
  5308. /// \code
  5309. /// !true
  5310. /// \endcode
  5311. AST_POLYMORPHIC_MATCHER_P(hasUnaryOperand,
  5312. AST_POLYMORPHIC_SUPPORTED_TYPES(UnaryOperator,
  5313. CXXOperatorCallExpr),
  5314. internal::Matcher<Expr>, InnerMatcher) {
  5315. const Expr *const Operand = internal::getSubExpr(Node);
  5316. return (Operand != nullptr &&
  5317. InnerMatcher.matches(*Operand, Finder, Builder));
  5318. }
  5319. /// Matches if the cast's source expression
  5320. /// or opaque value's source expression matches the given matcher.
  5321. ///
  5322. /// Example 1: matches "a string"
  5323. /// (matcher = castExpr(hasSourceExpression(cxxConstructExpr())))
  5324. /// \code
  5325. /// class URL { URL(string); };
  5326. /// URL url = "a string";
  5327. /// \endcode
  5328. ///
  5329. /// Example 2: matches 'b' (matcher =
  5330. /// opaqueValueExpr(hasSourceExpression(implicitCastExpr(declRefExpr())))
  5331. /// \code
  5332. /// int a = b ?: 1;
  5333. /// \endcode
  5334. AST_POLYMORPHIC_MATCHER_P(hasSourceExpression,
  5335. AST_POLYMORPHIC_SUPPORTED_TYPES(CastExpr,
  5336. OpaqueValueExpr),
  5337. internal::Matcher<Expr>, InnerMatcher) {
  5338. const Expr *const SubExpression =
  5339. internal::GetSourceExpressionMatcher<NodeType>::get(Node);
  5340. return (SubExpression != nullptr &&
  5341. InnerMatcher.matches(*SubExpression, Finder, Builder));
  5342. }
  5343. /// Matches casts that has a given cast kind.
  5344. ///
  5345. /// Example: matches the implicit cast around \c 0
  5346. /// (matcher = castExpr(hasCastKind(CK_NullToPointer)))
  5347. /// \code
  5348. /// int *p = 0;
  5349. /// \endcode
  5350. ///
  5351. /// If the matcher is use from clang-query, CastKind parameter
  5352. /// should be passed as a quoted string. e.g., hasCastKind("CK_NullToPointer").
  5353. AST_MATCHER_P(CastExpr, hasCastKind, CastKind, Kind) {
  5354. return Node.getCastKind() == Kind;
  5355. }
  5356. /// Matches casts whose destination type matches a given matcher.
  5357. ///
  5358. /// (Note: Clang's AST refers to other conversions as "casts" too, and calls
  5359. /// actual casts "explicit" casts.)
  5360. AST_MATCHER_P(ExplicitCastExpr, hasDestinationType,
  5361. internal::Matcher<QualType>, InnerMatcher) {
  5362. const QualType NodeType = Node.getTypeAsWritten();
  5363. return InnerMatcher.matches(NodeType, Finder, Builder);
  5364. }
  5365. /// Matches implicit casts whose destination type matches a given
  5366. /// matcher.
  5367. ///
  5368. /// FIXME: Unit test this matcher
  5369. AST_MATCHER_P(ImplicitCastExpr, hasImplicitDestinationType,
  5370. internal::Matcher<QualType>, InnerMatcher) {
  5371. return InnerMatcher.matches(Node.getType(), Finder, Builder);
  5372. }
  5373. /// Matches TagDecl object that are spelled with "struct."
  5374. ///
  5375. /// Example matches S, but not C, U or E.
  5376. /// \code
  5377. /// struct S {};
  5378. /// class C {};
  5379. /// union U {};
  5380. /// enum E {};
  5381. /// \endcode
  5382. AST_MATCHER(TagDecl, isStruct) {
  5383. return Node.isStruct();
  5384. }
  5385. /// Matches TagDecl object that are spelled with "union."
  5386. ///
  5387. /// Example matches U, but not C, S or E.
  5388. /// \code
  5389. /// struct S {};
  5390. /// class C {};
  5391. /// union U {};
  5392. /// enum E {};
  5393. /// \endcode
  5394. AST_MATCHER(TagDecl, isUnion) {
  5395. return Node.isUnion();
  5396. }
  5397. /// Matches TagDecl object that are spelled with "class."
  5398. ///
  5399. /// Example matches C, but not S, U or E.
  5400. /// \code
  5401. /// struct S {};
  5402. /// class C {};
  5403. /// union U {};
  5404. /// enum E {};
  5405. /// \endcode
  5406. AST_MATCHER(TagDecl, isClass) {
  5407. return Node.isClass();
  5408. }
  5409. /// Matches TagDecl object that are spelled with "enum."
  5410. ///
  5411. /// Example matches E, but not C, S or U.
  5412. /// \code
  5413. /// struct S {};
  5414. /// class C {};
  5415. /// union U {};
  5416. /// enum E {};
  5417. /// \endcode
  5418. AST_MATCHER(TagDecl, isEnum) {
  5419. return Node.isEnum();
  5420. }
  5421. /// Matches the true branch expression of a conditional operator.
  5422. ///
  5423. /// Example 1 (conditional ternary operator): matches a
  5424. /// \code
  5425. /// condition ? a : b
  5426. /// \endcode
  5427. ///
  5428. /// Example 2 (conditional binary operator): matches opaqueValueExpr(condition)
  5429. /// \code
  5430. /// condition ?: b
  5431. /// \endcode
  5432. AST_MATCHER_P(AbstractConditionalOperator, hasTrueExpression,
  5433. internal::Matcher<Expr>, InnerMatcher) {
  5434. const Expr *Expression = Node.getTrueExpr();
  5435. return (Expression != nullptr &&
  5436. InnerMatcher.matches(*Expression, Finder, Builder));
  5437. }
  5438. /// Matches the false branch expression of a conditional operator
  5439. /// (binary or ternary).
  5440. ///
  5441. /// Example matches b
  5442. /// \code
  5443. /// condition ? a : b
  5444. /// condition ?: b
  5445. /// \endcode
  5446. AST_MATCHER_P(AbstractConditionalOperator, hasFalseExpression,
  5447. internal::Matcher<Expr>, InnerMatcher) {
  5448. const Expr *Expression = Node.getFalseExpr();
  5449. return (Expression != nullptr &&
  5450. InnerMatcher.matches(*Expression, Finder, Builder));
  5451. }
  5452. /// Matches if a declaration has a body attached.
  5453. ///
  5454. /// Example matches A, va, fa
  5455. /// \code
  5456. /// class A {};
  5457. /// class B; // Doesn't match, as it has no body.
  5458. /// int va;
  5459. /// extern int vb; // Doesn't match, as it doesn't define the variable.
  5460. /// void fa() {}
  5461. /// void fb(); // Doesn't match, as it has no body.
  5462. /// @interface X
  5463. /// - (void)ma; // Doesn't match, interface is declaration.
  5464. /// @end
  5465. /// @implementation X
  5466. /// - (void)ma {}
  5467. /// @end
  5468. /// \endcode
  5469. ///
  5470. /// Usable as: Matcher<TagDecl>, Matcher<VarDecl>, Matcher<FunctionDecl>,
  5471. /// Matcher<ObjCMethodDecl>
  5472. AST_POLYMORPHIC_MATCHER(isDefinition,
  5473. AST_POLYMORPHIC_SUPPORTED_TYPES(TagDecl, VarDecl,
  5474. ObjCMethodDecl,
  5475. FunctionDecl)) {
  5476. return Node.isThisDeclarationADefinition();
  5477. }
  5478. /// Matches if a function declaration is variadic.
  5479. ///
  5480. /// Example matches f, but not g or h. The function i will not match, even when
  5481. /// compiled in C mode.
  5482. /// \code
  5483. /// void f(...);
  5484. /// void g(int);
  5485. /// template <typename... Ts> void h(Ts...);
  5486. /// void i();
  5487. /// \endcode
  5488. AST_MATCHER(FunctionDecl, isVariadic) {
  5489. return Node.isVariadic();
  5490. }
  5491. /// Matches the class declaration that the given method declaration
  5492. /// belongs to.
  5493. ///
  5494. /// FIXME: Generalize this for other kinds of declarations.
  5495. /// FIXME: What other kind of declarations would we need to generalize
  5496. /// this to?
  5497. ///
  5498. /// Example matches A() in the last line
  5499. /// (matcher = cxxConstructExpr(hasDeclaration(cxxMethodDecl(
  5500. /// ofClass(hasName("A"))))))
  5501. /// \code
  5502. /// class A {
  5503. /// public:
  5504. /// A();
  5505. /// };
  5506. /// A a = A();
  5507. /// \endcode
  5508. AST_MATCHER_P(CXXMethodDecl, ofClass,
  5509. internal::Matcher<CXXRecordDecl>, InnerMatcher) {
  5510. ASTChildrenNotSpelledInSourceScope RAII(Finder, false);
  5511. const CXXRecordDecl *Parent = Node.getParent();
  5512. return (Parent != nullptr &&
  5513. InnerMatcher.matches(*Parent, Finder, Builder));
  5514. }
  5515. /// Matches each method overridden by the given method. This matcher may
  5516. /// produce multiple matches.
  5517. ///
  5518. /// Given
  5519. /// \code
  5520. /// class A { virtual void f(); };
  5521. /// class B : public A { void f(); };
  5522. /// class C : public B { void f(); };
  5523. /// \endcode
  5524. /// cxxMethodDecl(ofClass(hasName("C")),
  5525. /// forEachOverridden(cxxMethodDecl().bind("b"))).bind("d")
  5526. /// matches once, with "b" binding "A::f" and "d" binding "C::f" (Note
  5527. /// that B::f is not overridden by C::f).
  5528. ///
  5529. /// The check can produce multiple matches in case of multiple inheritance, e.g.
  5530. /// \code
  5531. /// class A1 { virtual void f(); };
  5532. /// class A2 { virtual void f(); };
  5533. /// class C : public A1, public A2 { void f(); };
  5534. /// \endcode
  5535. /// cxxMethodDecl(ofClass(hasName("C")),
  5536. /// forEachOverridden(cxxMethodDecl().bind("b"))).bind("d")
  5537. /// matches twice, once with "b" binding "A1::f" and "d" binding "C::f", and
  5538. /// once with "b" binding "A2::f" and "d" binding "C::f".
  5539. AST_MATCHER_P(CXXMethodDecl, forEachOverridden,
  5540. internal::Matcher<CXXMethodDecl>, InnerMatcher) {
  5541. BoundNodesTreeBuilder Result;
  5542. bool Matched = false;
  5543. for (const auto *Overridden : Node.overridden_methods()) {
  5544. BoundNodesTreeBuilder OverriddenBuilder(*Builder);
  5545. const bool OverriddenMatched =
  5546. InnerMatcher.matches(*Overridden, Finder, &OverriddenBuilder);
  5547. if (OverriddenMatched) {
  5548. Matched = true;
  5549. Result.addMatch(OverriddenBuilder);
  5550. }
  5551. }
  5552. *Builder = std::move(Result);
  5553. return Matched;
  5554. }
  5555. /// Matches declarations of virtual methods and C++ base specifers that specify
  5556. /// virtual inheritance.
  5557. ///
  5558. /// Example:
  5559. /// \code
  5560. /// class A {
  5561. /// public:
  5562. /// virtual void x(); // matches x
  5563. /// };
  5564. /// \endcode
  5565. ///
  5566. /// Example:
  5567. /// \code
  5568. /// class Base {};
  5569. /// class DirectlyDerived : virtual Base {}; // matches Base
  5570. /// class IndirectlyDerived : DirectlyDerived, Base {}; // matches Base
  5571. /// \endcode
  5572. ///
  5573. /// Usable as: Matcher<CXXMethodDecl>, Matcher<CXXBaseSpecifier>
  5574. AST_POLYMORPHIC_MATCHER(isVirtual,
  5575. AST_POLYMORPHIC_SUPPORTED_TYPES(CXXMethodDecl,
  5576. CXXBaseSpecifier)) {
  5577. return Node.isVirtual();
  5578. }
  5579. /// Matches if the given method declaration has an explicit "virtual".
  5580. ///
  5581. /// Given
  5582. /// \code
  5583. /// class A {
  5584. /// public:
  5585. /// virtual void x();
  5586. /// };
  5587. /// class B : public A {
  5588. /// public:
  5589. /// void x();
  5590. /// };
  5591. /// \endcode
  5592. /// matches A::x but not B::x
  5593. AST_MATCHER(CXXMethodDecl, isVirtualAsWritten) {
  5594. return Node.isVirtualAsWritten();
  5595. }
  5596. AST_MATCHER(CXXConstructorDecl, isInheritingConstructor) {
  5597. return Node.isInheritingConstructor();
  5598. }
  5599. /// Matches if the given method or class declaration is final.
  5600. ///
  5601. /// Given:
  5602. /// \code
  5603. /// class A final {};
  5604. ///
  5605. /// struct B {
  5606. /// virtual void f();
  5607. /// };
  5608. ///
  5609. /// struct C : B {
  5610. /// void f() final;
  5611. /// };
  5612. /// \endcode
  5613. /// matches A and C::f, but not B, C, or B::f
  5614. AST_POLYMORPHIC_MATCHER(isFinal,
  5615. AST_POLYMORPHIC_SUPPORTED_TYPES(CXXRecordDecl,
  5616. CXXMethodDecl)) {
  5617. return Node.template hasAttr<FinalAttr>();
  5618. }
  5619. /// Matches if the given method declaration is pure.
  5620. ///
  5621. /// Given
  5622. /// \code
  5623. /// class A {
  5624. /// public:
  5625. /// virtual void x() = 0;
  5626. /// };
  5627. /// \endcode
  5628. /// matches A::x
  5629. AST_MATCHER(CXXMethodDecl, isPure) {
  5630. return Node.isPure();
  5631. }
  5632. /// Matches if the given method declaration is const.
  5633. ///
  5634. /// Given
  5635. /// \code
  5636. /// struct A {
  5637. /// void foo() const;
  5638. /// void bar();
  5639. /// };
  5640. /// \endcode
  5641. ///
  5642. /// cxxMethodDecl(isConst()) matches A::foo() but not A::bar()
  5643. AST_MATCHER(CXXMethodDecl, isConst) {
  5644. return Node.isConst();
  5645. }
  5646. /// Matches if the given method declaration declares a copy assignment
  5647. /// operator.
  5648. ///
  5649. /// Given
  5650. /// \code
  5651. /// struct A {
  5652. /// A &operator=(const A &);
  5653. /// A &operator=(A &&);
  5654. /// };
  5655. /// \endcode
  5656. ///
  5657. /// cxxMethodDecl(isCopyAssignmentOperator()) matches the first method but not
  5658. /// the second one.
  5659. AST_MATCHER(CXXMethodDecl, isCopyAssignmentOperator) {
  5660. return Node.isCopyAssignmentOperator();
  5661. }
  5662. /// Matches if the given method declaration declares a move assignment
  5663. /// operator.
  5664. ///
  5665. /// Given
  5666. /// \code
  5667. /// struct A {
  5668. /// A &operator=(const A &);
  5669. /// A &operator=(A &&);
  5670. /// };
  5671. /// \endcode
  5672. ///
  5673. /// cxxMethodDecl(isMoveAssignmentOperator()) matches the second method but not
  5674. /// the first one.
  5675. AST_MATCHER(CXXMethodDecl, isMoveAssignmentOperator) {
  5676. return Node.isMoveAssignmentOperator();
  5677. }
  5678. /// Matches if the given method declaration overrides another method.
  5679. ///
  5680. /// Given
  5681. /// \code
  5682. /// class A {
  5683. /// public:
  5684. /// virtual void x();
  5685. /// };
  5686. /// class B : public A {
  5687. /// public:
  5688. /// virtual void x();
  5689. /// };
  5690. /// \endcode
  5691. /// matches B::x
  5692. AST_MATCHER(CXXMethodDecl, isOverride) {
  5693. return Node.size_overridden_methods() > 0 || Node.hasAttr<OverrideAttr>();
  5694. }
  5695. /// Matches method declarations that are user-provided.
  5696. ///
  5697. /// Given
  5698. /// \code
  5699. /// struct S {
  5700. /// S(); // #1
  5701. /// S(const S &) = default; // #2
  5702. /// S(S &&) = delete; // #3
  5703. /// };
  5704. /// \endcode
  5705. /// cxxConstructorDecl(isUserProvided()) will match #1, but not #2 or #3.
  5706. AST_MATCHER(CXXMethodDecl, isUserProvided) {
  5707. return Node.isUserProvided();
  5708. }
  5709. /// Matches member expressions that are called with '->' as opposed
  5710. /// to '.'.
  5711. ///
  5712. /// Member calls on the implicit this pointer match as called with '->'.
  5713. ///
  5714. /// Given
  5715. /// \code
  5716. /// class Y {
  5717. /// void x() { this->x(); x(); Y y; y.x(); a; this->b; Y::b; }
  5718. /// template <class T> void f() { this->f<T>(); f<T>(); }
  5719. /// int a;
  5720. /// static int b;
  5721. /// };
  5722. /// template <class T>
  5723. /// class Z {
  5724. /// void x() { this->m; }
  5725. /// };
  5726. /// \endcode
  5727. /// memberExpr(isArrow())
  5728. /// matches this->x, x, y.x, a, this->b
  5729. /// cxxDependentScopeMemberExpr(isArrow())
  5730. /// matches this->m
  5731. /// unresolvedMemberExpr(isArrow())
  5732. /// matches this->f<T>, f<T>
  5733. AST_POLYMORPHIC_MATCHER(
  5734. isArrow, AST_POLYMORPHIC_SUPPORTED_TYPES(MemberExpr, UnresolvedMemberExpr,
  5735. CXXDependentScopeMemberExpr)) {
  5736. return Node.isArrow();
  5737. }
  5738. /// Matches QualType nodes that are of integer type.
  5739. ///
  5740. /// Given
  5741. /// \code
  5742. /// void a(int);
  5743. /// void b(long);
  5744. /// void c(double);
  5745. /// \endcode
  5746. /// functionDecl(hasAnyParameter(hasType(isInteger())))
  5747. /// matches "a(int)", "b(long)", but not "c(double)".
  5748. AST_MATCHER(QualType, isInteger) {
  5749. return Node->isIntegerType();
  5750. }
  5751. /// Matches QualType nodes that are of unsigned integer type.
  5752. ///
  5753. /// Given
  5754. /// \code
  5755. /// void a(int);
  5756. /// void b(unsigned long);
  5757. /// void c(double);
  5758. /// \endcode
  5759. /// functionDecl(hasAnyParameter(hasType(isUnsignedInteger())))
  5760. /// matches "b(unsigned long)", but not "a(int)" and "c(double)".
  5761. AST_MATCHER(QualType, isUnsignedInteger) {
  5762. return Node->isUnsignedIntegerType();
  5763. }
  5764. /// Matches QualType nodes that are of signed integer type.
  5765. ///
  5766. /// Given
  5767. /// \code
  5768. /// void a(int);
  5769. /// void b(unsigned long);
  5770. /// void c(double);
  5771. /// \endcode
  5772. /// functionDecl(hasAnyParameter(hasType(isSignedInteger())))
  5773. /// matches "a(int)", but not "b(unsigned long)" and "c(double)".
  5774. AST_MATCHER(QualType, isSignedInteger) {
  5775. return Node->isSignedIntegerType();
  5776. }
  5777. /// Matches QualType nodes that are of character type.
  5778. ///
  5779. /// Given
  5780. /// \code
  5781. /// void a(char);
  5782. /// void b(wchar_t);
  5783. /// void c(double);
  5784. /// \endcode
  5785. /// functionDecl(hasAnyParameter(hasType(isAnyCharacter())))
  5786. /// matches "a(char)", "b(wchar_t)", but not "c(double)".
  5787. AST_MATCHER(QualType, isAnyCharacter) {
  5788. return Node->isAnyCharacterType();
  5789. }
  5790. /// Matches QualType nodes that are of any pointer type; this includes
  5791. /// the Objective-C object pointer type, which is different despite being
  5792. /// syntactically similar.
  5793. ///
  5794. /// Given
  5795. /// \code
  5796. /// int *i = nullptr;
  5797. ///
  5798. /// @interface Foo
  5799. /// @end
  5800. /// Foo *f;
  5801. ///
  5802. /// int j;
  5803. /// \endcode
  5804. /// varDecl(hasType(isAnyPointer()))
  5805. /// matches "int *i" and "Foo *f", but not "int j".
  5806. AST_MATCHER(QualType, isAnyPointer) {
  5807. return Node->isAnyPointerType();
  5808. }
  5809. /// Matches QualType nodes that are const-qualified, i.e., that
  5810. /// include "top-level" const.
  5811. ///
  5812. /// Given
  5813. /// \code
  5814. /// void a(int);
  5815. /// void b(int const);
  5816. /// void c(const int);
  5817. /// void d(const int*);
  5818. /// void e(int const) {};
  5819. /// \endcode
  5820. /// functionDecl(hasAnyParameter(hasType(isConstQualified())))
  5821. /// matches "void b(int const)", "void c(const int)" and
  5822. /// "void e(int const) {}". It does not match d as there
  5823. /// is no top-level const on the parameter type "const int *".
  5824. AST_MATCHER(QualType, isConstQualified) {
  5825. return Node.isConstQualified();
  5826. }
  5827. /// Matches QualType nodes that are volatile-qualified, i.e., that
  5828. /// include "top-level" volatile.
  5829. ///
  5830. /// Given
  5831. /// \code
  5832. /// void a(int);
  5833. /// void b(int volatile);
  5834. /// void c(volatile int);
  5835. /// void d(volatile int*);
  5836. /// void e(int volatile) {};
  5837. /// \endcode
  5838. /// functionDecl(hasAnyParameter(hasType(isVolatileQualified())))
  5839. /// matches "void b(int volatile)", "void c(volatile int)" and
  5840. /// "void e(int volatile) {}". It does not match d as there
  5841. /// is no top-level volatile on the parameter type "volatile int *".
  5842. AST_MATCHER(QualType, isVolatileQualified) {
  5843. return Node.isVolatileQualified();
  5844. }
  5845. /// Matches QualType nodes that have local CV-qualifiers attached to
  5846. /// the node, not hidden within a typedef.
  5847. ///
  5848. /// Given
  5849. /// \code
  5850. /// typedef const int const_int;
  5851. /// const_int i;
  5852. /// int *const j;
  5853. /// int *volatile k;
  5854. /// int m;
  5855. /// \endcode
  5856. /// \c varDecl(hasType(hasLocalQualifiers())) matches only \c j and \c k.
  5857. /// \c i is const-qualified but the qualifier is not local.
  5858. AST_MATCHER(QualType, hasLocalQualifiers) {
  5859. return Node.hasLocalQualifiers();
  5860. }
  5861. /// Matches a member expression where the member is matched by a
  5862. /// given matcher.
  5863. ///
  5864. /// Given
  5865. /// \code
  5866. /// struct { int first, second; } first, second;
  5867. /// int i(second.first);
  5868. /// int j(first.second);
  5869. /// \endcode
  5870. /// memberExpr(member(hasName("first")))
  5871. /// matches second.first
  5872. /// but not first.second (because the member name there is "second").
  5873. AST_MATCHER_P(MemberExpr, member,
  5874. internal::Matcher<ValueDecl>, InnerMatcher) {
  5875. return InnerMatcher.matches(*Node.getMemberDecl(), Finder, Builder);
  5876. }
  5877. /// Matches a member expression where the object expression is matched by a
  5878. /// given matcher. Implicit object expressions are included; that is, it matches
  5879. /// use of implicit `this`.
  5880. ///
  5881. /// Given
  5882. /// \code
  5883. /// struct X {
  5884. /// int m;
  5885. /// int f(X x) { x.m; return m; }
  5886. /// };
  5887. /// \endcode
  5888. /// memberExpr(hasObjectExpression(hasType(cxxRecordDecl(hasName("X")))))
  5889. /// matches `x.m`, but not `m`; however,
  5890. /// memberExpr(hasObjectExpression(hasType(pointsTo(
  5891. // cxxRecordDecl(hasName("X"))))))
  5892. /// matches `m` (aka. `this->m`), but not `x.m`.
  5893. AST_POLYMORPHIC_MATCHER_P(
  5894. hasObjectExpression,
  5895. AST_POLYMORPHIC_SUPPORTED_TYPES(MemberExpr, UnresolvedMemberExpr,
  5896. CXXDependentScopeMemberExpr),
  5897. internal::Matcher<Expr>, InnerMatcher) {
  5898. if (const auto *E = dyn_cast<UnresolvedMemberExpr>(&Node))
  5899. if (E->isImplicitAccess())
  5900. return false;
  5901. if (const auto *E = dyn_cast<CXXDependentScopeMemberExpr>(&Node))
  5902. if (E->isImplicitAccess())
  5903. return false;
  5904. return InnerMatcher.matches(*Node.getBase(), Finder, Builder);
  5905. }
  5906. /// Matches any using shadow declaration.
  5907. ///
  5908. /// Given
  5909. /// \code
  5910. /// namespace X { void b(); }
  5911. /// using X::b;
  5912. /// \endcode
  5913. /// usingDecl(hasAnyUsingShadowDecl(hasName("b"))))
  5914. /// matches \code using X::b \endcode
  5915. AST_MATCHER_P(BaseUsingDecl, hasAnyUsingShadowDecl,
  5916. internal::Matcher<UsingShadowDecl>, InnerMatcher) {
  5917. return matchesFirstInPointerRange(InnerMatcher, Node.shadow_begin(),
  5918. Node.shadow_end(), Finder,
  5919. Builder) != Node.shadow_end();
  5920. }
  5921. /// Matches a using shadow declaration where the target declaration is
  5922. /// matched by the given matcher.
  5923. ///
  5924. /// Given
  5925. /// \code
  5926. /// namespace X { int a; void b(); }
  5927. /// using X::a;
  5928. /// using X::b;
  5929. /// \endcode
  5930. /// usingDecl(hasAnyUsingShadowDecl(hasTargetDecl(functionDecl())))
  5931. /// matches \code using X::b \endcode
  5932. /// but not \code using X::a \endcode
  5933. AST_MATCHER_P(UsingShadowDecl, hasTargetDecl,
  5934. internal::Matcher<NamedDecl>, InnerMatcher) {
  5935. return InnerMatcher.matches(*Node.getTargetDecl(), Finder, Builder);
  5936. }
  5937. /// Matches template instantiations of function, class, or static
  5938. /// member variable template instantiations.
  5939. ///
  5940. /// Given
  5941. /// \code
  5942. /// template <typename T> class X {}; class A {}; X<A> x;
  5943. /// \endcode
  5944. /// or
  5945. /// \code
  5946. /// template <typename T> class X {}; class A {}; template class X<A>;
  5947. /// \endcode
  5948. /// or
  5949. /// \code
  5950. /// template <typename T> class X {}; class A {}; extern template class X<A>;
  5951. /// \endcode
  5952. /// cxxRecordDecl(hasName("::X"), isTemplateInstantiation())
  5953. /// matches the template instantiation of X<A>.
  5954. ///
  5955. /// But given
  5956. /// \code
  5957. /// template <typename T> class X {}; class A {};
  5958. /// template <> class X<A> {}; X<A> x;
  5959. /// \endcode
  5960. /// cxxRecordDecl(hasName("::X"), isTemplateInstantiation())
  5961. /// does not match, as X<A> is an explicit template specialization.
  5962. ///
  5963. /// Usable as: Matcher<FunctionDecl>, Matcher<VarDecl>, Matcher<CXXRecordDecl>
  5964. AST_POLYMORPHIC_MATCHER(isTemplateInstantiation,
  5965. AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, VarDecl,
  5966. CXXRecordDecl)) {
  5967. return (Node.getTemplateSpecializationKind() == TSK_ImplicitInstantiation ||
  5968. Node.getTemplateSpecializationKind() ==
  5969. TSK_ExplicitInstantiationDefinition ||
  5970. Node.getTemplateSpecializationKind() ==
  5971. TSK_ExplicitInstantiationDeclaration);
  5972. }
  5973. /// Matches declarations that are template instantiations or are inside
  5974. /// template instantiations.
  5975. ///
  5976. /// Given
  5977. /// \code
  5978. /// template<typename T> void A(T t) { T i; }
  5979. /// A(0);
  5980. /// A(0U);
  5981. /// \endcode
  5982. /// functionDecl(isInstantiated())
  5983. /// matches 'A(int) {...};' and 'A(unsigned) {...}'.
  5984. AST_MATCHER_FUNCTION(internal::Matcher<Decl>, isInstantiated) {
  5985. auto IsInstantiation = decl(anyOf(cxxRecordDecl(isTemplateInstantiation()),
  5986. functionDecl(isTemplateInstantiation())));
  5987. return decl(anyOf(IsInstantiation, hasAncestor(IsInstantiation)));
  5988. }
  5989. /// Matches statements inside of a template instantiation.
  5990. ///
  5991. /// Given
  5992. /// \code
  5993. /// int j;
  5994. /// template<typename T> void A(T t) { T i; j += 42;}
  5995. /// A(0);
  5996. /// A(0U);
  5997. /// \endcode
  5998. /// declStmt(isInTemplateInstantiation())
  5999. /// matches 'int i;' and 'unsigned i'.
  6000. /// unless(stmt(isInTemplateInstantiation()))
  6001. /// will NOT match j += 42; as it's shared between the template definition and
  6002. /// instantiation.
  6003. AST_MATCHER_FUNCTION(internal::Matcher<Stmt>, isInTemplateInstantiation) {
  6004. return stmt(
  6005. hasAncestor(decl(anyOf(cxxRecordDecl(isTemplateInstantiation()),
  6006. functionDecl(isTemplateInstantiation())))));
  6007. }
  6008. /// Matches explicit template specializations of function, class, or
  6009. /// static member variable template instantiations.
  6010. ///
  6011. /// Given
  6012. /// \code
  6013. /// template<typename T> void A(T t) { }
  6014. /// template<> void A(int N) { }
  6015. /// \endcode
  6016. /// functionDecl(isExplicitTemplateSpecialization())
  6017. /// matches the specialization A<int>().
  6018. ///
  6019. /// Usable as: Matcher<FunctionDecl>, Matcher<VarDecl>, Matcher<CXXRecordDecl>
  6020. AST_POLYMORPHIC_MATCHER(isExplicitTemplateSpecialization,
  6021. AST_POLYMORPHIC_SUPPORTED_TYPES(FunctionDecl, VarDecl,
  6022. CXXRecordDecl)) {
  6023. return (Node.getTemplateSpecializationKind() == TSK_ExplicitSpecialization);
  6024. }
  6025. /// Matches \c TypeLocs for which the given inner
  6026. /// QualType-matcher matches.
  6027. AST_MATCHER_FUNCTION_P_OVERLOAD(internal::BindableMatcher<TypeLoc>, loc,
  6028. internal::Matcher<QualType>, InnerMatcher, 0) {
  6029. return internal::BindableMatcher<TypeLoc>(
  6030. new internal::TypeLocTypeMatcher(InnerMatcher));
  6031. }
  6032. /// Matches `QualifiedTypeLoc`s in the clang AST.
  6033. ///
  6034. /// Given
  6035. /// \code
  6036. /// const int x = 0;
  6037. /// \endcode
  6038. /// qualifiedTypeLoc()
  6039. /// matches `const int`.
  6040. extern const internal::VariadicDynCastAllOfMatcher<TypeLoc, QualifiedTypeLoc>
  6041. qualifiedTypeLoc;
  6042. /// Matches `QualifiedTypeLoc`s that have an unqualified `TypeLoc` matching
  6043. /// `InnerMatcher`.
  6044. ///
  6045. /// Given
  6046. /// \code
  6047. /// int* const x;
  6048. /// const int y;
  6049. /// \endcode
  6050. /// qualifiedTypeLoc(hasUnqualifiedLoc(pointerTypeLoc()))
  6051. /// matches the `TypeLoc` of the variable declaration of `x`, but not `y`.
  6052. AST_MATCHER_P(QualifiedTypeLoc, hasUnqualifiedLoc, internal::Matcher<TypeLoc>,
  6053. InnerMatcher) {
  6054. return InnerMatcher.matches(Node.getUnqualifiedLoc(), Finder, Builder);
  6055. }
  6056. /// Matches a function declared with the specified return `TypeLoc`.
  6057. ///
  6058. /// Given
  6059. /// \code
  6060. /// int f() { return 5; }
  6061. /// void g() {}
  6062. /// \endcode
  6063. /// functionDecl(hasReturnTypeLoc(loc(asString("int"))))
  6064. /// matches the declaration of `f`, but not `g`.
  6065. AST_MATCHER_P(FunctionDecl, hasReturnTypeLoc, internal::Matcher<TypeLoc>,
  6066. ReturnMatcher) {
  6067. auto Loc = Node.getFunctionTypeLoc();
  6068. return Loc && ReturnMatcher.matches(Loc.getReturnLoc(), Finder, Builder);
  6069. }
  6070. /// Matches pointer `TypeLoc`s.
  6071. ///
  6072. /// Given
  6073. /// \code
  6074. /// int* x;
  6075. /// \endcode
  6076. /// pointerTypeLoc()
  6077. /// matches `int*`.
  6078. extern const internal::VariadicDynCastAllOfMatcher<TypeLoc, PointerTypeLoc>
  6079. pointerTypeLoc;
  6080. /// Matches pointer `TypeLoc`s that have a pointee `TypeLoc` matching
  6081. /// `PointeeMatcher`.
  6082. ///
  6083. /// Given
  6084. /// \code
  6085. /// int* x;
  6086. /// \endcode
  6087. /// pointerTypeLoc(hasPointeeLoc(loc(asString("int"))))
  6088. /// matches `int*`.
  6089. AST_MATCHER_P(PointerTypeLoc, hasPointeeLoc, internal::Matcher<TypeLoc>,
  6090. PointeeMatcher) {
  6091. return PointeeMatcher.matches(Node.getPointeeLoc(), Finder, Builder);
  6092. }
  6093. /// Matches reference `TypeLoc`s.
  6094. ///
  6095. /// Given
  6096. /// \code
  6097. /// int x = 3;
  6098. /// int& l = x;
  6099. /// int&& r = 3;
  6100. /// \endcode
  6101. /// referenceTypeLoc()
  6102. /// matches `int&` and `int&&`.
  6103. extern const internal::VariadicDynCastAllOfMatcher<TypeLoc, ReferenceTypeLoc>
  6104. referenceTypeLoc;
  6105. /// Matches reference `TypeLoc`s that have a referent `TypeLoc` matching
  6106. /// `ReferentMatcher`.
  6107. ///
  6108. /// Given
  6109. /// \code
  6110. /// int x = 3;
  6111. /// int& xx = x;
  6112. /// \endcode
  6113. /// referenceTypeLoc(hasReferentLoc(loc(asString("int"))))
  6114. /// matches `int&`.
  6115. AST_MATCHER_P(ReferenceTypeLoc, hasReferentLoc, internal::Matcher<TypeLoc>,
  6116. ReferentMatcher) {
  6117. return ReferentMatcher.matches(Node.getPointeeLoc(), Finder, Builder);
  6118. }
  6119. /// Matches template specialization `TypeLoc`s.
  6120. ///
  6121. /// Given
  6122. /// \code
  6123. /// template <typename T> class C {};
  6124. /// C<char> var;
  6125. /// \endcode
  6126. /// varDecl(hasTypeLoc(templateSpecializationTypeLoc(typeLoc())))
  6127. /// matches `C<char> var`.
  6128. extern const internal::VariadicDynCastAllOfMatcher<
  6129. TypeLoc, TemplateSpecializationTypeLoc>
  6130. templateSpecializationTypeLoc;
  6131. /// Matches template specialization `TypeLoc`s that have at least one
  6132. /// `TemplateArgumentLoc` matching the given `InnerMatcher`.
  6133. ///
  6134. /// Given
  6135. /// \code
  6136. /// template<typename T> class A {};
  6137. /// A<int> a;
  6138. /// \endcode
  6139. /// varDecl(hasTypeLoc(templateSpecializationTypeLoc(hasAnyTemplateArgumentLoc(
  6140. /// hasTypeLoc(loc(asString("int")))))))
  6141. /// matches `A<int> a`.
  6142. AST_MATCHER_P(TemplateSpecializationTypeLoc, hasAnyTemplateArgumentLoc,
  6143. internal::Matcher<TemplateArgumentLoc>, InnerMatcher) {
  6144. for (unsigned Index = 0, N = Node.getNumArgs(); Index < N; ++Index) {
  6145. clang::ast_matchers::internal::BoundNodesTreeBuilder Result(*Builder);
  6146. if (InnerMatcher.matches(Node.getArgLoc(Index), Finder, &Result)) {
  6147. *Builder = std::move(Result);
  6148. return true;
  6149. }
  6150. }
  6151. return false;
  6152. }
  6153. /// Matches template specialization `TypeLoc`s where the n'th
  6154. /// `TemplateArgumentLoc` matches the given `InnerMatcher`.
  6155. ///
  6156. /// Given
  6157. /// \code
  6158. /// template<typename T, typename U> class A {};
  6159. /// A<double, int> b;
  6160. /// A<int, double> c;
  6161. /// \endcode
  6162. /// varDecl(hasTypeLoc(templateSpecializationTypeLoc(hasTemplateArgumentLoc(0,
  6163. /// hasTypeLoc(loc(asString("double")))))))
  6164. /// matches `A<double, int> b`, but not `A<int, double> c`.
  6165. AST_POLYMORPHIC_MATCHER_P2(
  6166. hasTemplateArgumentLoc,
  6167. AST_POLYMORPHIC_SUPPORTED_TYPES(DeclRefExpr, TemplateSpecializationTypeLoc),
  6168. unsigned, Index, internal::Matcher<TemplateArgumentLoc>, InnerMatcher) {
  6169. return internal::MatchTemplateArgLocAt(Node, Index, InnerMatcher, Finder,
  6170. Builder);
  6171. }
  6172. /// Matches C or C++ elaborated `TypeLoc`s.
  6173. ///
  6174. /// Given
  6175. /// \code
  6176. /// struct s {};
  6177. /// struct s ss;
  6178. /// \endcode
  6179. /// elaboratedTypeLoc()
  6180. /// matches the `TypeLoc` of the variable declaration of `ss`.
  6181. extern const internal::VariadicDynCastAllOfMatcher<TypeLoc, ElaboratedTypeLoc>
  6182. elaboratedTypeLoc;
  6183. /// Matches elaborated `TypeLoc`s that have a named `TypeLoc` matching
  6184. /// `InnerMatcher`.
  6185. ///
  6186. /// Given
  6187. /// \code
  6188. /// template <typename T>
  6189. /// class C {};
  6190. /// class C<int> c;
  6191. ///
  6192. /// class D {};
  6193. /// class D d;
  6194. /// \endcode
  6195. /// elaboratedTypeLoc(hasNamedTypeLoc(templateSpecializationTypeLoc()));
  6196. /// matches the `TypeLoc` of the variable declaration of `c`, but not `d`.
  6197. AST_MATCHER_P(ElaboratedTypeLoc, hasNamedTypeLoc, internal::Matcher<TypeLoc>,
  6198. InnerMatcher) {
  6199. return InnerMatcher.matches(Node.getNamedTypeLoc(), Finder, Builder);
  6200. }
  6201. /// Matches type \c bool.
  6202. ///
  6203. /// Given
  6204. /// \code
  6205. /// struct S { bool func(); };
  6206. /// \endcode
  6207. /// functionDecl(returns(booleanType()))
  6208. /// matches "bool func();"
  6209. AST_MATCHER(Type, booleanType) {
  6210. return Node.isBooleanType();
  6211. }
  6212. /// Matches type \c void.
  6213. ///
  6214. /// Given
  6215. /// \code
  6216. /// struct S { void func(); };
  6217. /// \endcode
  6218. /// functionDecl(returns(voidType()))
  6219. /// matches "void func();"
  6220. AST_MATCHER(Type, voidType) {
  6221. return Node.isVoidType();
  6222. }
  6223. template <typename NodeType>
  6224. using AstTypeMatcher = internal::VariadicDynCastAllOfMatcher<Type, NodeType>;
  6225. /// Matches builtin Types.
  6226. ///
  6227. /// Given
  6228. /// \code
  6229. /// struct A {};
  6230. /// A a;
  6231. /// int b;
  6232. /// float c;
  6233. /// bool d;
  6234. /// \endcode
  6235. /// builtinType()
  6236. /// matches "int b", "float c" and "bool d"
  6237. extern const AstTypeMatcher<BuiltinType> builtinType;
  6238. /// Matches all kinds of arrays.
  6239. ///
  6240. /// Given
  6241. /// \code
  6242. /// int a[] = { 2, 3 };
  6243. /// int b[4];
  6244. /// void f() { int c[a[0]]; }
  6245. /// \endcode
  6246. /// arrayType()
  6247. /// matches "int a[]", "int b[4]" and "int c[a[0]]";
  6248. extern const AstTypeMatcher<ArrayType> arrayType;
  6249. /// Matches C99 complex types.
  6250. ///
  6251. /// Given
  6252. /// \code
  6253. /// _Complex float f;
  6254. /// \endcode
  6255. /// complexType()
  6256. /// matches "_Complex float f"
  6257. extern const AstTypeMatcher<ComplexType> complexType;
  6258. /// Matches any real floating-point type (float, double, long double).
  6259. ///
  6260. /// Given
  6261. /// \code
  6262. /// int i;
  6263. /// float f;
  6264. /// \endcode
  6265. /// realFloatingPointType()
  6266. /// matches "float f" but not "int i"
  6267. AST_MATCHER(Type, realFloatingPointType) {
  6268. return Node.isRealFloatingType();
  6269. }
  6270. /// Matches arrays and C99 complex types that have a specific element
  6271. /// type.
  6272. ///
  6273. /// Given
  6274. /// \code
  6275. /// struct A {};
  6276. /// A a[7];
  6277. /// int b[7];
  6278. /// \endcode
  6279. /// arrayType(hasElementType(builtinType()))
  6280. /// matches "int b[7]"
  6281. ///
  6282. /// Usable as: Matcher<ArrayType>, Matcher<ComplexType>
  6283. AST_TYPELOC_TRAVERSE_MATCHER_DECL(hasElementType, getElement,
  6284. AST_POLYMORPHIC_SUPPORTED_TYPES(ArrayType,
  6285. ComplexType));
  6286. /// Matches C arrays with a specified constant size.
  6287. ///
  6288. /// Given
  6289. /// \code
  6290. /// void() {
  6291. /// int a[2];
  6292. /// int b[] = { 2, 3 };
  6293. /// int c[b[0]];
  6294. /// }
  6295. /// \endcode
  6296. /// constantArrayType()
  6297. /// matches "int a[2]"
  6298. extern const AstTypeMatcher<ConstantArrayType> constantArrayType;
  6299. /// Matches nodes that have the specified size.
  6300. ///
  6301. /// Given
  6302. /// \code
  6303. /// int a[42];
  6304. /// int b[2 * 21];
  6305. /// int c[41], d[43];
  6306. /// char *s = "abcd";
  6307. /// wchar_t *ws = L"abcd";
  6308. /// char *w = "a";
  6309. /// \endcode
  6310. /// constantArrayType(hasSize(42))
  6311. /// matches "int a[42]" and "int b[2 * 21]"
  6312. /// stringLiteral(hasSize(4))
  6313. /// matches "abcd", L"abcd"
  6314. AST_POLYMORPHIC_MATCHER_P(hasSize,
  6315. AST_POLYMORPHIC_SUPPORTED_TYPES(ConstantArrayType,
  6316. StringLiteral),
  6317. unsigned, N) {
  6318. return internal::HasSizeMatcher<NodeType>::hasSize(Node, N);
  6319. }
  6320. /// Matches C++ arrays whose size is a value-dependent expression.
  6321. ///
  6322. /// Given
  6323. /// \code
  6324. /// template<typename T, int Size>
  6325. /// class array {
  6326. /// T data[Size];
  6327. /// };
  6328. /// \endcode
  6329. /// dependentSizedArrayType
  6330. /// matches "T data[Size]"
  6331. extern const AstTypeMatcher<DependentSizedArrayType> dependentSizedArrayType;
  6332. /// Matches C arrays with unspecified size.
  6333. ///
  6334. /// Given
  6335. /// \code
  6336. /// int a[] = { 2, 3 };
  6337. /// int b[42];
  6338. /// void f(int c[]) { int d[a[0]]; };
  6339. /// \endcode
  6340. /// incompleteArrayType()
  6341. /// matches "int a[]" and "int c[]"
  6342. extern const AstTypeMatcher<IncompleteArrayType> incompleteArrayType;
  6343. /// Matches C arrays with a specified size that is not an
  6344. /// integer-constant-expression.
  6345. ///
  6346. /// Given
  6347. /// \code
  6348. /// void f() {
  6349. /// int a[] = { 2, 3 }
  6350. /// int b[42];
  6351. /// int c[a[0]];
  6352. /// }
  6353. /// \endcode
  6354. /// variableArrayType()
  6355. /// matches "int c[a[0]]"
  6356. extern const AstTypeMatcher<VariableArrayType> variableArrayType;
  6357. /// Matches \c VariableArrayType nodes that have a specific size
  6358. /// expression.
  6359. ///
  6360. /// Given
  6361. /// \code
  6362. /// void f(int b) {
  6363. /// int a[b];
  6364. /// }
  6365. /// \endcode
  6366. /// variableArrayType(hasSizeExpr(ignoringImpCasts(declRefExpr(to(
  6367. /// varDecl(hasName("b")))))))
  6368. /// matches "int a[b]"
  6369. AST_MATCHER_P(VariableArrayType, hasSizeExpr,
  6370. internal::Matcher<Expr>, InnerMatcher) {
  6371. return InnerMatcher.matches(*Node.getSizeExpr(), Finder, Builder);
  6372. }
  6373. /// Matches atomic types.
  6374. ///
  6375. /// Given
  6376. /// \code
  6377. /// _Atomic(int) i;
  6378. /// \endcode
  6379. /// atomicType()
  6380. /// matches "_Atomic(int) i"
  6381. extern const AstTypeMatcher<AtomicType> atomicType;
  6382. /// Matches atomic types with a specific value type.
  6383. ///
  6384. /// Given
  6385. /// \code
  6386. /// _Atomic(int) i;
  6387. /// _Atomic(float) f;
  6388. /// \endcode
  6389. /// atomicType(hasValueType(isInteger()))
  6390. /// matches "_Atomic(int) i"
  6391. ///
  6392. /// Usable as: Matcher<AtomicType>
  6393. AST_TYPELOC_TRAVERSE_MATCHER_DECL(hasValueType, getValue,
  6394. AST_POLYMORPHIC_SUPPORTED_TYPES(AtomicType));
  6395. /// Matches types nodes representing C++11 auto types.
  6396. ///
  6397. /// Given:
  6398. /// \code
  6399. /// auto n = 4;
  6400. /// int v[] = { 2, 3 }
  6401. /// for (auto i : v) { }
  6402. /// \endcode
  6403. /// autoType()
  6404. /// matches "auto n" and "auto i"
  6405. extern const AstTypeMatcher<AutoType> autoType;
  6406. /// Matches types nodes representing C++11 decltype(<expr>) types.
  6407. ///
  6408. /// Given:
  6409. /// \code
  6410. /// short i = 1;
  6411. /// int j = 42;
  6412. /// decltype(i + j) result = i + j;
  6413. /// \endcode
  6414. /// decltypeType()
  6415. /// matches "decltype(i + j)"
  6416. extern const AstTypeMatcher<DecltypeType> decltypeType;
  6417. /// Matches \c AutoType nodes where the deduced type is a specific type.
  6418. ///
  6419. /// Note: There is no \c TypeLoc for the deduced type and thus no
  6420. /// \c getDeducedLoc() matcher.
  6421. ///
  6422. /// Given
  6423. /// \code
  6424. /// auto a = 1;
  6425. /// auto b = 2.0;
  6426. /// \endcode
  6427. /// autoType(hasDeducedType(isInteger()))
  6428. /// matches "auto a"
  6429. ///
  6430. /// Usable as: Matcher<AutoType>
  6431. AST_TYPE_TRAVERSE_MATCHER(hasDeducedType, getDeducedType,
  6432. AST_POLYMORPHIC_SUPPORTED_TYPES(AutoType));
  6433. /// Matches \c DecltypeType or \c UsingType nodes to find the underlying type.
  6434. ///
  6435. /// Given
  6436. /// \code
  6437. /// decltype(1) a = 1;
  6438. /// decltype(2.0) b = 2.0;
  6439. /// \endcode
  6440. /// decltypeType(hasUnderlyingType(isInteger()))
  6441. /// matches the type of "a"
  6442. ///
  6443. /// Usable as: Matcher<DecltypeType>, Matcher<UsingType>
  6444. AST_TYPE_TRAVERSE_MATCHER(hasUnderlyingType, getUnderlyingType,
  6445. AST_POLYMORPHIC_SUPPORTED_TYPES(DecltypeType,
  6446. UsingType));
  6447. /// Matches \c FunctionType nodes.
  6448. ///
  6449. /// Given
  6450. /// \code
  6451. /// int (*f)(int);
  6452. /// void g();
  6453. /// \endcode
  6454. /// functionType()
  6455. /// matches "int (*f)(int)" and the type of "g".
  6456. extern const AstTypeMatcher<FunctionType> functionType;
  6457. /// Matches \c FunctionProtoType nodes.
  6458. ///
  6459. /// Given
  6460. /// \code
  6461. /// int (*f)(int);
  6462. /// void g();
  6463. /// \endcode
  6464. /// functionProtoType()
  6465. /// matches "int (*f)(int)" and the type of "g" in C++ mode.
  6466. /// In C mode, "g" is not matched because it does not contain a prototype.
  6467. extern const AstTypeMatcher<FunctionProtoType> functionProtoType;
  6468. /// Matches \c ParenType nodes.
  6469. ///
  6470. /// Given
  6471. /// \code
  6472. /// int (*ptr_to_array)[4];
  6473. /// int *array_of_ptrs[4];
  6474. /// \endcode
  6475. ///
  6476. /// \c varDecl(hasType(pointsTo(parenType()))) matches \c ptr_to_array but not
  6477. /// \c array_of_ptrs.
  6478. extern const AstTypeMatcher<ParenType> parenType;
  6479. /// Matches \c ParenType nodes where the inner type is a specific type.
  6480. ///
  6481. /// Given
  6482. /// \code
  6483. /// int (*ptr_to_array)[4];
  6484. /// int (*ptr_to_func)(int);
  6485. /// \endcode
  6486. ///
  6487. /// \c varDecl(hasType(pointsTo(parenType(innerType(functionType()))))) matches
  6488. /// \c ptr_to_func but not \c ptr_to_array.
  6489. ///
  6490. /// Usable as: Matcher<ParenType>
  6491. AST_TYPE_TRAVERSE_MATCHER(innerType, getInnerType,
  6492. AST_POLYMORPHIC_SUPPORTED_TYPES(ParenType));
  6493. /// Matches block pointer types, i.e. types syntactically represented as
  6494. /// "void (^)(int)".
  6495. ///
  6496. /// The \c pointee is always required to be a \c FunctionType.
  6497. extern const AstTypeMatcher<BlockPointerType> blockPointerType;
  6498. /// Matches member pointer types.
  6499. /// Given
  6500. /// \code
  6501. /// struct A { int i; }
  6502. /// A::* ptr = A::i;
  6503. /// \endcode
  6504. /// memberPointerType()
  6505. /// matches "A::* ptr"
  6506. extern const AstTypeMatcher<MemberPointerType> memberPointerType;
  6507. /// Matches pointer types, but does not match Objective-C object pointer
  6508. /// types.
  6509. ///
  6510. /// Given
  6511. /// \code
  6512. /// int *a;
  6513. /// int &b = *a;
  6514. /// int c = 5;
  6515. ///
  6516. /// @interface Foo
  6517. /// @end
  6518. /// Foo *f;
  6519. /// \endcode
  6520. /// pointerType()
  6521. /// matches "int *a", but does not match "Foo *f".
  6522. extern const AstTypeMatcher<PointerType> pointerType;
  6523. /// Matches an Objective-C object pointer type, which is different from
  6524. /// a pointer type, despite being syntactically similar.
  6525. ///
  6526. /// Given
  6527. /// \code
  6528. /// int *a;
  6529. ///
  6530. /// @interface Foo
  6531. /// @end
  6532. /// Foo *f;
  6533. /// \endcode
  6534. /// pointerType()
  6535. /// matches "Foo *f", but does not match "int *a".
  6536. extern const AstTypeMatcher<ObjCObjectPointerType> objcObjectPointerType;
  6537. /// Matches both lvalue and rvalue reference types.
  6538. ///
  6539. /// Given
  6540. /// \code
  6541. /// int *a;
  6542. /// int &b = *a;
  6543. /// int &&c = 1;
  6544. /// auto &d = b;
  6545. /// auto &&e = c;
  6546. /// auto &&f = 2;
  6547. /// int g = 5;
  6548. /// \endcode
  6549. ///
  6550. /// \c referenceType() matches the types of \c b, \c c, \c d, \c e, and \c f.
  6551. extern const AstTypeMatcher<ReferenceType> referenceType;
  6552. /// Matches lvalue reference types.
  6553. ///
  6554. /// Given:
  6555. /// \code
  6556. /// int *a;
  6557. /// int &b = *a;
  6558. /// int &&c = 1;
  6559. /// auto &d = b;
  6560. /// auto &&e = c;
  6561. /// auto &&f = 2;
  6562. /// int g = 5;
  6563. /// \endcode
  6564. ///
  6565. /// \c lValueReferenceType() matches the types of \c b, \c d, and \c e. \c e is
  6566. /// matched since the type is deduced as int& by reference collapsing rules.
  6567. extern const AstTypeMatcher<LValueReferenceType> lValueReferenceType;
  6568. /// Matches rvalue reference types.
  6569. ///
  6570. /// Given:
  6571. /// \code
  6572. /// int *a;
  6573. /// int &b = *a;
  6574. /// int &&c = 1;
  6575. /// auto &d = b;
  6576. /// auto &&e = c;
  6577. /// auto &&f = 2;
  6578. /// int g = 5;
  6579. /// \endcode
  6580. ///
  6581. /// \c rValueReferenceType() matches the types of \c c and \c f. \c e is not
  6582. /// matched as it is deduced to int& by reference collapsing rules.
  6583. extern const AstTypeMatcher<RValueReferenceType> rValueReferenceType;
  6584. /// Narrows PointerType (and similar) matchers to those where the
  6585. /// \c pointee matches a given matcher.
  6586. ///
  6587. /// Given
  6588. /// \code
  6589. /// int *a;
  6590. /// int const *b;
  6591. /// float const *f;
  6592. /// \endcode
  6593. /// pointerType(pointee(isConstQualified(), isInteger()))
  6594. /// matches "int const *b"
  6595. ///
  6596. /// Usable as: Matcher<BlockPointerType>, Matcher<MemberPointerType>,
  6597. /// Matcher<PointerType>, Matcher<ReferenceType>
  6598. AST_TYPELOC_TRAVERSE_MATCHER_DECL(
  6599. pointee, getPointee,
  6600. AST_POLYMORPHIC_SUPPORTED_TYPES(BlockPointerType, MemberPointerType,
  6601. PointerType, ReferenceType));
  6602. /// Matches typedef types.
  6603. ///
  6604. /// Given
  6605. /// \code
  6606. /// typedef int X;
  6607. /// \endcode
  6608. /// typedefType()
  6609. /// matches "typedef int X"
  6610. extern const AstTypeMatcher<TypedefType> typedefType;
  6611. /// Matches enum types.
  6612. ///
  6613. /// Given
  6614. /// \code
  6615. /// enum C { Green };
  6616. /// enum class S { Red };
  6617. ///
  6618. /// C c;
  6619. /// S s;
  6620. /// \endcode
  6621. //
  6622. /// \c enumType() matches the type of the variable declarations of both \c c and
  6623. /// \c s.
  6624. extern const AstTypeMatcher<EnumType> enumType;
  6625. /// Matches template specialization types.
  6626. ///
  6627. /// Given
  6628. /// \code
  6629. /// template <typename T>
  6630. /// class C { };
  6631. ///
  6632. /// template class C<int>; // A
  6633. /// C<char> var; // B
  6634. /// \endcode
  6635. ///
  6636. /// \c templateSpecializationType() matches the type of the explicit
  6637. /// instantiation in \c A and the type of the variable declaration in \c B.
  6638. extern const AstTypeMatcher<TemplateSpecializationType>
  6639. templateSpecializationType;
  6640. /// Matches C++17 deduced template specialization types, e.g. deduced class
  6641. /// template types.
  6642. ///
  6643. /// Given
  6644. /// \code
  6645. /// template <typename T>
  6646. /// class C { public: C(T); };
  6647. ///
  6648. /// C c(123);
  6649. /// \endcode
  6650. /// \c deducedTemplateSpecializationType() matches the type in the declaration
  6651. /// of the variable \c c.
  6652. extern const AstTypeMatcher<DeducedTemplateSpecializationType>
  6653. deducedTemplateSpecializationType;
  6654. /// Matches types nodes representing unary type transformations.
  6655. ///
  6656. /// Given:
  6657. /// \code
  6658. /// typedef __underlying_type(T) type;
  6659. /// \endcode
  6660. /// unaryTransformType()
  6661. /// matches "__underlying_type(T)"
  6662. extern const AstTypeMatcher<UnaryTransformType> unaryTransformType;
  6663. /// Matches record types (e.g. structs, classes).
  6664. ///
  6665. /// Given
  6666. /// \code
  6667. /// class C {};
  6668. /// struct S {};
  6669. ///
  6670. /// C c;
  6671. /// S s;
  6672. /// \endcode
  6673. ///
  6674. /// \c recordType() matches the type of the variable declarations of both \c c
  6675. /// and \c s.
  6676. extern const AstTypeMatcher<RecordType> recordType;
  6677. /// Matches tag types (record and enum types).
  6678. ///
  6679. /// Given
  6680. /// \code
  6681. /// enum E {};
  6682. /// class C {};
  6683. ///
  6684. /// E e;
  6685. /// C c;
  6686. /// \endcode
  6687. ///
  6688. /// \c tagType() matches the type of the variable declarations of both \c e
  6689. /// and \c c.
  6690. extern const AstTypeMatcher<TagType> tagType;
  6691. /// Matches types specified with an elaborated type keyword or with a
  6692. /// qualified name.
  6693. ///
  6694. /// Given
  6695. /// \code
  6696. /// namespace N {
  6697. /// namespace M {
  6698. /// class D {};
  6699. /// }
  6700. /// }
  6701. /// class C {};
  6702. ///
  6703. /// class C c;
  6704. /// N::M::D d;
  6705. /// \endcode
  6706. ///
  6707. /// \c elaboratedType() matches the type of the variable declarations of both
  6708. /// \c c and \c d.
  6709. extern const AstTypeMatcher<ElaboratedType> elaboratedType;
  6710. /// Matches ElaboratedTypes whose qualifier, a NestedNameSpecifier,
  6711. /// matches \c InnerMatcher if the qualifier exists.
  6712. ///
  6713. /// Given
  6714. /// \code
  6715. /// namespace N {
  6716. /// namespace M {
  6717. /// class D {};
  6718. /// }
  6719. /// }
  6720. /// N::M::D d;
  6721. /// \endcode
  6722. ///
  6723. /// \c elaboratedType(hasQualifier(hasPrefix(specifiesNamespace(hasName("N"))))
  6724. /// matches the type of the variable declaration of \c d.
  6725. AST_MATCHER_P(ElaboratedType, hasQualifier,
  6726. internal::Matcher<NestedNameSpecifier>, InnerMatcher) {
  6727. if (const NestedNameSpecifier *Qualifier = Node.getQualifier())
  6728. return InnerMatcher.matches(*Qualifier, Finder, Builder);
  6729. return false;
  6730. }
  6731. /// Matches ElaboratedTypes whose named type matches \c InnerMatcher.
  6732. ///
  6733. /// Given
  6734. /// \code
  6735. /// namespace N {
  6736. /// namespace M {
  6737. /// class D {};
  6738. /// }
  6739. /// }
  6740. /// N::M::D d;
  6741. /// \endcode
  6742. ///
  6743. /// \c elaboratedType(namesType(recordType(
  6744. /// hasDeclaration(namedDecl(hasName("D")))))) matches the type of the variable
  6745. /// declaration of \c d.
  6746. AST_MATCHER_P(ElaboratedType, namesType, internal::Matcher<QualType>,
  6747. InnerMatcher) {
  6748. return InnerMatcher.matches(Node.getNamedType(), Finder, Builder);
  6749. }
  6750. /// Matches types specified through a using declaration.
  6751. ///
  6752. /// Given
  6753. /// \code
  6754. /// namespace a { struct S {}; }
  6755. /// using a::S;
  6756. /// S s;
  6757. /// \endcode
  6758. ///
  6759. /// \c usingType() matches the type of the variable declaration of \c s.
  6760. extern const AstTypeMatcher<UsingType> usingType;
  6761. /// Matches types that represent the result of substituting a type for a
  6762. /// template type parameter.
  6763. ///
  6764. /// Given
  6765. /// \code
  6766. /// template <typename T>
  6767. /// void F(T t) {
  6768. /// int i = 1 + t;
  6769. /// }
  6770. /// \endcode
  6771. ///
  6772. /// \c substTemplateTypeParmType() matches the type of 't' but not '1'
  6773. extern const AstTypeMatcher<SubstTemplateTypeParmType>
  6774. substTemplateTypeParmType;
  6775. /// Matches template type parameter substitutions that have a replacement
  6776. /// type that matches the provided matcher.
  6777. ///
  6778. /// Given
  6779. /// \code
  6780. /// template <typename T>
  6781. /// double F(T t);
  6782. /// int i;
  6783. /// double j = F(i);
  6784. /// \endcode
  6785. ///
  6786. /// \c substTemplateTypeParmType(hasReplacementType(type())) matches int
  6787. AST_TYPE_TRAVERSE_MATCHER(
  6788. hasReplacementType, getReplacementType,
  6789. AST_POLYMORPHIC_SUPPORTED_TYPES(SubstTemplateTypeParmType));
  6790. /// Matches template type parameter types.
  6791. ///
  6792. /// Example matches T, but not int.
  6793. /// (matcher = templateTypeParmType())
  6794. /// \code
  6795. /// template <typename T> void f(int i);
  6796. /// \endcode
  6797. extern const AstTypeMatcher<TemplateTypeParmType> templateTypeParmType;
  6798. /// Matches injected class name types.
  6799. ///
  6800. /// Example matches S s, but not S<T> s.
  6801. /// (matcher = parmVarDecl(hasType(injectedClassNameType())))
  6802. /// \code
  6803. /// template <typename T> struct S {
  6804. /// void f(S s);
  6805. /// void g(S<T> s);
  6806. /// };
  6807. /// \endcode
  6808. extern const AstTypeMatcher<InjectedClassNameType> injectedClassNameType;
  6809. /// Matches decayed type
  6810. /// Example matches i[] in declaration of f.
  6811. /// (matcher = valueDecl(hasType(decayedType(hasDecayedType(pointerType())))))
  6812. /// Example matches i[1].
  6813. /// (matcher = expr(hasType(decayedType(hasDecayedType(pointerType())))))
  6814. /// \code
  6815. /// void f(int i[]) {
  6816. /// i[1] = 0;
  6817. /// }
  6818. /// \endcode
  6819. extern const AstTypeMatcher<DecayedType> decayedType;
  6820. /// Matches the decayed type, whoes decayed type matches \c InnerMatcher
  6821. AST_MATCHER_P(DecayedType, hasDecayedType, internal::Matcher<QualType>,
  6822. InnerType) {
  6823. return InnerType.matches(Node.getDecayedType(), Finder, Builder);
  6824. }
  6825. /// Matches declarations whose declaration context, interpreted as a
  6826. /// Decl, matches \c InnerMatcher.
  6827. ///
  6828. /// Given
  6829. /// \code
  6830. /// namespace N {
  6831. /// namespace M {
  6832. /// class D {};
  6833. /// }
  6834. /// }
  6835. /// \endcode
  6836. ///
  6837. /// \c cxxRcordDecl(hasDeclContext(namedDecl(hasName("M")))) matches the
  6838. /// declaration of \c class \c D.
  6839. AST_MATCHER_P(Decl, hasDeclContext, internal::Matcher<Decl>, InnerMatcher) {
  6840. const DeclContext *DC = Node.getDeclContext();
  6841. if (!DC) return false;
  6842. return InnerMatcher.matches(*Decl::castFromDeclContext(DC), Finder, Builder);
  6843. }
  6844. /// Matches nested name specifiers.
  6845. ///
  6846. /// Given
  6847. /// \code
  6848. /// namespace ns {
  6849. /// struct A { static void f(); };
  6850. /// void A::f() {}
  6851. /// void g() { A::f(); }
  6852. /// }
  6853. /// ns::A a;
  6854. /// \endcode
  6855. /// nestedNameSpecifier()
  6856. /// matches "ns::" and both "A::"
  6857. extern const internal::VariadicAllOfMatcher<NestedNameSpecifier>
  6858. nestedNameSpecifier;
  6859. /// Same as \c nestedNameSpecifier but matches \c NestedNameSpecifierLoc.
  6860. extern const internal::VariadicAllOfMatcher<NestedNameSpecifierLoc>
  6861. nestedNameSpecifierLoc;
  6862. /// Matches \c NestedNameSpecifierLocs for which the given inner
  6863. /// NestedNameSpecifier-matcher matches.
  6864. AST_MATCHER_FUNCTION_P_OVERLOAD(
  6865. internal::BindableMatcher<NestedNameSpecifierLoc>, loc,
  6866. internal::Matcher<NestedNameSpecifier>, InnerMatcher, 1) {
  6867. return internal::BindableMatcher<NestedNameSpecifierLoc>(
  6868. new internal::LocMatcher<NestedNameSpecifierLoc, NestedNameSpecifier>(
  6869. InnerMatcher));
  6870. }
  6871. /// Matches nested name specifiers that specify a type matching the
  6872. /// given \c QualType matcher without qualifiers.
  6873. ///
  6874. /// Given
  6875. /// \code
  6876. /// struct A { struct B { struct C {}; }; };
  6877. /// A::B::C c;
  6878. /// \endcode
  6879. /// nestedNameSpecifier(specifiesType(
  6880. /// hasDeclaration(cxxRecordDecl(hasName("A")))
  6881. /// ))
  6882. /// matches "A::"
  6883. AST_MATCHER_P(NestedNameSpecifier, specifiesType,
  6884. internal::Matcher<QualType>, InnerMatcher) {
  6885. if (!Node.getAsType())
  6886. return false;
  6887. return InnerMatcher.matches(QualType(Node.getAsType(), 0), Finder, Builder);
  6888. }
  6889. /// Matches nested name specifier locs that specify a type matching the
  6890. /// given \c TypeLoc.
  6891. ///
  6892. /// Given
  6893. /// \code
  6894. /// struct A { struct B { struct C {}; }; };
  6895. /// A::B::C c;
  6896. /// \endcode
  6897. /// nestedNameSpecifierLoc(specifiesTypeLoc(loc(type(
  6898. /// hasDeclaration(cxxRecordDecl(hasName("A")))))))
  6899. /// matches "A::"
  6900. AST_MATCHER_P(NestedNameSpecifierLoc, specifiesTypeLoc,
  6901. internal::Matcher<TypeLoc>, InnerMatcher) {
  6902. return Node && Node.getNestedNameSpecifier()->getAsType() &&
  6903. InnerMatcher.matches(Node.getTypeLoc(), Finder, Builder);
  6904. }
  6905. /// Matches on the prefix of a \c NestedNameSpecifier.
  6906. ///
  6907. /// Given
  6908. /// \code
  6909. /// struct A { struct B { struct C {}; }; };
  6910. /// A::B::C c;
  6911. /// \endcode
  6912. /// nestedNameSpecifier(hasPrefix(specifiesType(asString("struct A")))) and
  6913. /// matches "A::"
  6914. AST_MATCHER_P_OVERLOAD(NestedNameSpecifier, hasPrefix,
  6915. internal::Matcher<NestedNameSpecifier>, InnerMatcher,
  6916. 0) {
  6917. const NestedNameSpecifier *NextNode = Node.getPrefix();
  6918. if (!NextNode)
  6919. return false;
  6920. return InnerMatcher.matches(*NextNode, Finder, Builder);
  6921. }
  6922. /// Matches on the prefix of a \c NestedNameSpecifierLoc.
  6923. ///
  6924. /// Given
  6925. /// \code
  6926. /// struct A { struct B { struct C {}; }; };
  6927. /// A::B::C c;
  6928. /// \endcode
  6929. /// nestedNameSpecifierLoc(hasPrefix(loc(specifiesType(asString("struct A")))))
  6930. /// matches "A::"
  6931. AST_MATCHER_P_OVERLOAD(NestedNameSpecifierLoc, hasPrefix,
  6932. internal::Matcher<NestedNameSpecifierLoc>, InnerMatcher,
  6933. 1) {
  6934. NestedNameSpecifierLoc NextNode = Node.getPrefix();
  6935. if (!NextNode)
  6936. return false;
  6937. return InnerMatcher.matches(NextNode, Finder, Builder);
  6938. }
  6939. /// Matches nested name specifiers that specify a namespace matching the
  6940. /// given namespace matcher.
  6941. ///
  6942. /// Given
  6943. /// \code
  6944. /// namespace ns { struct A {}; }
  6945. /// ns::A a;
  6946. /// \endcode
  6947. /// nestedNameSpecifier(specifiesNamespace(hasName("ns")))
  6948. /// matches "ns::"
  6949. AST_MATCHER_P(NestedNameSpecifier, specifiesNamespace,
  6950. internal::Matcher<NamespaceDecl>, InnerMatcher) {
  6951. if (!Node.getAsNamespace())
  6952. return false;
  6953. return InnerMatcher.matches(*Node.getAsNamespace(), Finder, Builder);
  6954. }
  6955. /// Matches attributes.
  6956. /// Attributes may be attached with a variety of different syntaxes (including
  6957. /// keywords, C++11 attributes, GNU ``__attribute``` and MSVC `__declspec``,
  6958. /// and ``#pragma``s). They may also be implicit.
  6959. ///
  6960. /// Given
  6961. /// \code
  6962. /// struct [[nodiscard]] Foo{};
  6963. /// void bar(int * __attribute__((nonnull)) );
  6964. /// __declspec(noinline) void baz();
  6965. ///
  6966. /// #pragma omp declare simd
  6967. /// int min();
  6968. /// \endcode
  6969. /// attr()
  6970. /// matches "nodiscard", "nonnull", "noinline", and the whole "#pragma" line.
  6971. extern const internal::VariadicAllOfMatcher<Attr> attr;
  6972. /// Overloads for the \c equalsNode matcher.
  6973. /// FIXME: Implement for other node types.
  6974. /// @{
  6975. /// Matches if a node equals another node.
  6976. ///
  6977. /// \c Decl has pointer identity in the AST.
  6978. AST_MATCHER_P_OVERLOAD(Decl, equalsNode, const Decl*, Other, 0) {
  6979. return &Node == Other;
  6980. }
  6981. /// Matches if a node equals another node.
  6982. ///
  6983. /// \c Stmt has pointer identity in the AST.
  6984. AST_MATCHER_P_OVERLOAD(Stmt, equalsNode, const Stmt*, Other, 1) {
  6985. return &Node == Other;
  6986. }
  6987. /// Matches if a node equals another node.
  6988. ///
  6989. /// \c Type has pointer identity in the AST.
  6990. AST_MATCHER_P_OVERLOAD(Type, equalsNode, const Type*, Other, 2) {
  6991. return &Node == Other;
  6992. }
  6993. /// @}
  6994. /// Matches each case or default statement belonging to the given switch
  6995. /// statement. This matcher may produce multiple matches.
  6996. ///
  6997. /// Given
  6998. /// \code
  6999. /// switch (1) { case 1: case 2: default: switch (2) { case 3: case 4: ; } }
  7000. /// \endcode
  7001. /// switchStmt(forEachSwitchCase(caseStmt().bind("c"))).bind("s")
  7002. /// matches four times, with "c" binding each of "case 1:", "case 2:",
  7003. /// "case 3:" and "case 4:", and "s" respectively binding "switch (1)",
  7004. /// "switch (1)", "switch (2)" and "switch (2)".
  7005. AST_MATCHER_P(SwitchStmt, forEachSwitchCase, internal::Matcher<SwitchCase>,
  7006. InnerMatcher) {
  7007. BoundNodesTreeBuilder Result;
  7008. // FIXME: getSwitchCaseList() does not necessarily guarantee a stable
  7009. // iteration order. We should use the more general iterating matchers once
  7010. // they are capable of expressing this matcher (for example, it should ignore
  7011. // case statements belonging to nested switch statements).
  7012. bool Matched = false;
  7013. for (const SwitchCase *SC = Node.getSwitchCaseList(); SC;
  7014. SC = SC->getNextSwitchCase()) {
  7015. BoundNodesTreeBuilder CaseBuilder(*Builder);
  7016. bool CaseMatched = InnerMatcher.matches(*SC, Finder, &CaseBuilder);
  7017. if (CaseMatched) {
  7018. Matched = true;
  7019. Result.addMatch(CaseBuilder);
  7020. }
  7021. }
  7022. *Builder = std::move(Result);
  7023. return Matched;
  7024. }
  7025. /// Matches each constructor initializer in a constructor definition.
  7026. ///
  7027. /// Given
  7028. /// \code
  7029. /// class A { A() : i(42), j(42) {} int i; int j; };
  7030. /// \endcode
  7031. /// cxxConstructorDecl(forEachConstructorInitializer(
  7032. /// forField(decl().bind("x"))
  7033. /// ))
  7034. /// will trigger two matches, binding for 'i' and 'j' respectively.
  7035. AST_MATCHER_P(CXXConstructorDecl, forEachConstructorInitializer,
  7036. internal::Matcher<CXXCtorInitializer>, InnerMatcher) {
  7037. BoundNodesTreeBuilder Result;
  7038. bool Matched = false;
  7039. for (const auto *I : Node.inits()) {
  7040. if (Finder->isTraversalIgnoringImplicitNodes() && !I->isWritten())
  7041. continue;
  7042. BoundNodesTreeBuilder InitBuilder(*Builder);
  7043. if (InnerMatcher.matches(*I, Finder, &InitBuilder)) {
  7044. Matched = true;
  7045. Result.addMatch(InitBuilder);
  7046. }
  7047. }
  7048. *Builder = std::move(Result);
  7049. return Matched;
  7050. }
  7051. /// Matches constructor declarations that are copy constructors.
  7052. ///
  7053. /// Given
  7054. /// \code
  7055. /// struct S {
  7056. /// S(); // #1
  7057. /// S(const S &); // #2
  7058. /// S(S &&); // #3
  7059. /// };
  7060. /// \endcode
  7061. /// cxxConstructorDecl(isCopyConstructor()) will match #2, but not #1 or #3.
  7062. AST_MATCHER(CXXConstructorDecl, isCopyConstructor) {
  7063. return Node.isCopyConstructor();
  7064. }
  7065. /// Matches constructor declarations that are move constructors.
  7066. ///
  7067. /// Given
  7068. /// \code
  7069. /// struct S {
  7070. /// S(); // #1
  7071. /// S(const S &); // #2
  7072. /// S(S &&); // #3
  7073. /// };
  7074. /// \endcode
  7075. /// cxxConstructorDecl(isMoveConstructor()) will match #3, but not #1 or #2.
  7076. AST_MATCHER(CXXConstructorDecl, isMoveConstructor) {
  7077. return Node.isMoveConstructor();
  7078. }
  7079. /// Matches constructor declarations that are default constructors.
  7080. ///
  7081. /// Given
  7082. /// \code
  7083. /// struct S {
  7084. /// S(); // #1
  7085. /// S(const S &); // #2
  7086. /// S(S &&); // #3
  7087. /// };
  7088. /// \endcode
  7089. /// cxxConstructorDecl(isDefaultConstructor()) will match #1, but not #2 or #3.
  7090. AST_MATCHER(CXXConstructorDecl, isDefaultConstructor) {
  7091. return Node.isDefaultConstructor();
  7092. }
  7093. /// Matches constructors that delegate to another constructor.
  7094. ///
  7095. /// Given
  7096. /// \code
  7097. /// struct S {
  7098. /// S(); // #1
  7099. /// S(int) {} // #2
  7100. /// S(S &&) : S() {} // #3
  7101. /// };
  7102. /// S::S() : S(0) {} // #4
  7103. /// \endcode
  7104. /// cxxConstructorDecl(isDelegatingConstructor()) will match #3 and #4, but not
  7105. /// #1 or #2.
  7106. AST_MATCHER(CXXConstructorDecl, isDelegatingConstructor) {
  7107. return Node.isDelegatingConstructor();
  7108. }
  7109. /// Matches constructor, conversion function, and deduction guide declarations
  7110. /// that have an explicit specifier if this explicit specifier is resolved to
  7111. /// true.
  7112. ///
  7113. /// Given
  7114. /// \code
  7115. /// template<bool b>
  7116. /// struct S {
  7117. /// S(int); // #1
  7118. /// explicit S(double); // #2
  7119. /// operator int(); // #3
  7120. /// explicit operator bool(); // #4
  7121. /// explicit(false) S(bool) // # 7
  7122. /// explicit(true) S(char) // # 8
  7123. /// explicit(b) S(S) // # 9
  7124. /// };
  7125. /// S(int) -> S<true> // #5
  7126. /// explicit S(double) -> S<false> // #6
  7127. /// \endcode
  7128. /// cxxConstructorDecl(isExplicit()) will match #2 and #8, but not #1, #7 or #9.
  7129. /// cxxConversionDecl(isExplicit()) will match #4, but not #3.
  7130. /// cxxDeductionGuideDecl(isExplicit()) will match #6, but not #5.
  7131. AST_POLYMORPHIC_MATCHER(isExplicit, AST_POLYMORPHIC_SUPPORTED_TYPES(
  7132. CXXConstructorDecl, CXXConversionDecl,
  7133. CXXDeductionGuideDecl)) {
  7134. return Node.isExplicit();
  7135. }
  7136. /// Matches the expression in an explicit specifier if present in the given
  7137. /// declaration.
  7138. ///
  7139. /// Given
  7140. /// \code
  7141. /// template<bool b>
  7142. /// struct S {
  7143. /// S(int); // #1
  7144. /// explicit S(double); // #2
  7145. /// operator int(); // #3
  7146. /// explicit operator bool(); // #4
  7147. /// explicit(false) S(bool) // # 7
  7148. /// explicit(true) S(char) // # 8
  7149. /// explicit(b) S(S) // # 9
  7150. /// };
  7151. /// S(int) -> S<true> // #5
  7152. /// explicit S(double) -> S<false> // #6
  7153. /// \endcode
  7154. /// cxxConstructorDecl(hasExplicitSpecifier(constantExpr())) will match #7, #8 and #9, but not #1 or #2.
  7155. /// cxxConversionDecl(hasExplicitSpecifier(constantExpr())) will not match #3 or #4.
  7156. /// cxxDeductionGuideDecl(hasExplicitSpecifier(constantExpr())) will not match #5 or #6.
  7157. AST_MATCHER_P(FunctionDecl, hasExplicitSpecifier, internal::Matcher<Expr>,
  7158. InnerMatcher) {
  7159. ExplicitSpecifier ES = ExplicitSpecifier::getFromDecl(&Node);
  7160. if (!ES.getExpr())
  7161. return false;
  7162. ASTChildrenNotSpelledInSourceScope RAII(Finder, false);
  7163. return InnerMatcher.matches(*ES.getExpr(), Finder, Builder);
  7164. }
  7165. /// Matches function and namespace declarations that are marked with
  7166. /// the inline keyword.
  7167. ///
  7168. /// Given
  7169. /// \code
  7170. /// inline void f();
  7171. /// void g();
  7172. /// namespace n {
  7173. /// inline namespace m {}
  7174. /// }
  7175. /// \endcode
  7176. /// functionDecl(isInline()) will match ::f().
  7177. /// namespaceDecl(isInline()) will match n::m.
  7178. AST_POLYMORPHIC_MATCHER(isInline,
  7179. AST_POLYMORPHIC_SUPPORTED_TYPES(NamespaceDecl,
  7180. FunctionDecl)) {
  7181. // This is required because the spelling of the function used to determine
  7182. // whether inline is specified or not differs between the polymorphic types.
  7183. if (const auto *FD = dyn_cast<FunctionDecl>(&Node))
  7184. return FD->isInlineSpecified();
  7185. else if (const auto *NSD = dyn_cast<NamespaceDecl>(&Node))
  7186. return NSD->isInline();
  7187. llvm_unreachable("Not a valid polymorphic type");
  7188. }
  7189. /// Matches anonymous namespace declarations.
  7190. ///
  7191. /// Given
  7192. /// \code
  7193. /// namespace n {
  7194. /// namespace {} // #1
  7195. /// }
  7196. /// \endcode
  7197. /// namespaceDecl(isAnonymous()) will match #1 but not ::n.
  7198. AST_MATCHER(NamespaceDecl, isAnonymous) {
  7199. return Node.isAnonymousNamespace();
  7200. }
  7201. /// Matches declarations in the namespace `std`, but not in nested namespaces.
  7202. ///
  7203. /// Given
  7204. /// \code
  7205. /// class vector {};
  7206. /// namespace foo {
  7207. /// class vector {};
  7208. /// namespace std {
  7209. /// class vector {};
  7210. /// }
  7211. /// }
  7212. /// namespace std {
  7213. /// inline namespace __1 {
  7214. /// class vector {}; // #1
  7215. /// namespace experimental {
  7216. /// class vector {};
  7217. /// }
  7218. /// }
  7219. /// }
  7220. /// \endcode
  7221. /// cxxRecordDecl(hasName("vector"), isInStdNamespace()) will match only #1.
  7222. AST_MATCHER(Decl, isInStdNamespace) { return Node.isInStdNamespace(); }
  7223. /// If the given case statement does not use the GNU case range
  7224. /// extension, matches the constant given in the statement.
  7225. ///
  7226. /// Given
  7227. /// \code
  7228. /// switch (1) { case 1: case 1+1: case 3 ... 4: ; }
  7229. /// \endcode
  7230. /// caseStmt(hasCaseConstant(integerLiteral()))
  7231. /// matches "case 1:"
  7232. AST_MATCHER_P(CaseStmt, hasCaseConstant, internal::Matcher<Expr>,
  7233. InnerMatcher) {
  7234. if (Node.getRHS())
  7235. return false;
  7236. return InnerMatcher.matches(*Node.getLHS(), Finder, Builder);
  7237. }
  7238. /// Matches declaration that has a given attribute.
  7239. ///
  7240. /// Given
  7241. /// \code
  7242. /// __attribute__((device)) void f() { ... }
  7243. /// \endcode
  7244. /// decl(hasAttr(clang::attr::CUDADevice)) matches the function declaration of
  7245. /// f. If the matcher is used from clang-query, attr::Kind parameter should be
  7246. /// passed as a quoted string. e.g., hasAttr("attr::CUDADevice").
  7247. AST_MATCHER_P(Decl, hasAttr, attr::Kind, AttrKind) {
  7248. for (const auto *Attr : Node.attrs()) {
  7249. if (Attr->getKind() == AttrKind)
  7250. return true;
  7251. }
  7252. return false;
  7253. }
  7254. /// Matches the return value expression of a return statement
  7255. ///
  7256. /// Given
  7257. /// \code
  7258. /// return a + b;
  7259. /// \endcode
  7260. /// hasReturnValue(binaryOperator())
  7261. /// matches 'return a + b'
  7262. /// with binaryOperator()
  7263. /// matching 'a + b'
  7264. AST_MATCHER_P(ReturnStmt, hasReturnValue, internal::Matcher<Expr>,
  7265. InnerMatcher) {
  7266. if (const auto *RetValue = Node.getRetValue())
  7267. return InnerMatcher.matches(*RetValue, Finder, Builder);
  7268. return false;
  7269. }
  7270. /// Matches CUDA kernel call expression.
  7271. ///
  7272. /// Example matches,
  7273. /// \code
  7274. /// kernel<<<i,j>>>();
  7275. /// \endcode
  7276. extern const internal::VariadicDynCastAllOfMatcher<Stmt, CUDAKernelCallExpr>
  7277. cudaKernelCallExpr;
  7278. /// Matches expressions that resolve to a null pointer constant, such as
  7279. /// GNU's __null, C++11's nullptr, or C's NULL macro.
  7280. ///
  7281. /// Given:
  7282. /// \code
  7283. /// void *v1 = NULL;
  7284. /// void *v2 = nullptr;
  7285. /// void *v3 = __null; // GNU extension
  7286. /// char *cp = (char *)0;
  7287. /// int *ip = 0;
  7288. /// int i = 0;
  7289. /// \endcode
  7290. /// expr(nullPointerConstant())
  7291. /// matches the initializer for v1, v2, v3, cp, and ip. Does not match the
  7292. /// initializer for i.
  7293. AST_MATCHER_FUNCTION(internal::Matcher<Expr>, nullPointerConstant) {
  7294. return anyOf(
  7295. gnuNullExpr(), cxxNullPtrLiteralExpr(),
  7296. integerLiteral(equals(0), hasParent(expr(hasType(pointerType())))));
  7297. }
  7298. /// Matches the DecompositionDecl the binding belongs to.
  7299. ///
  7300. /// For example, in:
  7301. /// \code
  7302. /// void foo()
  7303. /// {
  7304. /// int arr[3];
  7305. /// auto &[f, s, t] = arr;
  7306. ///
  7307. /// f = 42;
  7308. /// }
  7309. /// \endcode
  7310. /// The matcher:
  7311. /// \code
  7312. /// bindingDecl(hasName("f"),
  7313. /// forDecomposition(decompositionDecl())
  7314. /// \endcode
  7315. /// matches 'f' in 'auto &[f, s, t]'.
  7316. AST_MATCHER_P(BindingDecl, forDecomposition, internal::Matcher<ValueDecl>,
  7317. InnerMatcher) {
  7318. if (const ValueDecl *VD = Node.getDecomposedDecl())
  7319. return InnerMatcher.matches(*VD, Finder, Builder);
  7320. return false;
  7321. }
  7322. /// Matches the Nth binding of a DecompositionDecl.
  7323. ///
  7324. /// For example, in:
  7325. /// \code
  7326. /// void foo()
  7327. /// {
  7328. /// int arr[3];
  7329. /// auto &[f, s, t] = arr;
  7330. ///
  7331. /// f = 42;
  7332. /// }
  7333. /// \endcode
  7334. /// The matcher:
  7335. /// \code
  7336. /// decompositionDecl(hasBinding(0,
  7337. /// bindingDecl(hasName("f").bind("fBinding"))))
  7338. /// \endcode
  7339. /// matches the decomposition decl with 'f' bound to "fBinding".
  7340. AST_MATCHER_P2(DecompositionDecl, hasBinding, unsigned, N,
  7341. internal::Matcher<BindingDecl>, InnerMatcher) {
  7342. if (Node.bindings().size() <= N)
  7343. return false;
  7344. return InnerMatcher.matches(*Node.bindings()[N], Finder, Builder);
  7345. }
  7346. /// Matches any binding of a DecompositionDecl.
  7347. ///
  7348. /// For example, in:
  7349. /// \code
  7350. /// void foo()
  7351. /// {
  7352. /// int arr[3];
  7353. /// auto &[f, s, t] = arr;
  7354. ///
  7355. /// f = 42;
  7356. /// }
  7357. /// \endcode
  7358. /// The matcher:
  7359. /// \code
  7360. /// decompositionDecl(hasAnyBinding(bindingDecl(hasName("f").bind("fBinding"))))
  7361. /// \endcode
  7362. /// matches the decomposition decl with 'f' bound to "fBinding".
  7363. AST_MATCHER_P(DecompositionDecl, hasAnyBinding, internal::Matcher<BindingDecl>,
  7364. InnerMatcher) {
  7365. return llvm::any_of(Node.bindings(), [&](const auto *Binding) {
  7366. return InnerMatcher.matches(*Binding, Finder, Builder);
  7367. });
  7368. }
  7369. /// Matches declaration of the function the statement belongs to.
  7370. ///
  7371. /// Deprecated. Use forCallable() to correctly handle the situation when
  7372. /// the declaration is not a function (but a block or an Objective-C method).
  7373. /// forFunction() not only fails to take non-functions into account but also
  7374. /// may match the wrong declaration in their presence.
  7375. ///
  7376. /// Given:
  7377. /// \code
  7378. /// F& operator=(const F& o) {
  7379. /// std::copy_if(o.begin(), o.end(), begin(), [](V v) { return v > 0; });
  7380. /// return *this;
  7381. /// }
  7382. /// \endcode
  7383. /// returnStmt(forFunction(hasName("operator=")))
  7384. /// matches 'return *this'
  7385. /// but does not match 'return v > 0'
  7386. AST_MATCHER_P(Stmt, forFunction, internal::Matcher<FunctionDecl>,
  7387. InnerMatcher) {
  7388. const auto &Parents = Finder->getASTContext().getParents(Node);
  7389. llvm::SmallVector<DynTypedNode, 8> Stack(Parents.begin(), Parents.end());
  7390. while (!Stack.empty()) {
  7391. const auto &CurNode = Stack.back();
  7392. Stack.pop_back();
  7393. if (const auto *FuncDeclNode = CurNode.get<FunctionDecl>()) {
  7394. if (InnerMatcher.matches(*FuncDeclNode, Finder, Builder)) {
  7395. return true;
  7396. }
  7397. } else if (const auto *LambdaExprNode = CurNode.get<LambdaExpr>()) {
  7398. if (InnerMatcher.matches(*LambdaExprNode->getCallOperator(), Finder,
  7399. Builder)) {
  7400. return true;
  7401. }
  7402. } else {
  7403. for (const auto &Parent : Finder->getASTContext().getParents(CurNode))
  7404. Stack.push_back(Parent);
  7405. }
  7406. }
  7407. return false;
  7408. }
  7409. /// Matches declaration of the function, method, or block the statement
  7410. /// belongs to.
  7411. ///
  7412. /// Given:
  7413. /// \code
  7414. /// F& operator=(const F& o) {
  7415. /// std::copy_if(o.begin(), o.end(), begin(), [](V v) { return v > 0; });
  7416. /// return *this;
  7417. /// }
  7418. /// \endcode
  7419. /// returnStmt(forCallable(functionDecl(hasName("operator="))))
  7420. /// matches 'return *this'
  7421. /// but does not match 'return v > 0'
  7422. ///
  7423. /// Given:
  7424. /// \code
  7425. /// -(void) foo {
  7426. /// int x = 1;
  7427. /// dispatch_sync(queue, ^{ int y = 2; });
  7428. /// }
  7429. /// \endcode
  7430. /// declStmt(forCallable(objcMethodDecl()))
  7431. /// matches 'int x = 1'
  7432. /// but does not match 'int y = 2'.
  7433. /// whereas declStmt(forCallable(blockDecl()))
  7434. /// matches 'int y = 2'
  7435. /// but does not match 'int x = 1'.
  7436. AST_MATCHER_P(Stmt, forCallable, internal::Matcher<Decl>, InnerMatcher) {
  7437. const auto &Parents = Finder->getASTContext().getParents(Node);
  7438. llvm::SmallVector<DynTypedNode, 8> Stack(Parents.begin(), Parents.end());
  7439. while (!Stack.empty()) {
  7440. const auto &CurNode = Stack.back();
  7441. Stack.pop_back();
  7442. if (const auto *FuncDeclNode = CurNode.get<FunctionDecl>()) {
  7443. if (InnerMatcher.matches(*FuncDeclNode, Finder, Builder)) {
  7444. return true;
  7445. }
  7446. } else if (const auto *LambdaExprNode = CurNode.get<LambdaExpr>()) {
  7447. if (InnerMatcher.matches(*LambdaExprNode->getCallOperator(), Finder,
  7448. Builder)) {
  7449. return true;
  7450. }
  7451. } else if (const auto *ObjCMethodDeclNode = CurNode.get<ObjCMethodDecl>()) {
  7452. if (InnerMatcher.matches(*ObjCMethodDeclNode, Finder, Builder)) {
  7453. return true;
  7454. }
  7455. } else if (const auto *BlockDeclNode = CurNode.get<BlockDecl>()) {
  7456. if (InnerMatcher.matches(*BlockDeclNode, Finder, Builder)) {
  7457. return true;
  7458. }
  7459. } else {
  7460. for (const auto &Parent : Finder->getASTContext().getParents(CurNode))
  7461. Stack.push_back(Parent);
  7462. }
  7463. }
  7464. return false;
  7465. }
  7466. /// Matches a declaration that has external formal linkage.
  7467. ///
  7468. /// Example matches only z (matcher = varDecl(hasExternalFormalLinkage()))
  7469. /// \code
  7470. /// void f() {
  7471. /// int x;
  7472. /// static int y;
  7473. /// }
  7474. /// int z;
  7475. /// \endcode
  7476. ///
  7477. /// Example matches f() because it has external formal linkage despite being
  7478. /// unique to the translation unit as though it has internal likage
  7479. /// (matcher = functionDecl(hasExternalFormalLinkage()))
  7480. ///
  7481. /// \code
  7482. /// namespace {
  7483. /// void f() {}
  7484. /// }
  7485. /// \endcode
  7486. AST_MATCHER(NamedDecl, hasExternalFormalLinkage) {
  7487. return Node.hasExternalFormalLinkage();
  7488. }
  7489. /// Matches a declaration that has default arguments.
  7490. ///
  7491. /// Example matches y (matcher = parmVarDecl(hasDefaultArgument()))
  7492. /// \code
  7493. /// void x(int val) {}
  7494. /// void y(int val = 0) {}
  7495. /// \endcode
  7496. ///
  7497. /// Deprecated. Use hasInitializer() instead to be able to
  7498. /// match on the contents of the default argument. For example:
  7499. ///
  7500. /// \code
  7501. /// void x(int val = 7) {}
  7502. /// void y(int val = 42) {}
  7503. /// \endcode
  7504. /// parmVarDecl(hasInitializer(integerLiteral(equals(42))))
  7505. /// matches the parameter of y
  7506. ///
  7507. /// A matcher such as
  7508. /// parmVarDecl(hasInitializer(anything()))
  7509. /// is equivalent to parmVarDecl(hasDefaultArgument()).
  7510. AST_MATCHER(ParmVarDecl, hasDefaultArgument) {
  7511. return Node.hasDefaultArg();
  7512. }
  7513. /// Matches array new expressions.
  7514. ///
  7515. /// Given:
  7516. /// \code
  7517. /// MyClass *p1 = new MyClass[10];
  7518. /// \endcode
  7519. /// cxxNewExpr(isArray())
  7520. /// matches the expression 'new MyClass[10]'.
  7521. AST_MATCHER(CXXNewExpr, isArray) {
  7522. return Node.isArray();
  7523. }
  7524. /// Matches placement new expression arguments.
  7525. ///
  7526. /// Given:
  7527. /// \code
  7528. /// MyClass *p1 = new (Storage, 16) MyClass();
  7529. /// \endcode
  7530. /// cxxNewExpr(hasPlacementArg(1, integerLiteral(equals(16))))
  7531. /// matches the expression 'new (Storage, 16) MyClass()'.
  7532. AST_MATCHER_P2(CXXNewExpr, hasPlacementArg, unsigned, Index,
  7533. internal::Matcher<Expr>, InnerMatcher) {
  7534. return Node.getNumPlacementArgs() > Index &&
  7535. InnerMatcher.matches(*Node.getPlacementArg(Index), Finder, Builder);
  7536. }
  7537. /// Matches any placement new expression arguments.
  7538. ///
  7539. /// Given:
  7540. /// \code
  7541. /// MyClass *p1 = new (Storage) MyClass();
  7542. /// \endcode
  7543. /// cxxNewExpr(hasAnyPlacementArg(anything()))
  7544. /// matches the expression 'new (Storage, 16) MyClass()'.
  7545. AST_MATCHER_P(CXXNewExpr, hasAnyPlacementArg, internal::Matcher<Expr>,
  7546. InnerMatcher) {
  7547. return llvm::any_of(Node.placement_arguments(), [&](const Expr *Arg) {
  7548. return InnerMatcher.matches(*Arg, Finder, Builder);
  7549. });
  7550. }
  7551. /// Matches array new expressions with a given array size.
  7552. ///
  7553. /// Given:
  7554. /// \code
  7555. /// MyClass *p1 = new MyClass[10];
  7556. /// \endcode
  7557. /// cxxNewExpr(hasArraySize(integerLiteral(equals(10))))
  7558. /// matches the expression 'new MyClass[10]'.
  7559. AST_MATCHER_P(CXXNewExpr, hasArraySize, internal::Matcher<Expr>, InnerMatcher) {
  7560. return Node.isArray() && *Node.getArraySize() &&
  7561. InnerMatcher.matches(**Node.getArraySize(), Finder, Builder);
  7562. }
  7563. /// Matches a class declaration that is defined.
  7564. ///
  7565. /// Example matches x (matcher = cxxRecordDecl(hasDefinition()))
  7566. /// \code
  7567. /// class x {};
  7568. /// class y;
  7569. /// \endcode
  7570. AST_MATCHER(CXXRecordDecl, hasDefinition) {
  7571. return Node.hasDefinition();
  7572. }
  7573. /// Matches C++11 scoped enum declaration.
  7574. ///
  7575. /// Example matches Y (matcher = enumDecl(isScoped()))
  7576. /// \code
  7577. /// enum X {};
  7578. /// enum class Y {};
  7579. /// \endcode
  7580. AST_MATCHER(EnumDecl, isScoped) {
  7581. return Node.isScoped();
  7582. }
  7583. /// Matches a function declared with a trailing return type.
  7584. ///
  7585. /// Example matches Y (matcher = functionDecl(hasTrailingReturn()))
  7586. /// \code
  7587. /// int X() {}
  7588. /// auto Y() -> int {}
  7589. /// \endcode
  7590. AST_MATCHER(FunctionDecl, hasTrailingReturn) {
  7591. if (const auto *F = Node.getType()->getAs<FunctionProtoType>())
  7592. return F->hasTrailingReturn();
  7593. return false;
  7594. }
  7595. /// Matches expressions that match InnerMatcher that are possibly wrapped in an
  7596. /// elidable constructor and other corresponding bookkeeping nodes.
  7597. ///
  7598. /// In C++17, elidable copy constructors are no longer being generated in the
  7599. /// AST as it is not permitted by the standard. They are, however, part of the
  7600. /// AST in C++14 and earlier. So, a matcher must abstract over these differences
  7601. /// to work in all language modes. This matcher skips elidable constructor-call
  7602. /// AST nodes, `ExprWithCleanups` nodes wrapping elidable constructor-calls and
  7603. /// various implicit nodes inside the constructor calls, all of which will not
  7604. /// appear in the C++17 AST.
  7605. ///
  7606. /// Given
  7607. ///
  7608. /// \code
  7609. /// struct H {};
  7610. /// H G();
  7611. /// void f() {
  7612. /// H D = G();
  7613. /// }
  7614. /// \endcode
  7615. ///
  7616. /// ``varDecl(hasInitializer(ignoringElidableConstructorCall(callExpr())))``
  7617. /// matches ``H D = G()`` in C++11 through C++17 (and beyond).
  7618. AST_MATCHER_P(Expr, ignoringElidableConstructorCall,
  7619. ast_matchers::internal::Matcher<Expr>, InnerMatcher) {
  7620. // E tracks the node that we are examining.
  7621. const Expr *E = &Node;
  7622. // If present, remove an outer `ExprWithCleanups` corresponding to the
  7623. // underlying `CXXConstructExpr`. This check won't cover all cases of added
  7624. // `ExprWithCleanups` corresponding to `CXXConstructExpr` nodes (because the
  7625. // EWC is placed on the outermost node of the expression, which this may not
  7626. // be), but, it still improves the coverage of this matcher.
  7627. if (const auto *CleanupsExpr = dyn_cast<ExprWithCleanups>(&Node))
  7628. E = CleanupsExpr->getSubExpr();
  7629. if (const auto *CtorExpr = dyn_cast<CXXConstructExpr>(E)) {
  7630. if (CtorExpr->isElidable()) {
  7631. if (const auto *MaterializeTemp =
  7632. dyn_cast<MaterializeTemporaryExpr>(CtorExpr->getArg(0))) {
  7633. return InnerMatcher.matches(*MaterializeTemp->getSubExpr(), Finder,
  7634. Builder);
  7635. }
  7636. }
  7637. }
  7638. return InnerMatcher.matches(Node, Finder, Builder);
  7639. }
  7640. //----------------------------------------------------------------------------//
  7641. // OpenMP handling.
  7642. //----------------------------------------------------------------------------//
  7643. /// Matches any ``#pragma omp`` executable directive.
  7644. ///
  7645. /// Given
  7646. ///
  7647. /// \code
  7648. /// #pragma omp parallel
  7649. /// #pragma omp parallel default(none)
  7650. /// #pragma omp taskyield
  7651. /// \endcode
  7652. ///
  7653. /// ``ompExecutableDirective()`` matches ``omp parallel``,
  7654. /// ``omp parallel default(none)`` and ``omp taskyield``.
  7655. extern const internal::VariadicDynCastAllOfMatcher<Stmt, OMPExecutableDirective>
  7656. ompExecutableDirective;
  7657. /// Matches standalone OpenMP directives,
  7658. /// i.e., directives that can't have a structured block.
  7659. ///
  7660. /// Given
  7661. ///
  7662. /// \code
  7663. /// #pragma omp parallel
  7664. /// {}
  7665. /// #pragma omp taskyield
  7666. /// \endcode
  7667. ///
  7668. /// ``ompExecutableDirective(isStandaloneDirective()))`` matches
  7669. /// ``omp taskyield``.
  7670. AST_MATCHER(OMPExecutableDirective, isStandaloneDirective) {
  7671. return Node.isStandaloneDirective();
  7672. }
  7673. /// Matches the structured-block of the OpenMP executable directive
  7674. ///
  7675. /// Prerequisite: the executable directive must not be standalone directive.
  7676. /// If it is, it will never match.
  7677. ///
  7678. /// Given
  7679. ///
  7680. /// \code
  7681. /// #pragma omp parallel
  7682. /// ;
  7683. /// #pragma omp parallel
  7684. /// {}
  7685. /// \endcode
  7686. ///
  7687. /// ``ompExecutableDirective(hasStructuredBlock(nullStmt()))`` will match ``;``
  7688. AST_MATCHER_P(OMPExecutableDirective, hasStructuredBlock,
  7689. internal::Matcher<Stmt>, InnerMatcher) {
  7690. if (Node.isStandaloneDirective())
  7691. return false; // Standalone directives have no structured blocks.
  7692. return InnerMatcher.matches(*Node.getStructuredBlock(), Finder, Builder);
  7693. }
  7694. /// Matches any clause in an OpenMP directive.
  7695. ///
  7696. /// Given
  7697. ///
  7698. /// \code
  7699. /// #pragma omp parallel
  7700. /// #pragma omp parallel default(none)
  7701. /// \endcode
  7702. ///
  7703. /// ``ompExecutableDirective(hasAnyClause(anything()))`` matches
  7704. /// ``omp parallel default(none)``.
  7705. AST_MATCHER_P(OMPExecutableDirective, hasAnyClause,
  7706. internal::Matcher<OMPClause>, InnerMatcher) {
  7707. ArrayRef<OMPClause *> Clauses = Node.clauses();
  7708. return matchesFirstInPointerRange(InnerMatcher, Clauses.begin(),
  7709. Clauses.end(), Finder,
  7710. Builder) != Clauses.end();
  7711. }
  7712. /// Matches OpenMP ``default`` clause.
  7713. ///
  7714. /// Given
  7715. ///
  7716. /// \code
  7717. /// #pragma omp parallel default(none)
  7718. /// #pragma omp parallel default(shared)
  7719. /// #pragma omp parallel default(firstprivate)
  7720. /// #pragma omp parallel
  7721. /// \endcode
  7722. ///
  7723. /// ``ompDefaultClause()`` matches ``default(none)``, ``default(shared)``, and
  7724. /// ``default(firstprivate)``
  7725. extern const internal::VariadicDynCastAllOfMatcher<OMPClause, OMPDefaultClause>
  7726. ompDefaultClause;
  7727. /// Matches if the OpenMP ``default`` clause has ``none`` kind specified.
  7728. ///
  7729. /// Given
  7730. ///
  7731. /// \code
  7732. /// #pragma omp parallel
  7733. /// #pragma omp parallel default(none)
  7734. /// #pragma omp parallel default(shared)
  7735. /// #pragma omp parallel default(firstprivate)
  7736. /// \endcode
  7737. ///
  7738. /// ``ompDefaultClause(isNoneKind())`` matches only ``default(none)``.
  7739. AST_MATCHER(OMPDefaultClause, isNoneKind) {
  7740. return Node.getDefaultKind() == llvm::omp::OMP_DEFAULT_none;
  7741. }
  7742. /// Matches if the OpenMP ``default`` clause has ``shared`` kind specified.
  7743. ///
  7744. /// Given
  7745. ///
  7746. /// \code
  7747. /// #pragma omp parallel
  7748. /// #pragma omp parallel default(none)
  7749. /// #pragma omp parallel default(shared)
  7750. /// #pragma omp parallel default(firstprivate)
  7751. /// \endcode
  7752. ///
  7753. /// ``ompDefaultClause(isSharedKind())`` matches only ``default(shared)``.
  7754. AST_MATCHER(OMPDefaultClause, isSharedKind) {
  7755. return Node.getDefaultKind() == llvm::omp::OMP_DEFAULT_shared;
  7756. }
  7757. /// Matches if the OpenMP ``default`` clause has ``firstprivate`` kind
  7758. /// specified.
  7759. ///
  7760. /// Given
  7761. ///
  7762. /// \code
  7763. /// #pragma omp parallel
  7764. /// #pragma omp parallel default(none)
  7765. /// #pragma omp parallel default(shared)
  7766. /// #pragma omp parallel default(firstprivate)
  7767. /// \endcode
  7768. ///
  7769. /// ``ompDefaultClause(isFirstPrivateKind())`` matches only
  7770. /// ``default(firstprivate)``.
  7771. AST_MATCHER(OMPDefaultClause, isFirstPrivateKind) {
  7772. return Node.getDefaultKind() == llvm::omp::OMP_DEFAULT_firstprivate;
  7773. }
  7774. /// Matches if the OpenMP directive is allowed to contain the specified OpenMP
  7775. /// clause kind.
  7776. ///
  7777. /// Given
  7778. ///
  7779. /// \code
  7780. /// #pragma omp parallel
  7781. /// #pragma omp parallel for
  7782. /// #pragma omp for
  7783. /// \endcode
  7784. ///
  7785. /// `ompExecutableDirective(isAllowedToContainClause(OMPC_default))`` matches
  7786. /// ``omp parallel`` and ``omp parallel for``.
  7787. ///
  7788. /// If the matcher is use from clang-query, ``OpenMPClauseKind`` parameter
  7789. /// should be passed as a quoted string. e.g.,
  7790. /// ``isAllowedToContainClauseKind("OMPC_default").``
  7791. AST_MATCHER_P(OMPExecutableDirective, isAllowedToContainClauseKind,
  7792. OpenMPClauseKind, CKind) {
  7793. return llvm::omp::isAllowedClauseForDirective(
  7794. Node.getDirectiveKind(), CKind,
  7795. Finder->getASTContext().getLangOpts().OpenMP);
  7796. }
  7797. //----------------------------------------------------------------------------//
  7798. // End OpenMP handling.
  7799. //----------------------------------------------------------------------------//
  7800. } // namespace ast_matchers
  7801. } // namespace clang
  7802. #endif // LLVM_CLANG_ASTMATCHERS_ASTMATCHERS_H
  7803. #ifdef __GNUC__
  7804. #pragma GCC diagnostic pop
  7805. #endif