PPDirectives.cpp 134 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488
  1. //===--- PPDirectives.cpp - Directive Handling for Preprocessor -----------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. ///
  9. /// \file
  10. /// Implements # directive processing for the Preprocessor.
  11. ///
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/Basic/CharInfo.h"
  14. #include "clang/Basic/FileManager.h"
  15. #include "clang/Basic/IdentifierTable.h"
  16. #include "clang/Basic/LangOptions.h"
  17. #include "clang/Basic/Module.h"
  18. #include "clang/Basic/SourceLocation.h"
  19. #include "clang/Basic/SourceManager.h"
  20. #include "clang/Basic/TokenKinds.h"
  21. #include "clang/Lex/CodeCompletionHandler.h"
  22. #include "clang/Lex/HeaderSearch.h"
  23. #include "clang/Lex/LexDiagnostic.h"
  24. #include "clang/Lex/LiteralSupport.h"
  25. #include "clang/Lex/MacroInfo.h"
  26. #include "clang/Lex/ModuleLoader.h"
  27. #include "clang/Lex/ModuleMap.h"
  28. #include "clang/Lex/PPCallbacks.h"
  29. #include "clang/Lex/Pragma.h"
  30. #include "clang/Lex/Preprocessor.h"
  31. #include "clang/Lex/PreprocessorOptions.h"
  32. #include "clang/Lex/Token.h"
  33. #include "clang/Lex/VariadicMacroSupport.h"
  34. #include "llvm/ADT/ArrayRef.h"
  35. #include "llvm/ADT/STLExtras.h"
  36. #include "llvm/ADT/ScopeExit.h"
  37. #include "llvm/ADT/SmallString.h"
  38. #include "llvm/ADT/SmallVector.h"
  39. #include "llvm/ADT/StringRef.h"
  40. #include "llvm/ADT/StringSwitch.h"
  41. #include "llvm/Support/AlignOf.h"
  42. #include "llvm/Support/ErrorHandling.h"
  43. #include "llvm/Support/Path.h"
  44. #include "llvm/Support/SaveAndRestore.h"
  45. #include <algorithm>
  46. #include <cassert>
  47. #include <cstring>
  48. #include <new>
  49. #include <optional>
  50. #include <string>
  51. #include <utility>
  52. using namespace clang;
  53. //===----------------------------------------------------------------------===//
  54. // Utility Methods for Preprocessor Directive Handling.
  55. //===----------------------------------------------------------------------===//
  56. MacroInfo *Preprocessor::AllocateMacroInfo(SourceLocation L) {
  57. static_assert(std::is_trivially_destructible_v<MacroInfo>, "");
  58. return new (BP) MacroInfo(L);
  59. }
  60. DefMacroDirective *Preprocessor::AllocateDefMacroDirective(MacroInfo *MI,
  61. SourceLocation Loc) {
  62. return new (BP) DefMacroDirective(MI, Loc);
  63. }
  64. UndefMacroDirective *
  65. Preprocessor::AllocateUndefMacroDirective(SourceLocation UndefLoc) {
  66. return new (BP) UndefMacroDirective(UndefLoc);
  67. }
  68. VisibilityMacroDirective *
  69. Preprocessor::AllocateVisibilityMacroDirective(SourceLocation Loc,
  70. bool isPublic) {
  71. return new (BP) VisibilityMacroDirective(Loc, isPublic);
  72. }
  73. /// Read and discard all tokens remaining on the current line until
  74. /// the tok::eod token is found.
  75. SourceRange Preprocessor::DiscardUntilEndOfDirective() {
  76. Token Tmp;
  77. SourceRange Res;
  78. LexUnexpandedToken(Tmp);
  79. Res.setBegin(Tmp.getLocation());
  80. while (Tmp.isNot(tok::eod)) {
  81. assert(Tmp.isNot(tok::eof) && "EOF seen while discarding directive tokens");
  82. LexUnexpandedToken(Tmp);
  83. }
  84. Res.setEnd(Tmp.getLocation());
  85. return Res;
  86. }
  87. /// Enumerates possible cases of #define/#undef a reserved identifier.
  88. enum MacroDiag {
  89. MD_NoWarn, //> Not a reserved identifier
  90. MD_KeywordDef, //> Macro hides keyword, enabled by default
  91. MD_ReservedMacro //> #define of #undef reserved id, disabled by default
  92. };
  93. /// Enumerates possible %select values for the pp_err_elif_after_else and
  94. /// pp_err_elif_without_if diagnostics.
  95. enum PPElifDiag {
  96. PED_Elif,
  97. PED_Elifdef,
  98. PED_Elifndef
  99. };
  100. static MacroDiag shouldWarnOnMacroDef(Preprocessor &PP, IdentifierInfo *II) {
  101. const LangOptions &Lang = PP.getLangOpts();
  102. if (isReservedInAllContexts(II->isReserved(Lang))) {
  103. // list from:
  104. // - https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_macros.html
  105. // - https://docs.microsoft.com/en-us/cpp/c-runtime-library/security-features-in-the-crt?view=msvc-160
  106. // - man 7 feature_test_macros
  107. // The list must be sorted for correct binary search.
  108. static constexpr StringRef ReservedMacro[] = {
  109. "_ATFILE_SOURCE",
  110. "_BSD_SOURCE",
  111. "_CRT_NONSTDC_NO_WARNINGS",
  112. "_CRT_SECURE_CPP_OVERLOAD_STANDARD_NAMES",
  113. "_CRT_SECURE_NO_WARNINGS",
  114. "_FILE_OFFSET_BITS",
  115. "_FORTIFY_SOURCE",
  116. "_GLIBCXX_ASSERTIONS",
  117. "_GLIBCXX_CONCEPT_CHECKS",
  118. "_GLIBCXX_DEBUG",
  119. "_GLIBCXX_DEBUG_PEDANTIC",
  120. "_GLIBCXX_PARALLEL",
  121. "_GLIBCXX_PARALLEL_ASSERTIONS",
  122. "_GLIBCXX_SANITIZE_VECTOR",
  123. "_GLIBCXX_USE_CXX11_ABI",
  124. "_GLIBCXX_USE_DEPRECATED",
  125. "_GNU_SOURCE",
  126. "_ISOC11_SOURCE",
  127. "_ISOC95_SOURCE",
  128. "_ISOC99_SOURCE",
  129. "_LARGEFILE64_SOURCE",
  130. "_POSIX_C_SOURCE",
  131. "_REENTRANT",
  132. "_SVID_SOURCE",
  133. "_THREAD_SAFE",
  134. "_XOPEN_SOURCE",
  135. "_XOPEN_SOURCE_EXTENDED",
  136. "__STDCPP_WANT_MATH_SPEC_FUNCS__",
  137. "__STDC_FORMAT_MACROS",
  138. };
  139. if (std::binary_search(std::begin(ReservedMacro), std::end(ReservedMacro),
  140. II->getName()))
  141. return MD_NoWarn;
  142. return MD_ReservedMacro;
  143. }
  144. StringRef Text = II->getName();
  145. if (II->isKeyword(Lang))
  146. return MD_KeywordDef;
  147. if (Lang.CPlusPlus11 && (Text.equals("override") || Text.equals("final")))
  148. return MD_KeywordDef;
  149. return MD_NoWarn;
  150. }
  151. static MacroDiag shouldWarnOnMacroUndef(Preprocessor &PP, IdentifierInfo *II) {
  152. const LangOptions &Lang = PP.getLangOpts();
  153. // Do not warn on keyword undef. It is generally harmless and widely used.
  154. if (isReservedInAllContexts(II->isReserved(Lang)))
  155. return MD_ReservedMacro;
  156. return MD_NoWarn;
  157. }
  158. // Return true if we want to issue a diagnostic by default if we
  159. // encounter this name in a #include with the wrong case. For now,
  160. // this includes the standard C and C++ headers, Posix headers,
  161. // and Boost headers. Improper case for these #includes is a
  162. // potential portability issue.
  163. static bool warnByDefaultOnWrongCase(StringRef Include) {
  164. // If the first component of the path is "boost", treat this like a standard header
  165. // for the purposes of diagnostics.
  166. if (::llvm::sys::path::begin(Include)->equals_insensitive("boost"))
  167. return true;
  168. // "condition_variable" is the longest standard header name at 18 characters.
  169. // If the include file name is longer than that, it can't be a standard header.
  170. static const size_t MaxStdHeaderNameLen = 18u;
  171. if (Include.size() > MaxStdHeaderNameLen)
  172. return false;
  173. // Lowercase and normalize the search string.
  174. SmallString<32> LowerInclude{Include};
  175. for (char &Ch : LowerInclude) {
  176. // In the ASCII range?
  177. if (static_cast<unsigned char>(Ch) > 0x7f)
  178. return false; // Can't be a standard header
  179. // ASCII lowercase:
  180. if (Ch >= 'A' && Ch <= 'Z')
  181. Ch += 'a' - 'A';
  182. // Normalize path separators for comparison purposes.
  183. else if (::llvm::sys::path::is_separator(Ch))
  184. Ch = '/';
  185. }
  186. // The standard C/C++ and Posix headers
  187. return llvm::StringSwitch<bool>(LowerInclude)
  188. // C library headers
  189. .Cases("assert.h", "complex.h", "ctype.h", "errno.h", "fenv.h", true)
  190. .Cases("float.h", "inttypes.h", "iso646.h", "limits.h", "locale.h", true)
  191. .Cases("math.h", "setjmp.h", "signal.h", "stdalign.h", "stdarg.h", true)
  192. .Cases("stdatomic.h", "stdbool.h", "stddef.h", "stdint.h", "stdio.h", true)
  193. .Cases("stdlib.h", "stdnoreturn.h", "string.h", "tgmath.h", "threads.h", true)
  194. .Cases("time.h", "uchar.h", "wchar.h", "wctype.h", true)
  195. // C++ headers for C library facilities
  196. .Cases("cassert", "ccomplex", "cctype", "cerrno", "cfenv", true)
  197. .Cases("cfloat", "cinttypes", "ciso646", "climits", "clocale", true)
  198. .Cases("cmath", "csetjmp", "csignal", "cstdalign", "cstdarg", true)
  199. .Cases("cstdbool", "cstddef", "cstdint", "cstdio", "cstdlib", true)
  200. .Cases("cstring", "ctgmath", "ctime", "cuchar", "cwchar", true)
  201. .Case("cwctype", true)
  202. // C++ library headers
  203. .Cases("algorithm", "fstream", "list", "regex", "thread", true)
  204. .Cases("array", "functional", "locale", "scoped_allocator", "tuple", true)
  205. .Cases("atomic", "future", "map", "set", "type_traits", true)
  206. .Cases("bitset", "initializer_list", "memory", "shared_mutex", "typeindex", true)
  207. .Cases("chrono", "iomanip", "mutex", "sstream", "typeinfo", true)
  208. .Cases("codecvt", "ios", "new", "stack", "unordered_map", true)
  209. .Cases("complex", "iosfwd", "numeric", "stdexcept", "unordered_set", true)
  210. .Cases("condition_variable", "iostream", "ostream", "streambuf", "utility", true)
  211. .Cases("deque", "istream", "queue", "string", "valarray", true)
  212. .Cases("exception", "iterator", "random", "strstream", "vector", true)
  213. .Cases("forward_list", "limits", "ratio", "system_error", true)
  214. // POSIX headers (which aren't also C headers)
  215. .Cases("aio.h", "arpa/inet.h", "cpio.h", "dirent.h", "dlfcn.h", true)
  216. .Cases("fcntl.h", "fmtmsg.h", "fnmatch.h", "ftw.h", "glob.h", true)
  217. .Cases("grp.h", "iconv.h", "langinfo.h", "libgen.h", "monetary.h", true)
  218. .Cases("mqueue.h", "ndbm.h", "net/if.h", "netdb.h", "netinet/in.h", true)
  219. .Cases("netinet/tcp.h", "nl_types.h", "poll.h", "pthread.h", "pwd.h", true)
  220. .Cases("regex.h", "sched.h", "search.h", "semaphore.h", "spawn.h", true)
  221. .Cases("strings.h", "stropts.h", "sys/ipc.h", "sys/mman.h", "sys/msg.h", true)
  222. .Cases("sys/resource.h", "sys/select.h", "sys/sem.h", "sys/shm.h", "sys/socket.h", true)
  223. .Cases("sys/stat.h", "sys/statvfs.h", "sys/time.h", "sys/times.h", "sys/types.h", true)
  224. .Cases("sys/uio.h", "sys/un.h", "sys/utsname.h", "sys/wait.h", "syslog.h", true)
  225. .Cases("tar.h", "termios.h", "trace.h", "ulimit.h", true)
  226. .Cases("unistd.h", "utime.h", "utmpx.h", "wordexp.h", true)
  227. .Default(false);
  228. }
  229. /// Find a similar string in `Candidates`.
  230. ///
  231. /// \param LHS a string for a similar string in `Candidates`
  232. ///
  233. /// \param Candidates the candidates to find a similar string.
  234. ///
  235. /// \returns a similar string if exists. If no similar string exists,
  236. /// returns std::nullopt.
  237. static std::optional<StringRef>
  238. findSimilarStr(StringRef LHS, const std::vector<StringRef> &Candidates) {
  239. // We need to check if `Candidates` has the exact case-insensitive string
  240. // because the Levenshtein distance match does not care about it.
  241. for (StringRef C : Candidates) {
  242. if (LHS.equals_insensitive(C)) {
  243. return C;
  244. }
  245. }
  246. // Keep going with the Levenshtein distance match.
  247. // If the LHS size is less than 3, use the LHS size minus 1 and if not,
  248. // use the LHS size divided by 3.
  249. size_t Length = LHS.size();
  250. size_t MaxDist = Length < 3 ? Length - 1 : Length / 3;
  251. std::optional<std::pair<StringRef, size_t>> SimilarStr;
  252. for (StringRef C : Candidates) {
  253. size_t CurDist = LHS.edit_distance(C, true);
  254. if (CurDist <= MaxDist) {
  255. if (!SimilarStr) {
  256. // The first similar string found.
  257. SimilarStr = {C, CurDist};
  258. } else if (CurDist < SimilarStr->second) {
  259. // More similar string found.
  260. SimilarStr = {C, CurDist};
  261. }
  262. }
  263. }
  264. if (SimilarStr) {
  265. return SimilarStr->first;
  266. } else {
  267. return std::nullopt;
  268. }
  269. }
  270. bool Preprocessor::CheckMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
  271. bool *ShadowFlag) {
  272. // Missing macro name?
  273. if (MacroNameTok.is(tok::eod))
  274. return Diag(MacroNameTok, diag::err_pp_missing_macro_name);
  275. IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
  276. if (!II)
  277. return Diag(MacroNameTok, diag::err_pp_macro_not_identifier);
  278. if (II->isCPlusPlusOperatorKeyword()) {
  279. // C++ 2.5p2: Alternative tokens behave the same as its primary token
  280. // except for their spellings.
  281. Diag(MacroNameTok, getLangOpts().MicrosoftExt
  282. ? diag::ext_pp_operator_used_as_macro_name
  283. : diag::err_pp_operator_used_as_macro_name)
  284. << II << MacroNameTok.getKind();
  285. // Allow #defining |and| and friends for Microsoft compatibility or
  286. // recovery when legacy C headers are included in C++.
  287. }
  288. if ((isDefineUndef != MU_Other) && II->getPPKeywordID() == tok::pp_defined) {
  289. // Error if defining "defined": C99 6.10.8/4, C++ [cpp.predefined]p4.
  290. return Diag(MacroNameTok, diag::err_defined_macro_name);
  291. }
  292. if (isDefineUndef == MU_Undef) {
  293. auto *MI = getMacroInfo(II);
  294. if (MI && MI->isBuiltinMacro()) {
  295. // Warn if undefining "__LINE__" and other builtins, per C99 6.10.8/4
  296. // and C++ [cpp.predefined]p4], but allow it as an extension.
  297. Diag(MacroNameTok, diag::ext_pp_undef_builtin_macro);
  298. }
  299. }
  300. // If defining/undefining reserved identifier or a keyword, we need to issue
  301. // a warning.
  302. SourceLocation MacroNameLoc = MacroNameTok.getLocation();
  303. if (ShadowFlag)
  304. *ShadowFlag = false;
  305. if (!SourceMgr.isInSystemHeader(MacroNameLoc) &&
  306. (SourceMgr.getBufferName(MacroNameLoc) != "<built-in>")) {
  307. MacroDiag D = MD_NoWarn;
  308. if (isDefineUndef == MU_Define) {
  309. D = shouldWarnOnMacroDef(*this, II);
  310. }
  311. else if (isDefineUndef == MU_Undef)
  312. D = shouldWarnOnMacroUndef(*this, II);
  313. if (D == MD_KeywordDef) {
  314. // We do not want to warn on some patterns widely used in configuration
  315. // scripts. This requires analyzing next tokens, so do not issue warnings
  316. // now, only inform caller.
  317. if (ShadowFlag)
  318. *ShadowFlag = true;
  319. }
  320. if (D == MD_ReservedMacro)
  321. Diag(MacroNameTok, diag::warn_pp_macro_is_reserved_id);
  322. }
  323. // Okay, we got a good identifier.
  324. return false;
  325. }
  326. /// Lex and validate a macro name, which occurs after a
  327. /// \#define or \#undef.
  328. ///
  329. /// This sets the token kind to eod and discards the rest of the macro line if
  330. /// the macro name is invalid.
  331. ///
  332. /// \param MacroNameTok Token that is expected to be a macro name.
  333. /// \param isDefineUndef Context in which macro is used.
  334. /// \param ShadowFlag Points to a flag that is set if macro shadows a keyword.
  335. void Preprocessor::ReadMacroName(Token &MacroNameTok, MacroUse isDefineUndef,
  336. bool *ShadowFlag) {
  337. // Read the token, don't allow macro expansion on it.
  338. LexUnexpandedToken(MacroNameTok);
  339. if (MacroNameTok.is(tok::code_completion)) {
  340. if (CodeComplete)
  341. CodeComplete->CodeCompleteMacroName(isDefineUndef == MU_Define);
  342. setCodeCompletionReached();
  343. LexUnexpandedToken(MacroNameTok);
  344. }
  345. if (!CheckMacroName(MacroNameTok, isDefineUndef, ShadowFlag))
  346. return;
  347. // Invalid macro name, read and discard the rest of the line and set the
  348. // token kind to tok::eod if necessary.
  349. if (MacroNameTok.isNot(tok::eod)) {
  350. MacroNameTok.setKind(tok::eod);
  351. DiscardUntilEndOfDirective();
  352. }
  353. }
  354. /// Ensure that the next token is a tok::eod token.
  355. ///
  356. /// If not, emit a diagnostic and consume up until the eod. If EnableMacros is
  357. /// true, then we consider macros that expand to zero tokens as being ok.
  358. ///
  359. /// Returns the location of the end of the directive.
  360. SourceLocation Preprocessor::CheckEndOfDirective(const char *DirType,
  361. bool EnableMacros) {
  362. Token Tmp;
  363. // Lex unexpanded tokens for most directives: macros might expand to zero
  364. // tokens, causing us to miss diagnosing invalid lines. Some directives (like
  365. // #line) allow empty macros.
  366. if (EnableMacros)
  367. Lex(Tmp);
  368. else
  369. LexUnexpandedToken(Tmp);
  370. // There should be no tokens after the directive, but we allow them as an
  371. // extension.
  372. while (Tmp.is(tok::comment)) // Skip comments in -C mode.
  373. LexUnexpandedToken(Tmp);
  374. if (Tmp.is(tok::eod))
  375. return Tmp.getLocation();
  376. // Add a fixit in GNU/C99/C++ mode. Don't offer a fixit for strict-C89,
  377. // or if this is a macro-style preprocessing directive, because it is more
  378. // trouble than it is worth to insert /**/ and check that there is no /**/
  379. // in the range also.
  380. FixItHint Hint;
  381. if ((LangOpts.GNUMode || LangOpts.C99 || LangOpts.CPlusPlus) &&
  382. !CurTokenLexer)
  383. Hint = FixItHint::CreateInsertion(Tmp.getLocation(),"//");
  384. Diag(Tmp, diag::ext_pp_extra_tokens_at_eol) << DirType << Hint;
  385. return DiscardUntilEndOfDirective().getEnd();
  386. }
  387. void Preprocessor::SuggestTypoedDirective(const Token &Tok,
  388. StringRef Directive) const {
  389. // If this is a `.S` file, treat unknown # directives as non-preprocessor
  390. // directives.
  391. if (getLangOpts().AsmPreprocessor) return;
  392. std::vector<StringRef> Candidates = {
  393. "if", "ifdef", "ifndef", "elif", "else", "endif"
  394. };
  395. if (LangOpts.C2x || LangOpts.CPlusPlus2b)
  396. Candidates.insert(Candidates.end(), {"elifdef", "elifndef"});
  397. if (std::optional<StringRef> Sugg = findSimilarStr(Directive, Candidates)) {
  398. // Directive cannot be coming from macro.
  399. assert(Tok.getLocation().isFileID());
  400. CharSourceRange DirectiveRange = CharSourceRange::getCharRange(
  401. Tok.getLocation(),
  402. Tok.getLocation().getLocWithOffset(Directive.size()));
  403. StringRef SuggValue = *Sugg;
  404. auto Hint = FixItHint::CreateReplacement(DirectiveRange, SuggValue);
  405. Diag(Tok, diag::warn_pp_invalid_directive) << 1 << SuggValue << Hint;
  406. }
  407. }
  408. /// SkipExcludedConditionalBlock - We just read a \#if or related directive and
  409. /// decided that the subsequent tokens are in the \#if'd out portion of the
  410. /// file. Lex the rest of the file, until we see an \#endif. If
  411. /// FoundNonSkipPortion is true, then we have already emitted code for part of
  412. /// this \#if directive, so \#else/\#elif blocks should never be entered.
  413. /// If ElseOk is true, then \#else directives are ok, if not, then we have
  414. /// already seen one so a \#else directive is a duplicate. When this returns,
  415. /// the caller can lex the first valid token.
  416. void Preprocessor::SkipExcludedConditionalBlock(SourceLocation HashTokenLoc,
  417. SourceLocation IfTokenLoc,
  418. bool FoundNonSkipPortion,
  419. bool FoundElse,
  420. SourceLocation ElseLoc) {
  421. // In SkippingRangeStateTy we are depending on SkipExcludedConditionalBlock()
  422. // not getting called recursively by storing the RecordedSkippedRanges
  423. // DenseMap lookup pointer (field SkipRangePtr). SkippingRangeStateTy expects
  424. // that RecordedSkippedRanges won't get modified and SkipRangePtr won't be
  425. // invalidated. If this changes and there is a need to call
  426. // SkipExcludedConditionalBlock() recursively, SkippingRangeStateTy should
  427. // change to do a second lookup in endLexPass function instead of reusing the
  428. // lookup pointer.
  429. assert(!SkippingExcludedConditionalBlock &&
  430. "calling SkipExcludedConditionalBlock recursively");
  431. llvm::SaveAndRestore SARSkipping(SkippingExcludedConditionalBlock, true);
  432. ++NumSkipped;
  433. assert(!CurTokenLexer && CurPPLexer && "Lexing a macro, not a file?");
  434. if (PreambleConditionalStack.reachedEOFWhileSkipping())
  435. PreambleConditionalStack.clearSkipInfo();
  436. else
  437. CurPPLexer->pushConditionalLevel(IfTokenLoc, /*isSkipping*/ false,
  438. FoundNonSkipPortion, FoundElse);
  439. // Enter raw mode to disable identifier lookup (and thus macro expansion),
  440. // disabling warnings, etc.
  441. CurPPLexer->LexingRawMode = true;
  442. Token Tok;
  443. SourceLocation endLoc;
  444. /// Keeps track and caches skipped ranges and also retrieves a prior skipped
  445. /// range if the same block is re-visited.
  446. struct SkippingRangeStateTy {
  447. Preprocessor &PP;
  448. const char *BeginPtr = nullptr;
  449. unsigned *SkipRangePtr = nullptr;
  450. SkippingRangeStateTy(Preprocessor &PP) : PP(PP) {}
  451. void beginLexPass() {
  452. if (BeginPtr)
  453. return; // continue skipping a block.
  454. // Initiate a skipping block and adjust the lexer if we already skipped it
  455. // before.
  456. BeginPtr = PP.CurLexer->getBufferLocation();
  457. SkipRangePtr = &PP.RecordedSkippedRanges[BeginPtr];
  458. if (*SkipRangePtr) {
  459. PP.CurLexer->seek(PP.CurLexer->getCurrentBufferOffset() + *SkipRangePtr,
  460. /*IsAtStartOfLine*/ true);
  461. }
  462. }
  463. void endLexPass(const char *Hashptr) {
  464. if (!BeginPtr) {
  465. // Not doing normal lexing.
  466. assert(PP.CurLexer->isDependencyDirectivesLexer());
  467. return;
  468. }
  469. // Finished skipping a block, record the range if it's first time visited.
  470. if (!*SkipRangePtr) {
  471. *SkipRangePtr = Hashptr - BeginPtr;
  472. }
  473. assert(*SkipRangePtr == Hashptr - BeginPtr);
  474. BeginPtr = nullptr;
  475. SkipRangePtr = nullptr;
  476. }
  477. } SkippingRangeState(*this);
  478. while (true) {
  479. if (CurLexer->isDependencyDirectivesLexer()) {
  480. CurLexer->LexDependencyDirectiveTokenWhileSkipping(Tok);
  481. } else {
  482. SkippingRangeState.beginLexPass();
  483. while (true) {
  484. CurLexer->Lex(Tok);
  485. if (Tok.is(tok::code_completion)) {
  486. setCodeCompletionReached();
  487. if (CodeComplete)
  488. CodeComplete->CodeCompleteInConditionalExclusion();
  489. continue;
  490. }
  491. // If this is the end of the buffer, we have an error.
  492. if (Tok.is(tok::eof)) {
  493. // We don't emit errors for unterminated conditionals here,
  494. // Lexer::LexEndOfFile can do that properly.
  495. // Just return and let the caller lex after this #include.
  496. if (PreambleConditionalStack.isRecording())
  497. PreambleConditionalStack.SkipInfo.emplace(HashTokenLoc, IfTokenLoc,
  498. FoundNonSkipPortion,
  499. FoundElse, ElseLoc);
  500. break;
  501. }
  502. // If this token is not a preprocessor directive, just skip it.
  503. if (Tok.isNot(tok::hash) || !Tok.isAtStartOfLine())
  504. continue;
  505. break;
  506. }
  507. }
  508. if (Tok.is(tok::eof))
  509. break;
  510. // We just parsed a # character at the start of a line, so we're in
  511. // directive mode. Tell the lexer this so any newlines we see will be
  512. // converted into an EOD token (this terminates the macro).
  513. CurPPLexer->ParsingPreprocessorDirective = true;
  514. if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
  515. assert(Tok.is(tok::hash));
  516. const char *Hashptr = CurLexer->getBufferLocation() - Tok.getLength();
  517. assert(CurLexer->getSourceLocation(Hashptr) == Tok.getLocation());
  518. // Read the next token, the directive flavor.
  519. LexUnexpandedToken(Tok);
  520. // If this isn't an identifier directive (e.g. is "# 1\n" or "#\n", or
  521. // something bogus), skip it.
  522. if (Tok.isNot(tok::raw_identifier)) {
  523. CurPPLexer->ParsingPreprocessorDirective = false;
  524. // Restore comment saving mode.
  525. if (CurLexer) CurLexer->resetExtendedTokenMode();
  526. continue;
  527. }
  528. // If the first letter isn't i or e, it isn't intesting to us. We know that
  529. // this is safe in the face of spelling differences, because there is no way
  530. // to spell an i/e in a strange way that is another letter. Skipping this
  531. // allows us to avoid looking up the identifier info for #define/#undef and
  532. // other common directives.
  533. StringRef RI = Tok.getRawIdentifier();
  534. char FirstChar = RI[0];
  535. if (FirstChar >= 'a' && FirstChar <= 'z' &&
  536. FirstChar != 'i' && FirstChar != 'e') {
  537. CurPPLexer->ParsingPreprocessorDirective = false;
  538. // Restore comment saving mode.
  539. if (CurLexer) CurLexer->resetExtendedTokenMode();
  540. continue;
  541. }
  542. // Get the identifier name without trigraphs or embedded newlines. Note
  543. // that we can't use Tok.getIdentifierInfo() because its lookup is disabled
  544. // when skipping.
  545. char DirectiveBuf[20];
  546. StringRef Directive;
  547. if (!Tok.needsCleaning() && RI.size() < 20) {
  548. Directive = RI;
  549. } else {
  550. std::string DirectiveStr = getSpelling(Tok);
  551. size_t IdLen = DirectiveStr.size();
  552. if (IdLen >= 20) {
  553. CurPPLexer->ParsingPreprocessorDirective = false;
  554. // Restore comment saving mode.
  555. if (CurLexer) CurLexer->resetExtendedTokenMode();
  556. continue;
  557. }
  558. memcpy(DirectiveBuf, &DirectiveStr[0], IdLen);
  559. Directive = StringRef(DirectiveBuf, IdLen);
  560. }
  561. if (Directive.startswith("if")) {
  562. StringRef Sub = Directive.substr(2);
  563. if (Sub.empty() || // "if"
  564. Sub == "def" || // "ifdef"
  565. Sub == "ndef") { // "ifndef"
  566. // We know the entire #if/#ifdef/#ifndef block will be skipped, don't
  567. // bother parsing the condition.
  568. DiscardUntilEndOfDirective();
  569. CurPPLexer->pushConditionalLevel(Tok.getLocation(), /*wasskipping*/true,
  570. /*foundnonskip*/false,
  571. /*foundelse*/false);
  572. } else {
  573. SuggestTypoedDirective(Tok, Directive);
  574. }
  575. } else if (Directive[0] == 'e') {
  576. StringRef Sub = Directive.substr(1);
  577. if (Sub == "ndif") { // "endif"
  578. PPConditionalInfo CondInfo;
  579. CondInfo.WasSkipping = true; // Silence bogus warning.
  580. bool InCond = CurPPLexer->popConditionalLevel(CondInfo);
  581. (void)InCond; // Silence warning in no-asserts mode.
  582. assert(!InCond && "Can't be skipping if not in a conditional!");
  583. // If we popped the outermost skipping block, we're done skipping!
  584. if (!CondInfo.WasSkipping) {
  585. SkippingRangeState.endLexPass(Hashptr);
  586. // Restore the value of LexingRawMode so that trailing comments
  587. // are handled correctly, if we've reached the outermost block.
  588. CurPPLexer->LexingRawMode = false;
  589. endLoc = CheckEndOfDirective("endif");
  590. CurPPLexer->LexingRawMode = true;
  591. if (Callbacks)
  592. Callbacks->Endif(Tok.getLocation(), CondInfo.IfLoc);
  593. break;
  594. } else {
  595. DiscardUntilEndOfDirective();
  596. }
  597. } else if (Sub == "lse") { // "else".
  598. // #else directive in a skipping conditional. If not in some other
  599. // skipping conditional, and if #else hasn't already been seen, enter it
  600. // as a non-skipping conditional.
  601. PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
  602. if (!CondInfo.WasSkipping)
  603. SkippingRangeState.endLexPass(Hashptr);
  604. // If this is a #else with a #else before it, report the error.
  605. if (CondInfo.FoundElse)
  606. Diag(Tok, diag::pp_err_else_after_else);
  607. // Note that we've seen a #else in this conditional.
  608. CondInfo.FoundElse = true;
  609. // If the conditional is at the top level, and the #if block wasn't
  610. // entered, enter the #else block now.
  611. if (!CondInfo.WasSkipping && !CondInfo.FoundNonSkip) {
  612. CondInfo.FoundNonSkip = true;
  613. // Restore the value of LexingRawMode so that trailing comments
  614. // are handled correctly.
  615. CurPPLexer->LexingRawMode = false;
  616. endLoc = CheckEndOfDirective("else");
  617. CurPPLexer->LexingRawMode = true;
  618. if (Callbacks)
  619. Callbacks->Else(Tok.getLocation(), CondInfo.IfLoc);
  620. break;
  621. } else {
  622. DiscardUntilEndOfDirective(); // C99 6.10p4.
  623. }
  624. } else if (Sub == "lif") { // "elif".
  625. PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
  626. if (!CondInfo.WasSkipping)
  627. SkippingRangeState.endLexPass(Hashptr);
  628. // If this is a #elif with a #else before it, report the error.
  629. if (CondInfo.FoundElse)
  630. Diag(Tok, diag::pp_err_elif_after_else) << PED_Elif;
  631. // If this is in a skipping block or if we're already handled this #if
  632. // block, don't bother parsing the condition.
  633. if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
  634. // FIXME: We should probably do at least some minimal parsing of the
  635. // condition to verify that it is well-formed. The current state
  636. // allows #elif* directives with completely malformed (or missing)
  637. // conditions.
  638. DiscardUntilEndOfDirective();
  639. } else {
  640. // Restore the value of LexingRawMode so that identifiers are
  641. // looked up, etc, inside the #elif expression.
  642. assert(CurPPLexer->LexingRawMode && "We have to be skipping here!");
  643. CurPPLexer->LexingRawMode = false;
  644. IdentifierInfo *IfNDefMacro = nullptr;
  645. DirectiveEvalResult DER = EvaluateDirectiveExpression(IfNDefMacro);
  646. // Stop if Lexer became invalid after hitting code completion token.
  647. if (!CurPPLexer)
  648. return;
  649. const bool CondValue = DER.Conditional;
  650. CurPPLexer->LexingRawMode = true;
  651. if (Callbacks) {
  652. Callbacks->Elif(
  653. Tok.getLocation(), DER.ExprRange,
  654. (CondValue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False),
  655. CondInfo.IfLoc);
  656. }
  657. // If this condition is true, enter it!
  658. if (CondValue) {
  659. CondInfo.FoundNonSkip = true;
  660. break;
  661. }
  662. }
  663. } else if (Sub == "lifdef" || // "elifdef"
  664. Sub == "lifndef") { // "elifndef"
  665. bool IsElifDef = Sub == "lifdef";
  666. PPConditionalInfo &CondInfo = CurPPLexer->peekConditionalLevel();
  667. Token DirectiveToken = Tok;
  668. if (!CondInfo.WasSkipping)
  669. SkippingRangeState.endLexPass(Hashptr);
  670. // Warn if using `#elifdef` & `#elifndef` in not C2x & C++2b mode even
  671. // if this branch is in a skipping block.
  672. unsigned DiagID;
  673. if (LangOpts.CPlusPlus)
  674. DiagID = LangOpts.CPlusPlus2b ? diag::warn_cxx2b_compat_pp_directive
  675. : diag::ext_cxx2b_pp_directive;
  676. else
  677. DiagID = LangOpts.C2x ? diag::warn_c2x_compat_pp_directive
  678. : diag::ext_c2x_pp_directive;
  679. Diag(Tok, DiagID) << (IsElifDef ? PED_Elifdef : PED_Elifndef);
  680. // If this is a #elif with a #else before it, report the error.
  681. if (CondInfo.FoundElse)
  682. Diag(Tok, diag::pp_err_elif_after_else)
  683. << (IsElifDef ? PED_Elifdef : PED_Elifndef);
  684. // If this is in a skipping block or if we're already handled this #if
  685. // block, don't bother parsing the condition.
  686. if (CondInfo.WasSkipping || CondInfo.FoundNonSkip) {
  687. // FIXME: We should probably do at least some minimal parsing of the
  688. // condition to verify that it is well-formed. The current state
  689. // allows #elif* directives with completely malformed (or missing)
  690. // conditions.
  691. DiscardUntilEndOfDirective();
  692. } else {
  693. // Restore the value of LexingRawMode so that identifiers are
  694. // looked up, etc, inside the #elif[n]def expression.
  695. assert(CurPPLexer->LexingRawMode && "We have to be skipping here!");
  696. CurPPLexer->LexingRawMode = false;
  697. Token MacroNameTok;
  698. ReadMacroName(MacroNameTok);
  699. CurPPLexer->LexingRawMode = true;
  700. // If the macro name token is tok::eod, there was an error that was
  701. // already reported.
  702. if (MacroNameTok.is(tok::eod)) {
  703. // Skip code until we get to #endif. This helps with recovery by
  704. // not emitting an error when the #endif is reached.
  705. continue;
  706. }
  707. emitMacroExpansionWarnings(MacroNameTok);
  708. CheckEndOfDirective(IsElifDef ? "elifdef" : "elifndef");
  709. IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
  710. auto MD = getMacroDefinition(MII);
  711. MacroInfo *MI = MD.getMacroInfo();
  712. if (Callbacks) {
  713. if (IsElifDef) {
  714. Callbacks->Elifdef(DirectiveToken.getLocation(), MacroNameTok,
  715. MD);
  716. } else {
  717. Callbacks->Elifndef(DirectiveToken.getLocation(), MacroNameTok,
  718. MD);
  719. }
  720. }
  721. // If this condition is true, enter it!
  722. if (static_cast<bool>(MI) == IsElifDef) {
  723. CondInfo.FoundNonSkip = true;
  724. break;
  725. }
  726. }
  727. } else {
  728. SuggestTypoedDirective(Tok, Directive);
  729. }
  730. } else {
  731. SuggestTypoedDirective(Tok, Directive);
  732. }
  733. CurPPLexer->ParsingPreprocessorDirective = false;
  734. // Restore comment saving mode.
  735. if (CurLexer) CurLexer->resetExtendedTokenMode();
  736. }
  737. // Finally, if we are out of the conditional (saw an #endif or ran off the end
  738. // of the file, just stop skipping and return to lexing whatever came after
  739. // the #if block.
  740. CurPPLexer->LexingRawMode = false;
  741. // The last skipped range isn't actually skipped yet if it's truncated
  742. // by the end of the preamble; we'll resume parsing after the preamble.
  743. if (Callbacks && (Tok.isNot(tok::eof) || !isRecordingPreamble()))
  744. Callbacks->SourceRangeSkipped(
  745. SourceRange(HashTokenLoc, endLoc.isValid()
  746. ? endLoc
  747. : CurPPLexer->getSourceLocation()),
  748. Tok.getLocation());
  749. }
  750. Module *Preprocessor::getModuleForLocation(SourceLocation Loc,
  751. bool AllowTextual) {
  752. if (!SourceMgr.isInMainFile(Loc)) {
  753. // Try to determine the module of the include directive.
  754. // FIXME: Look into directly passing the FileEntry from LookupFile instead.
  755. FileID IDOfIncl = SourceMgr.getFileID(SourceMgr.getExpansionLoc(Loc));
  756. if (const FileEntry *EntryOfIncl = SourceMgr.getFileEntryForID(IDOfIncl)) {
  757. // The include comes from an included file.
  758. return HeaderInfo.getModuleMap()
  759. .findModuleForHeader(EntryOfIncl, AllowTextual)
  760. .getModule();
  761. }
  762. }
  763. // This is either in the main file or not in a file at all. It belongs
  764. // to the current module, if there is one.
  765. return getLangOpts().CurrentModule.empty()
  766. ? nullptr
  767. : HeaderInfo.lookupModule(getLangOpts().CurrentModule, Loc);
  768. }
  769. const FileEntry *
  770. Preprocessor::getHeaderToIncludeForDiagnostics(SourceLocation IncLoc,
  771. SourceLocation Loc) {
  772. Module *IncM = getModuleForLocation(
  773. IncLoc, LangOpts.ModulesValidateTextualHeaderIncludes);
  774. // Walk up through the include stack, looking through textual headers of M
  775. // until we hit a non-textual header that we can #include. (We assume textual
  776. // headers of a module with non-textual headers aren't meant to be used to
  777. // import entities from the module.)
  778. auto &SM = getSourceManager();
  779. while (!Loc.isInvalid() && !SM.isInMainFile(Loc)) {
  780. auto ID = SM.getFileID(SM.getExpansionLoc(Loc));
  781. auto *FE = SM.getFileEntryForID(ID);
  782. if (!FE)
  783. break;
  784. // We want to find all possible modules that might contain this header, so
  785. // search all enclosing directories for module maps and load them.
  786. HeaderInfo.hasModuleMap(FE->getName(), /*Root*/ nullptr,
  787. SourceMgr.isInSystemHeader(Loc));
  788. bool InPrivateHeader = false;
  789. for (auto Header : HeaderInfo.findAllModulesForHeader(FE)) {
  790. if (!Header.isAccessibleFrom(IncM)) {
  791. // It's in a private header; we can't #include it.
  792. // FIXME: If there's a public header in some module that re-exports it,
  793. // then we could suggest including that, but it's not clear that's the
  794. // expected way to make this entity visible.
  795. InPrivateHeader = true;
  796. continue;
  797. }
  798. // Don't suggest explicitly excluded headers.
  799. if (Header.getRole() == ModuleMap::ExcludedHeader)
  800. continue;
  801. // We'll suggest including textual headers below if they're
  802. // include-guarded.
  803. if (Header.getRole() & ModuleMap::TextualHeader)
  804. continue;
  805. // If we have a module import syntax, we shouldn't include a header to
  806. // make a particular module visible. Let the caller know they should
  807. // suggest an import instead.
  808. if (getLangOpts().ObjC || getLangOpts().CPlusPlusModules ||
  809. getLangOpts().ModulesTS)
  810. return nullptr;
  811. // If this is an accessible, non-textual header of M's top-level module
  812. // that transitively includes the given location and makes the
  813. // corresponding module visible, this is the thing to #include.
  814. return FE;
  815. }
  816. // FIXME: If we're bailing out due to a private header, we shouldn't suggest
  817. // an import either.
  818. if (InPrivateHeader)
  819. return nullptr;
  820. // If the header is includable and has an include guard, assume the
  821. // intended way to expose its contents is by #include, not by importing a
  822. // module that transitively includes it.
  823. if (getHeaderSearchInfo().isFileMultipleIncludeGuarded(FE))
  824. return FE;
  825. Loc = SM.getIncludeLoc(ID);
  826. }
  827. return nullptr;
  828. }
  829. OptionalFileEntryRef Preprocessor::LookupFile(
  830. SourceLocation FilenameLoc, StringRef Filename, bool isAngled,
  831. ConstSearchDirIterator FromDir, const FileEntry *FromFile,
  832. ConstSearchDirIterator *CurDirArg, SmallVectorImpl<char> *SearchPath,
  833. SmallVectorImpl<char> *RelativePath,
  834. ModuleMap::KnownHeader *SuggestedModule, bool *IsMapped,
  835. bool *IsFrameworkFound, bool SkipCache, bool OpenFile, bool CacheFailures) {
  836. ConstSearchDirIterator CurDirLocal = nullptr;
  837. ConstSearchDirIterator &CurDir = CurDirArg ? *CurDirArg : CurDirLocal;
  838. Module *RequestingModule = getModuleForLocation(
  839. FilenameLoc, LangOpts.ModulesValidateTextualHeaderIncludes);
  840. bool RequestingModuleIsModuleInterface = !SourceMgr.isInMainFile(FilenameLoc);
  841. // If the header lookup mechanism may be relative to the current inclusion
  842. // stack, record the parent #includes.
  843. SmallVector<std::pair<const FileEntry *, const DirectoryEntry *>, 16>
  844. Includers;
  845. bool BuildSystemModule = false;
  846. if (!FromDir && !FromFile) {
  847. FileID FID = getCurrentFileLexer()->getFileID();
  848. const FileEntry *FileEnt = SourceMgr.getFileEntryForID(FID);
  849. // If there is no file entry associated with this file, it must be the
  850. // predefines buffer or the module includes buffer. Any other file is not
  851. // lexed with a normal lexer, so it won't be scanned for preprocessor
  852. // directives.
  853. //
  854. // If we have the predefines buffer, resolve #include references (which come
  855. // from the -include command line argument) from the current working
  856. // directory instead of relative to the main file.
  857. //
  858. // If we have the module includes buffer, resolve #include references (which
  859. // come from header declarations in the module map) relative to the module
  860. // map file.
  861. if (!FileEnt) {
  862. if (FID == SourceMgr.getMainFileID() && MainFileDir) {
  863. Includers.push_back(std::make_pair(nullptr, MainFileDir));
  864. BuildSystemModule = getCurrentModule()->IsSystem;
  865. } else if ((FileEnt =
  866. SourceMgr.getFileEntryForID(SourceMgr.getMainFileID())))
  867. Includers.push_back(std::make_pair(FileEnt, *FileMgr.getDirectory(".")));
  868. } else {
  869. Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir()));
  870. }
  871. // MSVC searches the current include stack from top to bottom for
  872. // headers included by quoted include directives.
  873. // See: http://msdn.microsoft.com/en-us/library/36k2cdd4.aspx
  874. if (LangOpts.MSVCCompat && !isAngled) {
  875. for (IncludeStackInfo &ISEntry : llvm::reverse(IncludeMacroStack)) {
  876. if (IsFileLexer(ISEntry))
  877. if ((FileEnt = ISEntry.ThePPLexer->getFileEntry()))
  878. Includers.push_back(std::make_pair(FileEnt, FileEnt->getDir()));
  879. }
  880. }
  881. }
  882. CurDir = CurDirLookup;
  883. if (FromFile) {
  884. // We're supposed to start looking from after a particular file. Search
  885. // the include path until we find that file or run out of files.
  886. ConstSearchDirIterator TmpCurDir = CurDir;
  887. ConstSearchDirIterator TmpFromDir = nullptr;
  888. while (OptionalFileEntryRef FE = HeaderInfo.LookupFile(
  889. Filename, FilenameLoc, isAngled, TmpFromDir, &TmpCurDir,
  890. Includers, SearchPath, RelativePath, RequestingModule,
  891. SuggestedModule, /*IsMapped=*/nullptr,
  892. /*IsFrameworkFound=*/nullptr, SkipCache)) {
  893. // Keep looking as if this file did a #include_next.
  894. TmpFromDir = TmpCurDir;
  895. ++TmpFromDir;
  896. if (&FE->getFileEntry() == FromFile) {
  897. // Found it.
  898. FromDir = TmpFromDir;
  899. CurDir = TmpCurDir;
  900. break;
  901. }
  902. }
  903. }
  904. // Do a standard file entry lookup.
  905. OptionalFileEntryRef FE = HeaderInfo.LookupFile(
  906. Filename, FilenameLoc, isAngled, FromDir, &CurDir, Includers, SearchPath,
  907. RelativePath, RequestingModule, SuggestedModule, IsMapped,
  908. IsFrameworkFound, SkipCache, BuildSystemModule, OpenFile, CacheFailures);
  909. if (FE) {
  910. if (SuggestedModule && !LangOpts.AsmPreprocessor)
  911. HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
  912. RequestingModule, RequestingModuleIsModuleInterface, FilenameLoc,
  913. Filename, *FE);
  914. return FE;
  915. }
  916. const FileEntry *CurFileEnt;
  917. // Otherwise, see if this is a subframework header. If so, this is relative
  918. // to one of the headers on the #include stack. Walk the list of the current
  919. // headers on the #include stack and pass them to HeaderInfo.
  920. if (IsFileLexer()) {
  921. if ((CurFileEnt = CurPPLexer->getFileEntry())) {
  922. if (OptionalFileEntryRef FE = HeaderInfo.LookupSubframeworkHeader(
  923. Filename, CurFileEnt, SearchPath, RelativePath, RequestingModule,
  924. SuggestedModule)) {
  925. if (SuggestedModule && !LangOpts.AsmPreprocessor)
  926. HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
  927. RequestingModule, RequestingModuleIsModuleInterface, FilenameLoc,
  928. Filename, *FE);
  929. return FE;
  930. }
  931. }
  932. }
  933. for (IncludeStackInfo &ISEntry : llvm::reverse(IncludeMacroStack)) {
  934. if (IsFileLexer(ISEntry)) {
  935. if ((CurFileEnt = ISEntry.ThePPLexer->getFileEntry())) {
  936. if (OptionalFileEntryRef FE = HeaderInfo.LookupSubframeworkHeader(
  937. Filename, CurFileEnt, SearchPath, RelativePath,
  938. RequestingModule, SuggestedModule)) {
  939. if (SuggestedModule && !LangOpts.AsmPreprocessor)
  940. HeaderInfo.getModuleMap().diagnoseHeaderInclusion(
  941. RequestingModule, RequestingModuleIsModuleInterface,
  942. FilenameLoc, Filename, *FE);
  943. return FE;
  944. }
  945. }
  946. }
  947. }
  948. // Otherwise, we really couldn't find the file.
  949. return std::nullopt;
  950. }
  951. //===----------------------------------------------------------------------===//
  952. // Preprocessor Directive Handling.
  953. //===----------------------------------------------------------------------===//
  954. class Preprocessor::ResetMacroExpansionHelper {
  955. public:
  956. ResetMacroExpansionHelper(Preprocessor *pp)
  957. : PP(pp), save(pp->DisableMacroExpansion) {
  958. if (pp->MacroExpansionInDirectivesOverride)
  959. pp->DisableMacroExpansion = false;
  960. }
  961. ~ResetMacroExpansionHelper() {
  962. PP->DisableMacroExpansion = save;
  963. }
  964. private:
  965. Preprocessor *PP;
  966. bool save;
  967. };
  968. /// Process a directive while looking for the through header or a #pragma
  969. /// hdrstop. The following directives are handled:
  970. /// #include (to check if it is the through header)
  971. /// #define (to warn about macros that don't match the PCH)
  972. /// #pragma (to check for pragma hdrstop).
  973. /// All other directives are completely discarded.
  974. void Preprocessor::HandleSkippedDirectiveWhileUsingPCH(Token &Result,
  975. SourceLocation HashLoc) {
  976. if (const IdentifierInfo *II = Result.getIdentifierInfo()) {
  977. if (II->getPPKeywordID() == tok::pp_define) {
  978. return HandleDefineDirective(Result,
  979. /*ImmediatelyAfterHeaderGuard=*/false);
  980. }
  981. if (SkippingUntilPCHThroughHeader &&
  982. II->getPPKeywordID() == tok::pp_include) {
  983. return HandleIncludeDirective(HashLoc, Result);
  984. }
  985. if (SkippingUntilPragmaHdrStop && II->getPPKeywordID() == tok::pp_pragma) {
  986. Lex(Result);
  987. auto *II = Result.getIdentifierInfo();
  988. if (II && II->getName() == "hdrstop")
  989. return HandlePragmaHdrstop(Result);
  990. }
  991. }
  992. DiscardUntilEndOfDirective();
  993. }
  994. /// HandleDirective - This callback is invoked when the lexer sees a # token
  995. /// at the start of a line. This consumes the directive, modifies the
  996. /// lexer/preprocessor state, and advances the lexer(s) so that the next token
  997. /// read is the correct one.
  998. void Preprocessor::HandleDirective(Token &Result) {
  999. // FIXME: Traditional: # with whitespace before it not recognized by K&R?
  1000. // We just parsed a # character at the start of a line, so we're in directive
  1001. // mode. Tell the lexer this so any newlines we see will be converted into an
  1002. // EOD token (which terminates the directive).
  1003. CurPPLexer->ParsingPreprocessorDirective = true;
  1004. if (CurLexer) CurLexer->SetKeepWhitespaceMode(false);
  1005. bool ImmediatelyAfterTopLevelIfndef =
  1006. CurPPLexer->MIOpt.getImmediatelyAfterTopLevelIfndef();
  1007. CurPPLexer->MIOpt.resetImmediatelyAfterTopLevelIfndef();
  1008. ++NumDirectives;
  1009. // We are about to read a token. For the multiple-include optimization FA to
  1010. // work, we have to remember if we had read any tokens *before* this
  1011. // pp-directive.
  1012. bool ReadAnyTokensBeforeDirective =CurPPLexer->MIOpt.getHasReadAnyTokensVal();
  1013. // Save the '#' token in case we need to return it later.
  1014. Token SavedHash = Result;
  1015. // Read the next token, the directive flavor. This isn't expanded due to
  1016. // C99 6.10.3p8.
  1017. LexUnexpandedToken(Result);
  1018. // C99 6.10.3p11: Is this preprocessor directive in macro invocation? e.g.:
  1019. // #define A(x) #x
  1020. // A(abc
  1021. // #warning blah
  1022. // def)
  1023. // If so, the user is relying on undefined behavior, emit a diagnostic. Do
  1024. // not support this for #include-like directives, since that can result in
  1025. // terrible diagnostics, and does not work in GCC.
  1026. if (InMacroArgs) {
  1027. if (IdentifierInfo *II = Result.getIdentifierInfo()) {
  1028. switch (II->getPPKeywordID()) {
  1029. case tok::pp_include:
  1030. case tok::pp_import:
  1031. case tok::pp_include_next:
  1032. case tok::pp___include_macros:
  1033. case tok::pp_pragma:
  1034. Diag(Result, diag::err_embedded_directive) << II->getName();
  1035. Diag(*ArgMacro, diag::note_macro_expansion_here)
  1036. << ArgMacro->getIdentifierInfo();
  1037. DiscardUntilEndOfDirective();
  1038. return;
  1039. default:
  1040. break;
  1041. }
  1042. }
  1043. Diag(Result, diag::ext_embedded_directive);
  1044. }
  1045. // Temporarily enable macro expansion if set so
  1046. // and reset to previous state when returning from this function.
  1047. ResetMacroExpansionHelper helper(this);
  1048. if (SkippingUntilPCHThroughHeader || SkippingUntilPragmaHdrStop)
  1049. return HandleSkippedDirectiveWhileUsingPCH(Result, SavedHash.getLocation());
  1050. switch (Result.getKind()) {
  1051. case tok::eod:
  1052. return; // null directive.
  1053. case tok::code_completion:
  1054. setCodeCompletionReached();
  1055. if (CodeComplete)
  1056. CodeComplete->CodeCompleteDirective(
  1057. CurPPLexer->getConditionalStackDepth() > 0);
  1058. return;
  1059. case tok::numeric_constant: // # 7 GNU line marker directive.
  1060. if (getLangOpts().AsmPreprocessor)
  1061. break; // # 4 is not a preprocessor directive in .S files.
  1062. return HandleDigitDirective(Result);
  1063. default:
  1064. IdentifierInfo *II = Result.getIdentifierInfo();
  1065. if (!II) break; // Not an identifier.
  1066. // Ask what the preprocessor keyword ID is.
  1067. switch (II->getPPKeywordID()) {
  1068. default: break;
  1069. // C99 6.10.1 - Conditional Inclusion.
  1070. case tok::pp_if:
  1071. return HandleIfDirective(Result, SavedHash, ReadAnyTokensBeforeDirective);
  1072. case tok::pp_ifdef:
  1073. return HandleIfdefDirective(Result, SavedHash, false,
  1074. true /*not valid for miopt*/);
  1075. case tok::pp_ifndef:
  1076. return HandleIfdefDirective(Result, SavedHash, true,
  1077. ReadAnyTokensBeforeDirective);
  1078. case tok::pp_elif:
  1079. case tok::pp_elifdef:
  1080. case tok::pp_elifndef:
  1081. return HandleElifFamilyDirective(Result, SavedHash, II->getPPKeywordID());
  1082. case tok::pp_else:
  1083. return HandleElseDirective(Result, SavedHash);
  1084. case tok::pp_endif:
  1085. return HandleEndifDirective(Result);
  1086. // C99 6.10.2 - Source File Inclusion.
  1087. case tok::pp_include:
  1088. // Handle #include.
  1089. return HandleIncludeDirective(SavedHash.getLocation(), Result);
  1090. case tok::pp___include_macros:
  1091. // Handle -imacros.
  1092. return HandleIncludeMacrosDirective(SavedHash.getLocation(), Result);
  1093. // C99 6.10.3 - Macro Replacement.
  1094. case tok::pp_define:
  1095. return HandleDefineDirective(Result, ImmediatelyAfterTopLevelIfndef);
  1096. case tok::pp_undef:
  1097. return HandleUndefDirective();
  1098. // C99 6.10.4 - Line Control.
  1099. case tok::pp_line:
  1100. return HandleLineDirective();
  1101. // C99 6.10.5 - Error Directive.
  1102. case tok::pp_error:
  1103. return HandleUserDiagnosticDirective(Result, false);
  1104. // C99 6.10.6 - Pragma Directive.
  1105. case tok::pp_pragma:
  1106. return HandlePragmaDirective({PIK_HashPragma, SavedHash.getLocation()});
  1107. // GNU Extensions.
  1108. case tok::pp_import:
  1109. return HandleImportDirective(SavedHash.getLocation(), Result);
  1110. case tok::pp_include_next:
  1111. return HandleIncludeNextDirective(SavedHash.getLocation(), Result);
  1112. case tok::pp_warning:
  1113. if (LangOpts.CPlusPlus)
  1114. Diag(Result, LangOpts.CPlusPlus2b
  1115. ? diag::warn_cxx2b_compat_warning_directive
  1116. : diag::ext_pp_warning_directive)
  1117. << /*C++2b*/ 1;
  1118. else
  1119. Diag(Result, LangOpts.C2x ? diag::warn_c2x_compat_warning_directive
  1120. : diag::ext_pp_warning_directive)
  1121. << /*C2x*/ 0;
  1122. return HandleUserDiagnosticDirective(Result, true);
  1123. case tok::pp_ident:
  1124. return HandleIdentSCCSDirective(Result);
  1125. case tok::pp_sccs:
  1126. return HandleIdentSCCSDirective(Result);
  1127. case tok::pp_assert:
  1128. //isExtension = true; // FIXME: implement #assert
  1129. break;
  1130. case tok::pp_unassert:
  1131. //isExtension = true; // FIXME: implement #unassert
  1132. break;
  1133. case tok::pp___public_macro:
  1134. if (getLangOpts().Modules || getLangOpts().ModulesLocalVisibility)
  1135. return HandleMacroPublicDirective(Result);
  1136. break;
  1137. case tok::pp___private_macro:
  1138. if (getLangOpts().Modules || getLangOpts().ModulesLocalVisibility)
  1139. return HandleMacroPrivateDirective();
  1140. break;
  1141. }
  1142. break;
  1143. }
  1144. // If this is a .S file, treat unknown # directives as non-preprocessor
  1145. // directives. This is important because # may be a comment or introduce
  1146. // various pseudo-ops. Just return the # token and push back the following
  1147. // token to be lexed next time.
  1148. if (getLangOpts().AsmPreprocessor) {
  1149. auto Toks = std::make_unique<Token[]>(2);
  1150. // Return the # and the token after it.
  1151. Toks[0] = SavedHash;
  1152. Toks[1] = Result;
  1153. // If the second token is a hashhash token, then we need to translate it to
  1154. // unknown so the token lexer doesn't try to perform token pasting.
  1155. if (Result.is(tok::hashhash))
  1156. Toks[1].setKind(tok::unknown);
  1157. // Enter this token stream so that we re-lex the tokens. Make sure to
  1158. // enable macro expansion, in case the token after the # is an identifier
  1159. // that is expanded.
  1160. EnterTokenStream(std::move(Toks), 2, false, /*IsReinject*/false);
  1161. return;
  1162. }
  1163. // If we reached here, the preprocessing token is not valid!
  1164. // Start suggesting if a similar directive found.
  1165. Diag(Result, diag::err_pp_invalid_directive) << 0;
  1166. // Read the rest of the PP line.
  1167. DiscardUntilEndOfDirective();
  1168. // Okay, we're done parsing the directive.
  1169. }
  1170. /// GetLineValue - Convert a numeric token into an unsigned value, emitting
  1171. /// Diagnostic DiagID if it is invalid, and returning the value in Val.
  1172. static bool GetLineValue(Token &DigitTok, unsigned &Val,
  1173. unsigned DiagID, Preprocessor &PP,
  1174. bool IsGNULineDirective=false) {
  1175. if (DigitTok.isNot(tok::numeric_constant)) {
  1176. PP.Diag(DigitTok, DiagID);
  1177. if (DigitTok.isNot(tok::eod))
  1178. PP.DiscardUntilEndOfDirective();
  1179. return true;
  1180. }
  1181. SmallString<64> IntegerBuffer;
  1182. IntegerBuffer.resize(DigitTok.getLength());
  1183. const char *DigitTokBegin = &IntegerBuffer[0];
  1184. bool Invalid = false;
  1185. unsigned ActualLength = PP.getSpelling(DigitTok, DigitTokBegin, &Invalid);
  1186. if (Invalid)
  1187. return true;
  1188. // Verify that we have a simple digit-sequence, and compute the value. This
  1189. // is always a simple digit string computed in decimal, so we do this manually
  1190. // here.
  1191. Val = 0;
  1192. for (unsigned i = 0; i != ActualLength; ++i) {
  1193. // C++1y [lex.fcon]p1:
  1194. // Optional separating single quotes in a digit-sequence are ignored
  1195. if (DigitTokBegin[i] == '\'')
  1196. continue;
  1197. if (!isDigit(DigitTokBegin[i])) {
  1198. PP.Diag(PP.AdvanceToTokenCharacter(DigitTok.getLocation(), i),
  1199. diag::err_pp_line_digit_sequence) << IsGNULineDirective;
  1200. PP.DiscardUntilEndOfDirective();
  1201. return true;
  1202. }
  1203. unsigned NextVal = Val*10+(DigitTokBegin[i]-'0');
  1204. if (NextVal < Val) { // overflow.
  1205. PP.Diag(DigitTok, DiagID);
  1206. PP.DiscardUntilEndOfDirective();
  1207. return true;
  1208. }
  1209. Val = NextVal;
  1210. }
  1211. if (DigitTokBegin[0] == '0' && Val)
  1212. PP.Diag(DigitTok.getLocation(), diag::warn_pp_line_decimal)
  1213. << IsGNULineDirective;
  1214. return false;
  1215. }
  1216. /// Handle a \#line directive: C99 6.10.4.
  1217. ///
  1218. /// The two acceptable forms are:
  1219. /// \verbatim
  1220. /// # line digit-sequence
  1221. /// # line digit-sequence "s-char-sequence"
  1222. /// \endverbatim
  1223. void Preprocessor::HandleLineDirective() {
  1224. // Read the line # and string argument. Per C99 6.10.4p5, these tokens are
  1225. // expanded.
  1226. Token DigitTok;
  1227. Lex(DigitTok);
  1228. // Validate the number and convert it to an unsigned.
  1229. unsigned LineNo;
  1230. if (GetLineValue(DigitTok, LineNo, diag::err_pp_line_requires_integer,*this))
  1231. return;
  1232. if (LineNo == 0)
  1233. Diag(DigitTok, diag::ext_pp_line_zero);
  1234. // Enforce C99 6.10.4p3: "The digit sequence shall not specify ... a
  1235. // number greater than 2147483647". C90 requires that the line # be <= 32767.
  1236. unsigned LineLimit = 32768U;
  1237. if (LangOpts.C99 || LangOpts.CPlusPlus11)
  1238. LineLimit = 2147483648U;
  1239. if (LineNo >= LineLimit)
  1240. Diag(DigitTok, diag::ext_pp_line_too_big) << LineLimit;
  1241. else if (LangOpts.CPlusPlus11 && LineNo >= 32768U)
  1242. Diag(DigitTok, diag::warn_cxx98_compat_pp_line_too_big);
  1243. int FilenameID = -1;
  1244. Token StrTok;
  1245. Lex(StrTok);
  1246. // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
  1247. // string followed by eod.
  1248. if (StrTok.is(tok::eod))
  1249. ; // ok
  1250. else if (StrTok.isNot(tok::string_literal)) {
  1251. Diag(StrTok, diag::err_pp_line_invalid_filename);
  1252. DiscardUntilEndOfDirective();
  1253. return;
  1254. } else if (StrTok.hasUDSuffix()) {
  1255. Diag(StrTok, diag::err_invalid_string_udl);
  1256. DiscardUntilEndOfDirective();
  1257. return;
  1258. } else {
  1259. // Parse and validate the string, converting it into a unique ID.
  1260. StringLiteralParser Literal(StrTok, *this);
  1261. assert(Literal.isOrdinary() && "Didn't allow wide strings in");
  1262. if (Literal.hadError) {
  1263. DiscardUntilEndOfDirective();
  1264. return;
  1265. }
  1266. if (Literal.Pascal) {
  1267. Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
  1268. DiscardUntilEndOfDirective();
  1269. return;
  1270. }
  1271. FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
  1272. // Verify that there is nothing after the string, other than EOD. Because
  1273. // of C99 6.10.4p5, macros that expand to empty tokens are ok.
  1274. CheckEndOfDirective("line", true);
  1275. }
  1276. // Take the file kind of the file containing the #line directive. #line
  1277. // directives are often used for generated sources from the same codebase, so
  1278. // the new file should generally be classified the same way as the current
  1279. // file. This is visible in GCC's pre-processed output, which rewrites #line
  1280. // to GNU line markers.
  1281. SrcMgr::CharacteristicKind FileKind =
  1282. SourceMgr.getFileCharacteristic(DigitTok.getLocation());
  1283. SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID, false,
  1284. false, FileKind);
  1285. if (Callbacks)
  1286. Callbacks->FileChanged(CurPPLexer->getSourceLocation(),
  1287. PPCallbacks::RenameFile, FileKind);
  1288. }
  1289. /// ReadLineMarkerFlags - Parse and validate any flags at the end of a GNU line
  1290. /// marker directive.
  1291. static bool ReadLineMarkerFlags(bool &IsFileEntry, bool &IsFileExit,
  1292. SrcMgr::CharacteristicKind &FileKind,
  1293. Preprocessor &PP) {
  1294. unsigned FlagVal;
  1295. Token FlagTok;
  1296. PP.Lex(FlagTok);
  1297. if (FlagTok.is(tok::eod)) return false;
  1298. if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
  1299. return true;
  1300. if (FlagVal == 1) {
  1301. IsFileEntry = true;
  1302. PP.Lex(FlagTok);
  1303. if (FlagTok.is(tok::eod)) return false;
  1304. if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
  1305. return true;
  1306. } else if (FlagVal == 2) {
  1307. IsFileExit = true;
  1308. SourceManager &SM = PP.getSourceManager();
  1309. // If we are leaving the current presumed file, check to make sure the
  1310. // presumed include stack isn't empty!
  1311. FileID CurFileID =
  1312. SM.getDecomposedExpansionLoc(FlagTok.getLocation()).first;
  1313. PresumedLoc PLoc = SM.getPresumedLoc(FlagTok.getLocation());
  1314. if (PLoc.isInvalid())
  1315. return true;
  1316. // If there is no include loc (main file) or if the include loc is in a
  1317. // different physical file, then we aren't in a "1" line marker flag region.
  1318. SourceLocation IncLoc = PLoc.getIncludeLoc();
  1319. if (IncLoc.isInvalid() ||
  1320. SM.getDecomposedExpansionLoc(IncLoc).first != CurFileID) {
  1321. PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_pop);
  1322. PP.DiscardUntilEndOfDirective();
  1323. return true;
  1324. }
  1325. PP.Lex(FlagTok);
  1326. if (FlagTok.is(tok::eod)) return false;
  1327. if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag,PP))
  1328. return true;
  1329. }
  1330. // We must have 3 if there are still flags.
  1331. if (FlagVal != 3) {
  1332. PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
  1333. PP.DiscardUntilEndOfDirective();
  1334. return true;
  1335. }
  1336. FileKind = SrcMgr::C_System;
  1337. PP.Lex(FlagTok);
  1338. if (FlagTok.is(tok::eod)) return false;
  1339. if (GetLineValue(FlagTok, FlagVal, diag::err_pp_linemarker_invalid_flag, PP))
  1340. return true;
  1341. // We must have 4 if there is yet another flag.
  1342. if (FlagVal != 4) {
  1343. PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
  1344. PP.DiscardUntilEndOfDirective();
  1345. return true;
  1346. }
  1347. FileKind = SrcMgr::C_ExternCSystem;
  1348. PP.Lex(FlagTok);
  1349. if (FlagTok.is(tok::eod)) return false;
  1350. // There are no more valid flags here.
  1351. PP.Diag(FlagTok, diag::err_pp_linemarker_invalid_flag);
  1352. PP.DiscardUntilEndOfDirective();
  1353. return true;
  1354. }
  1355. /// HandleDigitDirective - Handle a GNU line marker directive, whose syntax is
  1356. /// one of the following forms:
  1357. ///
  1358. /// # 42
  1359. /// # 42 "file" ('1' | '2')?
  1360. /// # 42 "file" ('1' | '2')? '3' '4'?
  1361. ///
  1362. void Preprocessor::HandleDigitDirective(Token &DigitTok) {
  1363. // Validate the number and convert it to an unsigned. GNU does not have a
  1364. // line # limit other than it fit in 32-bits.
  1365. unsigned LineNo;
  1366. if (GetLineValue(DigitTok, LineNo, diag::err_pp_linemarker_requires_integer,
  1367. *this, true))
  1368. return;
  1369. Token StrTok;
  1370. Lex(StrTok);
  1371. bool IsFileEntry = false, IsFileExit = false;
  1372. int FilenameID = -1;
  1373. SrcMgr::CharacteristicKind FileKind = SrcMgr::C_User;
  1374. // If the StrTok is "eod", then it wasn't present. Otherwise, it must be a
  1375. // string followed by eod.
  1376. if (StrTok.is(tok::eod)) {
  1377. Diag(StrTok, diag::ext_pp_gnu_line_directive);
  1378. // Treat this like "#line NN", which doesn't change file characteristics.
  1379. FileKind = SourceMgr.getFileCharacteristic(DigitTok.getLocation());
  1380. } else if (StrTok.isNot(tok::string_literal)) {
  1381. Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
  1382. DiscardUntilEndOfDirective();
  1383. return;
  1384. } else if (StrTok.hasUDSuffix()) {
  1385. Diag(StrTok, diag::err_invalid_string_udl);
  1386. DiscardUntilEndOfDirective();
  1387. return;
  1388. } else {
  1389. // Parse and validate the string, converting it into a unique ID.
  1390. StringLiteralParser Literal(StrTok, *this);
  1391. assert(Literal.isOrdinary() && "Didn't allow wide strings in");
  1392. if (Literal.hadError) {
  1393. DiscardUntilEndOfDirective();
  1394. return;
  1395. }
  1396. if (Literal.Pascal) {
  1397. Diag(StrTok, diag::err_pp_linemarker_invalid_filename);
  1398. DiscardUntilEndOfDirective();
  1399. return;
  1400. }
  1401. // If a filename was present, read any flags that are present.
  1402. if (ReadLineMarkerFlags(IsFileEntry, IsFileExit, FileKind, *this))
  1403. return;
  1404. if (!SourceMgr.isWrittenInBuiltinFile(DigitTok.getLocation()) &&
  1405. !SourceMgr.isWrittenInCommandLineFile(DigitTok.getLocation()))
  1406. Diag(StrTok, diag::ext_pp_gnu_line_directive);
  1407. // Exiting to an empty string means pop to the including file, so leave
  1408. // FilenameID as -1 in that case.
  1409. if (!(IsFileExit && Literal.GetString().empty()))
  1410. FilenameID = SourceMgr.getLineTableFilenameID(Literal.GetString());
  1411. }
  1412. // Create a line note with this information.
  1413. SourceMgr.AddLineNote(DigitTok.getLocation(), LineNo, FilenameID, IsFileEntry,
  1414. IsFileExit, FileKind);
  1415. // If the preprocessor has callbacks installed, notify them of the #line
  1416. // change. This is used so that the line marker comes out in -E mode for
  1417. // example.
  1418. if (Callbacks) {
  1419. PPCallbacks::FileChangeReason Reason = PPCallbacks::RenameFile;
  1420. if (IsFileEntry)
  1421. Reason = PPCallbacks::EnterFile;
  1422. else if (IsFileExit)
  1423. Reason = PPCallbacks::ExitFile;
  1424. Callbacks->FileChanged(CurPPLexer->getSourceLocation(), Reason, FileKind);
  1425. }
  1426. }
  1427. /// HandleUserDiagnosticDirective - Handle a #warning or #error directive.
  1428. ///
  1429. void Preprocessor::HandleUserDiagnosticDirective(Token &Tok,
  1430. bool isWarning) {
  1431. // Read the rest of the line raw. We do this because we don't want macros
  1432. // to be expanded and we don't require that the tokens be valid preprocessing
  1433. // tokens. For example, this is allowed: "#warning ` 'foo". GCC does
  1434. // collapse multiple consecutive white space between tokens, but this isn't
  1435. // specified by the standard.
  1436. SmallString<128> Message;
  1437. CurLexer->ReadToEndOfLine(&Message);
  1438. // Find the first non-whitespace character, so that we can make the
  1439. // diagnostic more succinct.
  1440. StringRef Msg = Message.str().ltrim(' ');
  1441. if (isWarning)
  1442. Diag(Tok, diag::pp_hash_warning) << Msg;
  1443. else
  1444. Diag(Tok, diag::err_pp_hash_error) << Msg;
  1445. }
  1446. /// HandleIdentSCCSDirective - Handle a #ident/#sccs directive.
  1447. ///
  1448. void Preprocessor::HandleIdentSCCSDirective(Token &Tok) {
  1449. // Yes, this directive is an extension.
  1450. Diag(Tok, diag::ext_pp_ident_directive);
  1451. // Read the string argument.
  1452. Token StrTok;
  1453. Lex(StrTok);
  1454. // If the token kind isn't a string, it's a malformed directive.
  1455. if (StrTok.isNot(tok::string_literal) &&
  1456. StrTok.isNot(tok::wide_string_literal)) {
  1457. Diag(StrTok, diag::err_pp_malformed_ident);
  1458. if (StrTok.isNot(tok::eod))
  1459. DiscardUntilEndOfDirective();
  1460. return;
  1461. }
  1462. if (StrTok.hasUDSuffix()) {
  1463. Diag(StrTok, diag::err_invalid_string_udl);
  1464. DiscardUntilEndOfDirective();
  1465. return;
  1466. }
  1467. // Verify that there is nothing after the string, other than EOD.
  1468. CheckEndOfDirective("ident");
  1469. if (Callbacks) {
  1470. bool Invalid = false;
  1471. std::string Str = getSpelling(StrTok, &Invalid);
  1472. if (!Invalid)
  1473. Callbacks->Ident(Tok.getLocation(), Str);
  1474. }
  1475. }
  1476. /// Handle a #public directive.
  1477. void Preprocessor::HandleMacroPublicDirective(Token &Tok) {
  1478. Token MacroNameTok;
  1479. ReadMacroName(MacroNameTok, MU_Undef);
  1480. // Error reading macro name? If so, diagnostic already issued.
  1481. if (MacroNameTok.is(tok::eod))
  1482. return;
  1483. // Check to see if this is the last token on the #__public_macro line.
  1484. CheckEndOfDirective("__public_macro");
  1485. IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
  1486. // Okay, we finally have a valid identifier to undef.
  1487. MacroDirective *MD = getLocalMacroDirective(II);
  1488. // If the macro is not defined, this is an error.
  1489. if (!MD) {
  1490. Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
  1491. return;
  1492. }
  1493. // Note that this macro has now been exported.
  1494. appendMacroDirective(II, AllocateVisibilityMacroDirective(
  1495. MacroNameTok.getLocation(), /*isPublic=*/true));
  1496. }
  1497. /// Handle a #private directive.
  1498. void Preprocessor::HandleMacroPrivateDirective() {
  1499. Token MacroNameTok;
  1500. ReadMacroName(MacroNameTok, MU_Undef);
  1501. // Error reading macro name? If so, diagnostic already issued.
  1502. if (MacroNameTok.is(tok::eod))
  1503. return;
  1504. // Check to see if this is the last token on the #__private_macro line.
  1505. CheckEndOfDirective("__private_macro");
  1506. IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
  1507. // Okay, we finally have a valid identifier to undef.
  1508. MacroDirective *MD = getLocalMacroDirective(II);
  1509. // If the macro is not defined, this is an error.
  1510. if (!MD) {
  1511. Diag(MacroNameTok, diag::err_pp_visibility_non_macro) << II;
  1512. return;
  1513. }
  1514. // Note that this macro has now been marked private.
  1515. appendMacroDirective(II, AllocateVisibilityMacroDirective(
  1516. MacroNameTok.getLocation(), /*isPublic=*/false));
  1517. }
  1518. //===----------------------------------------------------------------------===//
  1519. // Preprocessor Include Directive Handling.
  1520. //===----------------------------------------------------------------------===//
  1521. /// GetIncludeFilenameSpelling - Turn the specified lexer token into a fully
  1522. /// checked and spelled filename, e.g. as an operand of \#include. This returns
  1523. /// true if the input filename was in <>'s or false if it were in ""'s. The
  1524. /// caller is expected to provide a buffer that is large enough to hold the
  1525. /// spelling of the filename, but is also expected to handle the case when
  1526. /// this method decides to use a different buffer.
  1527. bool Preprocessor::GetIncludeFilenameSpelling(SourceLocation Loc,
  1528. StringRef &Buffer) {
  1529. // Get the text form of the filename.
  1530. assert(!Buffer.empty() && "Can't have tokens with empty spellings!");
  1531. // FIXME: Consider warning on some of the cases described in C11 6.4.7/3 and
  1532. // C++20 [lex.header]/2:
  1533. //
  1534. // If `"`, `'`, `\`, `/*`, or `//` appears in a header-name, then
  1535. // in C: behavior is undefined
  1536. // in C++: program is conditionally-supported with implementation-defined
  1537. // semantics
  1538. // Make sure the filename is <x> or "x".
  1539. bool isAngled;
  1540. if (Buffer[0] == '<') {
  1541. if (Buffer.back() != '>') {
  1542. Diag(Loc, diag::err_pp_expects_filename);
  1543. Buffer = StringRef();
  1544. return true;
  1545. }
  1546. isAngled = true;
  1547. } else if (Buffer[0] == '"') {
  1548. if (Buffer.back() != '"') {
  1549. Diag(Loc, diag::err_pp_expects_filename);
  1550. Buffer = StringRef();
  1551. return true;
  1552. }
  1553. isAngled = false;
  1554. } else {
  1555. Diag(Loc, diag::err_pp_expects_filename);
  1556. Buffer = StringRef();
  1557. return true;
  1558. }
  1559. // Diagnose #include "" as invalid.
  1560. if (Buffer.size() <= 2) {
  1561. Diag(Loc, diag::err_pp_empty_filename);
  1562. Buffer = StringRef();
  1563. return true;
  1564. }
  1565. // Skip the brackets.
  1566. Buffer = Buffer.substr(1, Buffer.size()-2);
  1567. return isAngled;
  1568. }
  1569. /// Push a token onto the token stream containing an annotation.
  1570. void Preprocessor::EnterAnnotationToken(SourceRange Range,
  1571. tok::TokenKind Kind,
  1572. void *AnnotationVal) {
  1573. // FIXME: Produce this as the current token directly, rather than
  1574. // allocating a new token for it.
  1575. auto Tok = std::make_unique<Token[]>(1);
  1576. Tok[0].startToken();
  1577. Tok[0].setKind(Kind);
  1578. Tok[0].setLocation(Range.getBegin());
  1579. Tok[0].setAnnotationEndLoc(Range.getEnd());
  1580. Tok[0].setAnnotationValue(AnnotationVal);
  1581. EnterTokenStream(std::move(Tok), 1, true, /*IsReinject*/ false);
  1582. }
  1583. /// Produce a diagnostic informing the user that a #include or similar
  1584. /// was implicitly treated as a module import.
  1585. static void diagnoseAutoModuleImport(
  1586. Preprocessor &PP, SourceLocation HashLoc, Token &IncludeTok,
  1587. ArrayRef<std::pair<IdentifierInfo *, SourceLocation>> Path,
  1588. SourceLocation PathEnd) {
  1589. SmallString<128> PathString;
  1590. for (size_t I = 0, N = Path.size(); I != N; ++I) {
  1591. if (I)
  1592. PathString += '.';
  1593. PathString += Path[I].first->getName();
  1594. }
  1595. int IncludeKind = 0;
  1596. switch (IncludeTok.getIdentifierInfo()->getPPKeywordID()) {
  1597. case tok::pp_include:
  1598. IncludeKind = 0;
  1599. break;
  1600. case tok::pp_import:
  1601. IncludeKind = 1;
  1602. break;
  1603. case tok::pp_include_next:
  1604. IncludeKind = 2;
  1605. break;
  1606. case tok::pp___include_macros:
  1607. IncludeKind = 3;
  1608. break;
  1609. default:
  1610. llvm_unreachable("unknown include directive kind");
  1611. }
  1612. PP.Diag(HashLoc, diag::remark_pp_include_directive_modular_translation)
  1613. << IncludeKind << PathString;
  1614. }
  1615. // Given a vector of path components and a string containing the real
  1616. // path to the file, build a properly-cased replacement in the vector,
  1617. // and return true if the replacement should be suggested.
  1618. static bool trySimplifyPath(SmallVectorImpl<StringRef> &Components,
  1619. StringRef RealPathName) {
  1620. auto RealPathComponentIter = llvm::sys::path::rbegin(RealPathName);
  1621. auto RealPathComponentEnd = llvm::sys::path::rend(RealPathName);
  1622. int Cnt = 0;
  1623. bool SuggestReplacement = false;
  1624. // Below is a best-effort to handle ".." in paths. It is admittedly
  1625. // not 100% correct in the presence of symlinks.
  1626. for (auto &Component : llvm::reverse(Components)) {
  1627. if ("." == Component) {
  1628. } else if (".." == Component) {
  1629. ++Cnt;
  1630. } else if (Cnt) {
  1631. --Cnt;
  1632. } else if (RealPathComponentIter != RealPathComponentEnd) {
  1633. if (Component != *RealPathComponentIter) {
  1634. // If these path components differ by more than just case, then we
  1635. // may be looking at symlinked paths. Bail on this diagnostic to avoid
  1636. // noisy false positives.
  1637. SuggestReplacement =
  1638. RealPathComponentIter->equals_insensitive(Component);
  1639. if (!SuggestReplacement)
  1640. break;
  1641. Component = *RealPathComponentIter;
  1642. }
  1643. ++RealPathComponentIter;
  1644. }
  1645. }
  1646. return SuggestReplacement;
  1647. }
  1648. bool Preprocessor::checkModuleIsAvailable(const LangOptions &LangOpts,
  1649. const TargetInfo &TargetInfo,
  1650. DiagnosticsEngine &Diags, Module *M) {
  1651. Module::Requirement Requirement;
  1652. Module::UnresolvedHeaderDirective MissingHeader;
  1653. Module *ShadowingModule = nullptr;
  1654. if (M->isAvailable(LangOpts, TargetInfo, Requirement, MissingHeader,
  1655. ShadowingModule))
  1656. return false;
  1657. if (MissingHeader.FileNameLoc.isValid()) {
  1658. Diags.Report(MissingHeader.FileNameLoc, diag::err_module_header_missing)
  1659. << MissingHeader.IsUmbrella << MissingHeader.FileName;
  1660. } else if (ShadowingModule) {
  1661. Diags.Report(M->DefinitionLoc, diag::err_module_shadowed) << M->Name;
  1662. Diags.Report(ShadowingModule->DefinitionLoc,
  1663. diag::note_previous_definition);
  1664. } else {
  1665. // FIXME: Track the location at which the requirement was specified, and
  1666. // use it here.
  1667. Diags.Report(M->DefinitionLoc, diag::err_module_unavailable)
  1668. << M->getFullModuleName() << Requirement.second << Requirement.first;
  1669. }
  1670. return true;
  1671. }
  1672. std::pair<ConstSearchDirIterator, const FileEntry *>
  1673. Preprocessor::getIncludeNextStart(const Token &IncludeNextTok) const {
  1674. // #include_next is like #include, except that we start searching after
  1675. // the current found directory. If we can't do this, issue a
  1676. // diagnostic.
  1677. ConstSearchDirIterator Lookup = CurDirLookup;
  1678. const FileEntry *LookupFromFile = nullptr;
  1679. if (isInPrimaryFile() && LangOpts.IsHeaderFile) {
  1680. // If the main file is a header, then it's either for PCH/AST generation,
  1681. // or libclang opened it. Either way, handle it as a normal include below
  1682. // and do not complain about include_next.
  1683. } else if (isInPrimaryFile()) {
  1684. Lookup = nullptr;
  1685. Diag(IncludeNextTok, diag::pp_include_next_in_primary);
  1686. } else if (CurLexerSubmodule) {
  1687. // Start looking up in the directory *after* the one in which the current
  1688. // file would be found, if any.
  1689. assert(CurPPLexer && "#include_next directive in macro?");
  1690. LookupFromFile = CurPPLexer->getFileEntry();
  1691. Lookup = nullptr;
  1692. } else if (!Lookup) {
  1693. // The current file was not found by walking the include path. Either it
  1694. // is the primary file (handled above), or it was found by absolute path,
  1695. // or it was found relative to such a file.
  1696. // FIXME: Track enough information so we know which case we're in.
  1697. Diag(IncludeNextTok, diag::pp_include_next_absolute_path);
  1698. } else {
  1699. // Start looking up in the next directory.
  1700. ++Lookup;
  1701. }
  1702. return {Lookup, LookupFromFile};
  1703. }
  1704. /// HandleIncludeDirective - The "\#include" tokens have just been read, read
  1705. /// the file to be included from the lexer, then include it! This is a common
  1706. /// routine with functionality shared between \#include, \#include_next and
  1707. /// \#import. LookupFrom is set when this is a \#include_next directive, it
  1708. /// specifies the file to start searching from.
  1709. void Preprocessor::HandleIncludeDirective(SourceLocation HashLoc,
  1710. Token &IncludeTok,
  1711. ConstSearchDirIterator LookupFrom,
  1712. const FileEntry *LookupFromFile) {
  1713. Token FilenameTok;
  1714. if (LexHeaderName(FilenameTok))
  1715. return;
  1716. if (FilenameTok.isNot(tok::header_name)) {
  1717. Diag(FilenameTok.getLocation(), diag::err_pp_expects_filename);
  1718. if (FilenameTok.isNot(tok::eod))
  1719. DiscardUntilEndOfDirective();
  1720. return;
  1721. }
  1722. // Verify that there is nothing after the filename, other than EOD. Note
  1723. // that we allow macros that expand to nothing after the filename, because
  1724. // this falls into the category of "#include pp-tokens new-line" specified
  1725. // in C99 6.10.2p4.
  1726. SourceLocation EndLoc =
  1727. CheckEndOfDirective(IncludeTok.getIdentifierInfo()->getNameStart(), true);
  1728. auto Action = HandleHeaderIncludeOrImport(HashLoc, IncludeTok, FilenameTok,
  1729. EndLoc, LookupFrom, LookupFromFile);
  1730. switch (Action.Kind) {
  1731. case ImportAction::None:
  1732. case ImportAction::SkippedModuleImport:
  1733. break;
  1734. case ImportAction::ModuleBegin:
  1735. EnterAnnotationToken(SourceRange(HashLoc, EndLoc),
  1736. tok::annot_module_begin, Action.ModuleForHeader);
  1737. break;
  1738. case ImportAction::HeaderUnitImport:
  1739. EnterAnnotationToken(SourceRange(HashLoc, EndLoc), tok::annot_header_unit,
  1740. Action.ModuleForHeader);
  1741. break;
  1742. case ImportAction::ModuleImport:
  1743. EnterAnnotationToken(SourceRange(HashLoc, EndLoc),
  1744. tok::annot_module_include, Action.ModuleForHeader);
  1745. break;
  1746. case ImportAction::Failure:
  1747. assert(TheModuleLoader.HadFatalFailure &&
  1748. "This should be an early exit only to a fatal error");
  1749. TheModuleLoader.HadFatalFailure = true;
  1750. IncludeTok.setKind(tok::eof);
  1751. CurLexer->cutOffLexing();
  1752. return;
  1753. }
  1754. }
  1755. OptionalFileEntryRef Preprocessor::LookupHeaderIncludeOrImport(
  1756. ConstSearchDirIterator *CurDir, StringRef &Filename,
  1757. SourceLocation FilenameLoc, CharSourceRange FilenameRange,
  1758. const Token &FilenameTok, bool &IsFrameworkFound, bool IsImportDecl,
  1759. bool &IsMapped, ConstSearchDirIterator LookupFrom,
  1760. const FileEntry *LookupFromFile, StringRef &LookupFilename,
  1761. SmallVectorImpl<char> &RelativePath, SmallVectorImpl<char> &SearchPath,
  1762. ModuleMap::KnownHeader &SuggestedModule, bool isAngled) {
  1763. OptionalFileEntryRef File = LookupFile(
  1764. FilenameLoc, LookupFilename, isAngled, LookupFrom, LookupFromFile, CurDir,
  1765. Callbacks ? &SearchPath : nullptr, Callbacks ? &RelativePath : nullptr,
  1766. &SuggestedModule, &IsMapped, &IsFrameworkFound);
  1767. if (File)
  1768. return File;
  1769. // Give the clients a chance to silently skip this include.
  1770. if (Callbacks && Callbacks->FileNotFound(Filename))
  1771. return std::nullopt;
  1772. if (SuppressIncludeNotFoundError)
  1773. return std::nullopt;
  1774. // If the file could not be located and it was included via angle
  1775. // brackets, we can attempt a lookup as though it were a quoted path to
  1776. // provide the user with a possible fixit.
  1777. if (isAngled) {
  1778. OptionalFileEntryRef File = LookupFile(
  1779. FilenameLoc, LookupFilename, false, LookupFrom, LookupFromFile, CurDir,
  1780. Callbacks ? &SearchPath : nullptr, Callbacks ? &RelativePath : nullptr,
  1781. &SuggestedModule, &IsMapped,
  1782. /*IsFrameworkFound=*/nullptr);
  1783. if (File) {
  1784. Diag(FilenameTok, diag::err_pp_file_not_found_angled_include_not_fatal)
  1785. << Filename << IsImportDecl
  1786. << FixItHint::CreateReplacement(FilenameRange,
  1787. "\"" + Filename.str() + "\"");
  1788. return File;
  1789. }
  1790. }
  1791. // Check for likely typos due to leading or trailing non-isAlphanumeric
  1792. // characters
  1793. StringRef OriginalFilename = Filename;
  1794. if (LangOpts.SpellChecking) {
  1795. // A heuristic to correct a typo file name by removing leading and
  1796. // trailing non-isAlphanumeric characters.
  1797. auto CorrectTypoFilename = [](llvm::StringRef Filename) {
  1798. Filename = Filename.drop_until(isAlphanumeric);
  1799. while (!Filename.empty() && !isAlphanumeric(Filename.back())) {
  1800. Filename = Filename.drop_back();
  1801. }
  1802. return Filename;
  1803. };
  1804. StringRef TypoCorrectionName = CorrectTypoFilename(Filename);
  1805. StringRef TypoCorrectionLookupName = CorrectTypoFilename(LookupFilename);
  1806. OptionalFileEntryRef File = LookupFile(
  1807. FilenameLoc, TypoCorrectionLookupName, isAngled, LookupFrom,
  1808. LookupFromFile, CurDir, Callbacks ? &SearchPath : nullptr,
  1809. Callbacks ? &RelativePath : nullptr, &SuggestedModule, &IsMapped,
  1810. /*IsFrameworkFound=*/nullptr);
  1811. if (File) {
  1812. auto Hint =
  1813. isAngled ? FixItHint::CreateReplacement(
  1814. FilenameRange, "<" + TypoCorrectionName.str() + ">")
  1815. : FixItHint::CreateReplacement(
  1816. FilenameRange, "\"" + TypoCorrectionName.str() + "\"");
  1817. Diag(FilenameTok, diag::err_pp_file_not_found_typo_not_fatal)
  1818. << OriginalFilename << TypoCorrectionName << Hint;
  1819. // We found the file, so set the Filename to the name after typo
  1820. // correction.
  1821. Filename = TypoCorrectionName;
  1822. LookupFilename = TypoCorrectionLookupName;
  1823. return File;
  1824. }
  1825. }
  1826. // If the file is still not found, just go with the vanilla diagnostic
  1827. assert(!File && "expected missing file");
  1828. Diag(FilenameTok, diag::err_pp_file_not_found)
  1829. << OriginalFilename << FilenameRange;
  1830. if (IsFrameworkFound) {
  1831. size_t SlashPos = OriginalFilename.find('/');
  1832. assert(SlashPos != StringRef::npos &&
  1833. "Include with framework name should have '/' in the filename");
  1834. StringRef FrameworkName = OriginalFilename.substr(0, SlashPos);
  1835. FrameworkCacheEntry &CacheEntry =
  1836. HeaderInfo.LookupFrameworkCache(FrameworkName);
  1837. assert(CacheEntry.Directory && "Found framework should be in cache");
  1838. Diag(FilenameTok, diag::note_pp_framework_without_header)
  1839. << OriginalFilename.substr(SlashPos + 1) << FrameworkName
  1840. << CacheEntry.Directory->getName();
  1841. }
  1842. return std::nullopt;
  1843. }
  1844. /// Handle either a #include-like directive or an import declaration that names
  1845. /// a header file.
  1846. ///
  1847. /// \param HashLoc The location of the '#' token for an include, or
  1848. /// SourceLocation() for an import declaration.
  1849. /// \param IncludeTok The include / include_next / import token.
  1850. /// \param FilenameTok The header-name token.
  1851. /// \param EndLoc The location at which any imported macros become visible.
  1852. /// \param LookupFrom For #include_next, the starting directory for the
  1853. /// directory lookup.
  1854. /// \param LookupFromFile For #include_next, the starting file for the directory
  1855. /// lookup.
  1856. Preprocessor::ImportAction Preprocessor::HandleHeaderIncludeOrImport(
  1857. SourceLocation HashLoc, Token &IncludeTok, Token &FilenameTok,
  1858. SourceLocation EndLoc, ConstSearchDirIterator LookupFrom,
  1859. const FileEntry *LookupFromFile) {
  1860. SmallString<128> FilenameBuffer;
  1861. StringRef Filename = getSpelling(FilenameTok, FilenameBuffer);
  1862. SourceLocation CharEnd = FilenameTok.getEndLoc();
  1863. CharSourceRange FilenameRange
  1864. = CharSourceRange::getCharRange(FilenameTok.getLocation(), CharEnd);
  1865. StringRef OriginalFilename = Filename;
  1866. bool isAngled =
  1867. GetIncludeFilenameSpelling(FilenameTok.getLocation(), Filename);
  1868. // If GetIncludeFilenameSpelling set the start ptr to null, there was an
  1869. // error.
  1870. if (Filename.empty())
  1871. return {ImportAction::None};
  1872. bool IsImportDecl = HashLoc.isInvalid();
  1873. SourceLocation StartLoc = IsImportDecl ? IncludeTok.getLocation() : HashLoc;
  1874. // Complain about attempts to #include files in an audit pragma.
  1875. if (PragmaARCCFCodeAuditedInfo.second.isValid()) {
  1876. Diag(StartLoc, diag::err_pp_include_in_arc_cf_code_audited) << IsImportDecl;
  1877. Diag(PragmaARCCFCodeAuditedInfo.second, diag::note_pragma_entered_here);
  1878. // Immediately leave the pragma.
  1879. PragmaARCCFCodeAuditedInfo = {nullptr, SourceLocation()};
  1880. }
  1881. // Complain about attempts to #include files in an assume-nonnull pragma.
  1882. if (PragmaAssumeNonNullLoc.isValid()) {
  1883. Diag(StartLoc, diag::err_pp_include_in_assume_nonnull) << IsImportDecl;
  1884. Diag(PragmaAssumeNonNullLoc, diag::note_pragma_entered_here);
  1885. // Immediately leave the pragma.
  1886. PragmaAssumeNonNullLoc = SourceLocation();
  1887. }
  1888. if (HeaderInfo.HasIncludeAliasMap()) {
  1889. // Map the filename with the brackets still attached. If the name doesn't
  1890. // map to anything, fall back on the filename we've already gotten the
  1891. // spelling for.
  1892. StringRef NewName = HeaderInfo.MapHeaderToIncludeAlias(OriginalFilename);
  1893. if (!NewName.empty())
  1894. Filename = NewName;
  1895. }
  1896. // Search include directories.
  1897. bool IsMapped = false;
  1898. bool IsFrameworkFound = false;
  1899. ConstSearchDirIterator CurDir = nullptr;
  1900. SmallString<1024> SearchPath;
  1901. SmallString<1024> RelativePath;
  1902. // We get the raw path only if we have 'Callbacks' to which we later pass
  1903. // the path.
  1904. ModuleMap::KnownHeader SuggestedModule;
  1905. SourceLocation FilenameLoc = FilenameTok.getLocation();
  1906. StringRef LookupFilename = Filename;
  1907. // Normalize slashes when compiling with -fms-extensions on non-Windows. This
  1908. // is unnecessary on Windows since the filesystem there handles backslashes.
  1909. SmallString<128> NormalizedPath;
  1910. llvm::sys::path::Style BackslashStyle = llvm::sys::path::Style::native;
  1911. if (is_style_posix(BackslashStyle) && LangOpts.MicrosoftExt) {
  1912. NormalizedPath = Filename.str();
  1913. llvm::sys::path::native(NormalizedPath);
  1914. LookupFilename = NormalizedPath;
  1915. BackslashStyle = llvm::sys::path::Style::windows;
  1916. }
  1917. OptionalFileEntryRef File = LookupHeaderIncludeOrImport(
  1918. &CurDir, Filename, FilenameLoc, FilenameRange, FilenameTok,
  1919. IsFrameworkFound, IsImportDecl, IsMapped, LookupFrom, LookupFromFile,
  1920. LookupFilename, RelativePath, SearchPath, SuggestedModule, isAngled);
  1921. if (usingPCHWithThroughHeader() && SkippingUntilPCHThroughHeader) {
  1922. if (File && isPCHThroughHeader(&File->getFileEntry()))
  1923. SkippingUntilPCHThroughHeader = false;
  1924. return {ImportAction::None};
  1925. }
  1926. // Should we enter the source file? Set to Skip if either the source file is
  1927. // known to have no effect beyond its effect on module visibility -- that is,
  1928. // if it's got an include guard that is already defined, set to Import if it
  1929. // is a modular header we've already built and should import.
  1930. // For C++20 Modules
  1931. // [cpp.include]/7 If the header identified by the header-name denotes an
  1932. // importable header, it is implementation-defined whether the #include
  1933. // preprocessing directive is instead replaced by an import directive.
  1934. // For this implementation, the translation is permitted when we are parsing
  1935. // the Global Module Fragment, and not otherwise (the cases where it would be
  1936. // valid to replace an include with an import are highly constrained once in
  1937. // named module purview; this choice avoids considerable complexity in
  1938. // determining valid cases).
  1939. enum { Enter, Import, Skip, IncludeLimitReached } Action = Enter;
  1940. if (PPOpts->SingleFileParseMode)
  1941. Action = IncludeLimitReached;
  1942. // If we've reached the max allowed include depth, it is usually due to an
  1943. // include cycle. Don't enter already processed files again as it can lead to
  1944. // reaching the max allowed include depth again.
  1945. if (Action == Enter && HasReachedMaxIncludeDepth && File &&
  1946. alreadyIncluded(*File))
  1947. Action = IncludeLimitReached;
  1948. // FIXME: We do not have a good way to disambiguate C++ clang modules from
  1949. // C++ standard modules (other than use/non-use of Header Units).
  1950. Module *SM = SuggestedModule.getModule();
  1951. bool MaybeTranslateInclude =
  1952. Action == Enter && File && SM && !SM->isForBuilding(getLangOpts());
  1953. // Maybe a usable Header Unit
  1954. bool UsableHeaderUnit = false;
  1955. if (getLangOpts().CPlusPlusModules && SM && SM->isHeaderUnit()) {
  1956. if (TrackGMFState.inGMF() || IsImportDecl)
  1957. UsableHeaderUnit = true;
  1958. else if (!IsImportDecl) {
  1959. // This is a Header Unit that we do not include-translate
  1960. SuggestedModule = ModuleMap::KnownHeader();
  1961. SM = nullptr;
  1962. }
  1963. }
  1964. // Maybe a usable clang header module.
  1965. bool UsableClangHeaderModule =
  1966. (getLangOpts().CPlusPlusModules || getLangOpts().Modules) && SM &&
  1967. !SM->isHeaderUnit();
  1968. // Determine whether we should try to import the module for this #include, if
  1969. // there is one. Don't do so if precompiled module support is disabled or we
  1970. // are processing this module textually (because we're building the module).
  1971. if (MaybeTranslateInclude && (UsableHeaderUnit || UsableClangHeaderModule)) {
  1972. // If this include corresponds to a module but that module is
  1973. // unavailable, diagnose the situation and bail out.
  1974. // FIXME: Remove this; loadModule does the same check (but produces
  1975. // slightly worse diagnostics).
  1976. if (checkModuleIsAvailable(getLangOpts(), getTargetInfo(), getDiagnostics(),
  1977. SuggestedModule.getModule())) {
  1978. Diag(FilenameTok.getLocation(),
  1979. diag::note_implicit_top_level_module_import_here)
  1980. << SuggestedModule.getModule()->getTopLevelModuleName();
  1981. return {ImportAction::None};
  1982. }
  1983. // Compute the module access path corresponding to this module.
  1984. // FIXME: Should we have a second loadModule() overload to avoid this
  1985. // extra lookup step?
  1986. SmallVector<std::pair<IdentifierInfo *, SourceLocation>, 2> Path;
  1987. for (Module *Mod = SM; Mod; Mod = Mod->Parent)
  1988. Path.push_back(std::make_pair(getIdentifierInfo(Mod->Name),
  1989. FilenameTok.getLocation()));
  1990. std::reverse(Path.begin(), Path.end());
  1991. // Warn that we're replacing the include/import with a module import.
  1992. if (!IsImportDecl)
  1993. diagnoseAutoModuleImport(*this, StartLoc, IncludeTok, Path, CharEnd);
  1994. // Load the module to import its macros. We'll make the declarations
  1995. // visible when the parser gets here.
  1996. // FIXME: Pass SuggestedModule in here rather than converting it to a path
  1997. // and making the module loader convert it back again.
  1998. ModuleLoadResult Imported = TheModuleLoader.loadModule(
  1999. IncludeTok.getLocation(), Path, Module::Hidden,
  2000. /*IsInclusionDirective=*/true);
  2001. assert((Imported == nullptr || Imported == SuggestedModule.getModule()) &&
  2002. "the imported module is different than the suggested one");
  2003. if (Imported) {
  2004. Action = Import;
  2005. } else if (Imported.isMissingExpected()) {
  2006. markClangModuleAsAffecting(
  2007. static_cast<Module *>(Imported)->getTopLevelModule());
  2008. // We failed to find a submodule that we assumed would exist (because it
  2009. // was in the directory of an umbrella header, for instance), but no
  2010. // actual module containing it exists (because the umbrella header is
  2011. // incomplete). Treat this as a textual inclusion.
  2012. SuggestedModule = ModuleMap::KnownHeader();
  2013. SM = nullptr;
  2014. } else if (Imported.isConfigMismatch()) {
  2015. // On a configuration mismatch, enter the header textually. We still know
  2016. // that it's part of the corresponding module.
  2017. } else {
  2018. // We hit an error processing the import. Bail out.
  2019. if (hadModuleLoaderFatalFailure()) {
  2020. // With a fatal failure in the module loader, we abort parsing.
  2021. Token &Result = IncludeTok;
  2022. assert(CurLexer && "#include but no current lexer set!");
  2023. Result.startToken();
  2024. CurLexer->FormTokenWithChars(Result, CurLexer->BufferEnd, tok::eof);
  2025. CurLexer->cutOffLexing();
  2026. }
  2027. return {ImportAction::None};
  2028. }
  2029. }
  2030. // The #included file will be considered to be a system header if either it is
  2031. // in a system include directory, or if the #includer is a system include
  2032. // header.
  2033. SrcMgr::CharacteristicKind FileCharacter =
  2034. SourceMgr.getFileCharacteristic(FilenameTok.getLocation());
  2035. if (File)
  2036. FileCharacter = std::max(HeaderInfo.getFileDirFlavor(&File->getFileEntry()),
  2037. FileCharacter);
  2038. // If this is a '#import' or an import-declaration, don't re-enter the file.
  2039. //
  2040. // FIXME: If we have a suggested module for a '#include', and we've already
  2041. // visited this file, don't bother entering it again. We know it has no
  2042. // further effect.
  2043. bool EnterOnce =
  2044. IsImportDecl ||
  2045. IncludeTok.getIdentifierInfo()->getPPKeywordID() == tok::pp_import;
  2046. bool IsFirstIncludeOfFile = false;
  2047. // Ask HeaderInfo if we should enter this #include file. If not, #including
  2048. // this file will have no effect.
  2049. if (Action == Enter && File &&
  2050. !HeaderInfo.ShouldEnterIncludeFile(*this, &File->getFileEntry(),
  2051. EnterOnce, getLangOpts().Modules, SM,
  2052. IsFirstIncludeOfFile)) {
  2053. // C++ standard modules:
  2054. // If we are not in the GMF, then we textually include only
  2055. // clang modules:
  2056. // Even if we've already preprocessed this header once and know that we
  2057. // don't need to see its contents again, we still need to import it if it's
  2058. // modular because we might not have imported it from this submodule before.
  2059. //
  2060. // FIXME: We don't do this when compiling a PCH because the AST
  2061. // serialization layer can't cope with it. This means we get local
  2062. // submodule visibility semantics wrong in that case.
  2063. if (UsableHeaderUnit && !getLangOpts().CompilingPCH)
  2064. Action = TrackGMFState.inGMF() ? Import : Skip;
  2065. else
  2066. Action = (SuggestedModule && !getLangOpts().CompilingPCH) ? Import : Skip;
  2067. }
  2068. // Check for circular inclusion of the main file.
  2069. // We can't generate a consistent preamble with regard to the conditional
  2070. // stack if the main file is included again as due to the preamble bounds
  2071. // some directives (e.g. #endif of a header guard) will never be seen.
  2072. // Since this will lead to confusing errors, avoid the inclusion.
  2073. if (Action == Enter && File && PreambleConditionalStack.isRecording() &&
  2074. SourceMgr.isMainFile(File->getFileEntry())) {
  2075. Diag(FilenameTok.getLocation(),
  2076. diag::err_pp_including_mainfile_in_preamble);
  2077. return {ImportAction::None};
  2078. }
  2079. if (Callbacks && !IsImportDecl) {
  2080. // Notify the callback object that we've seen an inclusion directive.
  2081. // FIXME: Use a different callback for a pp-import?
  2082. Callbacks->InclusionDirective(HashLoc, IncludeTok, LookupFilename, isAngled,
  2083. FilenameRange, File, SearchPath, RelativePath,
  2084. Action == Import ? SuggestedModule.getModule()
  2085. : nullptr,
  2086. FileCharacter);
  2087. if (Action == Skip && File)
  2088. Callbacks->FileSkipped(*File, FilenameTok, FileCharacter);
  2089. }
  2090. if (!File)
  2091. return {ImportAction::None};
  2092. // If this is a C++20 pp-import declaration, diagnose if we didn't find any
  2093. // module corresponding to the named header.
  2094. if (IsImportDecl && !SuggestedModule) {
  2095. Diag(FilenameTok, diag::err_header_import_not_header_unit)
  2096. << OriginalFilename << File->getName();
  2097. return {ImportAction::None};
  2098. }
  2099. // Issue a diagnostic if the name of the file on disk has a different case
  2100. // than the one we're about to open.
  2101. const bool CheckIncludePathPortability =
  2102. !LangOpts.CaseInsensitive && !IsMapped && !File->getFileEntry().tryGetRealPathName().empty();
  2103. if (CheckIncludePathPortability) {
  2104. StringRef Name = LookupFilename;
  2105. StringRef NameWithoriginalSlashes = Filename;
  2106. #if defined(_WIN32)
  2107. // Skip UNC prefix if present. (tryGetRealPathName() always
  2108. // returns a path with the prefix skipped.)
  2109. bool NameWasUNC = Name.consume_front("\\\\?\\");
  2110. NameWithoriginalSlashes.consume_front("\\\\?\\");
  2111. #endif
  2112. StringRef RealPathName = File->getFileEntry().tryGetRealPathName();
  2113. SmallVector<StringRef, 16> Components(llvm::sys::path::begin(Name),
  2114. llvm::sys::path::end(Name));
  2115. #if defined(_WIN32)
  2116. // -Wnonportable-include-path is designed to diagnose includes using
  2117. // case even on systems with a case-insensitive file system.
  2118. // On Windows, RealPathName always starts with an upper-case drive
  2119. // letter for absolute paths, but Name might start with either
  2120. // case depending on if `cd c:\foo` or `cd C:\foo` was used in the shell.
  2121. // ("foo" will always have on-disk case, no matter which case was
  2122. // used in the cd command). To not emit this warning solely for
  2123. // the drive letter, whose case is dependent on if `cd` is used
  2124. // with upper- or lower-case drive letters, always consider the
  2125. // given drive letter case as correct for the purpose of this warning.
  2126. SmallString<128> FixedDriveRealPath;
  2127. if (llvm::sys::path::is_absolute(Name) &&
  2128. llvm::sys::path::is_absolute(RealPathName) &&
  2129. toLowercase(Name[0]) == toLowercase(RealPathName[0]) &&
  2130. isLowercase(Name[0]) != isLowercase(RealPathName[0])) {
  2131. assert(Components.size() >= 3 && "should have drive, backslash, name");
  2132. assert(Components[0].size() == 2 && "should start with drive");
  2133. assert(Components[0][1] == ':' && "should have colon");
  2134. FixedDriveRealPath = (Name.substr(0, 1) + RealPathName.substr(1)).str();
  2135. RealPathName = FixedDriveRealPath;
  2136. }
  2137. #endif
  2138. if (trySimplifyPath(Components, RealPathName)) {
  2139. SmallString<128> Path;
  2140. Path.reserve(Name.size()+2);
  2141. Path.push_back(isAngled ? '<' : '"');
  2142. const auto IsSep = [BackslashStyle](char c) {
  2143. return llvm::sys::path::is_separator(c, BackslashStyle);
  2144. };
  2145. for (auto Component : Components) {
  2146. // On POSIX, Components will contain a single '/' as first element
  2147. // exactly if Name is an absolute path.
  2148. // On Windows, it will contain "C:" followed by '\' for absolute paths.
  2149. // The drive letter is optional for absolute paths on Windows, but
  2150. // clang currently cannot process absolute paths in #include lines that
  2151. // don't have a drive.
  2152. // If the first entry in Components is a directory separator,
  2153. // then the code at the bottom of this loop that keeps the original
  2154. // directory separator style copies it. If the second entry is
  2155. // a directory separator (the C:\ case), then that separator already
  2156. // got copied when the C: was processed and we want to skip that entry.
  2157. if (!(Component.size() == 1 && IsSep(Component[0])))
  2158. Path.append(Component);
  2159. else if (!Path.empty())
  2160. continue;
  2161. // Append the separator(s) the user used, or the close quote
  2162. if (Path.size() > NameWithoriginalSlashes.size()) {
  2163. Path.push_back(isAngled ? '>' : '"');
  2164. continue;
  2165. }
  2166. assert(IsSep(NameWithoriginalSlashes[Path.size()-1]));
  2167. do
  2168. Path.push_back(NameWithoriginalSlashes[Path.size()-1]);
  2169. while (Path.size() <= NameWithoriginalSlashes.size() &&
  2170. IsSep(NameWithoriginalSlashes[Path.size()-1]));
  2171. }
  2172. #if defined(_WIN32)
  2173. // Restore UNC prefix if it was there.
  2174. if (NameWasUNC)
  2175. Path = (Path.substr(0, 1) + "\\\\?\\" + Path.substr(1)).str();
  2176. #endif
  2177. // For user files and known standard headers, issue a diagnostic.
  2178. // For other system headers, don't. They can be controlled separately.
  2179. auto DiagId =
  2180. (FileCharacter == SrcMgr::C_User || warnByDefaultOnWrongCase(Name))
  2181. ? diag::pp_nonportable_path
  2182. : diag::pp_nonportable_system_path;
  2183. Diag(FilenameTok, DiagId) << Path <<
  2184. FixItHint::CreateReplacement(FilenameRange, Path);
  2185. }
  2186. }
  2187. switch (Action) {
  2188. case Skip:
  2189. // If we don't need to enter the file, stop now.
  2190. if (SM)
  2191. return {ImportAction::SkippedModuleImport, SM};
  2192. return {ImportAction::None};
  2193. case IncludeLimitReached:
  2194. // If we reached our include limit and don't want to enter any more files,
  2195. // don't go any further.
  2196. return {ImportAction::None};
  2197. case Import: {
  2198. // If this is a module import, make it visible if needed.
  2199. assert(SM && "no module to import");
  2200. makeModuleVisible(SM, EndLoc);
  2201. if (IncludeTok.getIdentifierInfo()->getPPKeywordID() ==
  2202. tok::pp___include_macros)
  2203. return {ImportAction::None};
  2204. return {ImportAction::ModuleImport, SM};
  2205. }
  2206. case Enter:
  2207. break;
  2208. }
  2209. // Check that we don't have infinite #include recursion.
  2210. if (IncludeMacroStack.size() == MaxAllowedIncludeStackDepth-1) {
  2211. Diag(FilenameTok, diag::err_pp_include_too_deep);
  2212. HasReachedMaxIncludeDepth = true;
  2213. return {ImportAction::None};
  2214. }
  2215. // Look up the file, create a File ID for it.
  2216. SourceLocation IncludePos = FilenameTok.getLocation();
  2217. // If the filename string was the result of macro expansions, set the include
  2218. // position on the file where it will be included and after the expansions.
  2219. if (IncludePos.isMacroID())
  2220. IncludePos = SourceMgr.getExpansionRange(IncludePos).getEnd();
  2221. FileID FID = SourceMgr.createFileID(*File, IncludePos, FileCharacter);
  2222. if (!FID.isValid()) {
  2223. TheModuleLoader.HadFatalFailure = true;
  2224. return ImportAction::Failure;
  2225. }
  2226. // If all is good, enter the new file!
  2227. if (EnterSourceFile(FID, CurDir, FilenameTok.getLocation(),
  2228. IsFirstIncludeOfFile))
  2229. return {ImportAction::None};
  2230. // Determine if we're switching to building a new submodule, and which one.
  2231. // This does not apply for C++20 modules header units.
  2232. if (SM && !SM->isHeaderUnit()) {
  2233. if (SM->getTopLevelModule()->ShadowingModule) {
  2234. // We are building a submodule that belongs to a shadowed module. This
  2235. // means we find header files in the shadowed module.
  2236. Diag(SM->DefinitionLoc, diag::err_module_build_shadowed_submodule)
  2237. << SM->getFullModuleName();
  2238. Diag(SM->getTopLevelModule()->ShadowingModule->DefinitionLoc,
  2239. diag::note_previous_definition);
  2240. return {ImportAction::None};
  2241. }
  2242. // When building a pch, -fmodule-name tells the compiler to textually
  2243. // include headers in the specified module. We are not building the
  2244. // specified module.
  2245. //
  2246. // FIXME: This is the wrong way to handle this. We should produce a PCH
  2247. // that behaves the same as the header would behave in a compilation using
  2248. // that PCH, which means we should enter the submodule. We need to teach
  2249. // the AST serialization layer to deal with the resulting AST.
  2250. if (getLangOpts().CompilingPCH && SM->isForBuilding(getLangOpts()))
  2251. return {ImportAction::None};
  2252. assert(!CurLexerSubmodule && "should not have marked this as a module yet");
  2253. CurLexerSubmodule = SM;
  2254. // Let the macro handling code know that any future macros are within
  2255. // the new submodule.
  2256. EnterSubmodule(SM, EndLoc, /*ForPragma*/ false);
  2257. // Let the parser know that any future declarations are within the new
  2258. // submodule.
  2259. // FIXME: There's no point doing this if we're handling a #__include_macros
  2260. // directive.
  2261. return {ImportAction::ModuleBegin, SM};
  2262. }
  2263. assert(!IsImportDecl && "failed to diagnose missing module for import decl");
  2264. return {ImportAction::None};
  2265. }
  2266. /// HandleIncludeNextDirective - Implements \#include_next.
  2267. ///
  2268. void Preprocessor::HandleIncludeNextDirective(SourceLocation HashLoc,
  2269. Token &IncludeNextTok) {
  2270. Diag(IncludeNextTok, diag::ext_pp_include_next_directive);
  2271. ConstSearchDirIterator Lookup = nullptr;
  2272. const FileEntry *LookupFromFile;
  2273. std::tie(Lookup, LookupFromFile) = getIncludeNextStart(IncludeNextTok);
  2274. return HandleIncludeDirective(HashLoc, IncludeNextTok, Lookup,
  2275. LookupFromFile);
  2276. }
  2277. /// HandleMicrosoftImportDirective - Implements \#import for Microsoft Mode
  2278. void Preprocessor::HandleMicrosoftImportDirective(Token &Tok) {
  2279. // The Microsoft #import directive takes a type library and generates header
  2280. // files from it, and includes those. This is beyond the scope of what clang
  2281. // does, so we ignore it and error out. However, #import can optionally have
  2282. // trailing attributes that span multiple lines. We're going to eat those
  2283. // so we can continue processing from there.
  2284. Diag(Tok, diag::err_pp_import_directive_ms );
  2285. // Read tokens until we get to the end of the directive. Note that the
  2286. // directive can be split over multiple lines using the backslash character.
  2287. DiscardUntilEndOfDirective();
  2288. }
  2289. /// HandleImportDirective - Implements \#import.
  2290. ///
  2291. void Preprocessor::HandleImportDirective(SourceLocation HashLoc,
  2292. Token &ImportTok) {
  2293. if (!LangOpts.ObjC) { // #import is standard for ObjC.
  2294. if (LangOpts.MSVCCompat)
  2295. return HandleMicrosoftImportDirective(ImportTok);
  2296. Diag(ImportTok, diag::ext_pp_import_directive);
  2297. }
  2298. return HandleIncludeDirective(HashLoc, ImportTok);
  2299. }
  2300. /// HandleIncludeMacrosDirective - The -imacros command line option turns into a
  2301. /// pseudo directive in the predefines buffer. This handles it by sucking all
  2302. /// tokens through the preprocessor and discarding them (only keeping the side
  2303. /// effects on the preprocessor).
  2304. void Preprocessor::HandleIncludeMacrosDirective(SourceLocation HashLoc,
  2305. Token &IncludeMacrosTok) {
  2306. // This directive should only occur in the predefines buffer. If not, emit an
  2307. // error and reject it.
  2308. SourceLocation Loc = IncludeMacrosTok.getLocation();
  2309. if (SourceMgr.getBufferName(Loc) != "<built-in>") {
  2310. Diag(IncludeMacrosTok.getLocation(),
  2311. diag::pp_include_macros_out_of_predefines);
  2312. DiscardUntilEndOfDirective();
  2313. return;
  2314. }
  2315. // Treat this as a normal #include for checking purposes. If this is
  2316. // successful, it will push a new lexer onto the include stack.
  2317. HandleIncludeDirective(HashLoc, IncludeMacrosTok);
  2318. Token TmpTok;
  2319. do {
  2320. Lex(TmpTok);
  2321. assert(TmpTok.isNot(tok::eof) && "Didn't find end of -imacros!");
  2322. } while (TmpTok.isNot(tok::hashhash));
  2323. }
  2324. //===----------------------------------------------------------------------===//
  2325. // Preprocessor Macro Directive Handling.
  2326. //===----------------------------------------------------------------------===//
  2327. /// ReadMacroParameterList - The ( starting a parameter list of a macro
  2328. /// definition has just been read. Lex the rest of the parameters and the
  2329. /// closing ), updating MI with what we learn. Return true if an error occurs
  2330. /// parsing the param list.
  2331. bool Preprocessor::ReadMacroParameterList(MacroInfo *MI, Token &Tok) {
  2332. SmallVector<IdentifierInfo*, 32> Parameters;
  2333. while (true) {
  2334. LexUnexpandedToken(Tok);
  2335. switch (Tok.getKind()) {
  2336. case tok::r_paren:
  2337. // Found the end of the parameter list.
  2338. if (Parameters.empty()) // #define FOO()
  2339. return false;
  2340. // Otherwise we have #define FOO(A,)
  2341. Diag(Tok, diag::err_pp_expected_ident_in_arg_list);
  2342. return true;
  2343. case tok::ellipsis: // #define X(... -> C99 varargs
  2344. if (!LangOpts.C99)
  2345. Diag(Tok, LangOpts.CPlusPlus11 ?
  2346. diag::warn_cxx98_compat_variadic_macro :
  2347. diag::ext_variadic_macro);
  2348. // OpenCL v1.2 s6.9.e: variadic macros are not supported.
  2349. if (LangOpts.OpenCL && !LangOpts.OpenCLCPlusPlus) {
  2350. Diag(Tok, diag::ext_pp_opencl_variadic_macros);
  2351. }
  2352. // Lex the token after the identifier.
  2353. LexUnexpandedToken(Tok);
  2354. if (Tok.isNot(tok::r_paren)) {
  2355. Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
  2356. return true;
  2357. }
  2358. // Add the __VA_ARGS__ identifier as a parameter.
  2359. Parameters.push_back(Ident__VA_ARGS__);
  2360. MI->setIsC99Varargs();
  2361. MI->setParameterList(Parameters, BP);
  2362. return false;
  2363. case tok::eod: // #define X(
  2364. Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
  2365. return true;
  2366. default:
  2367. // Handle keywords and identifiers here to accept things like
  2368. // #define Foo(for) for.
  2369. IdentifierInfo *II = Tok.getIdentifierInfo();
  2370. if (!II) {
  2371. // #define X(1
  2372. Diag(Tok, diag::err_pp_invalid_tok_in_arg_list);
  2373. return true;
  2374. }
  2375. // If this is already used as a parameter, it is used multiple times (e.g.
  2376. // #define X(A,A.
  2377. if (llvm::is_contained(Parameters, II)) { // C99 6.10.3p6
  2378. Diag(Tok, diag::err_pp_duplicate_name_in_arg_list) << II;
  2379. return true;
  2380. }
  2381. // Add the parameter to the macro info.
  2382. Parameters.push_back(II);
  2383. // Lex the token after the identifier.
  2384. LexUnexpandedToken(Tok);
  2385. switch (Tok.getKind()) {
  2386. default: // #define X(A B
  2387. Diag(Tok, diag::err_pp_expected_comma_in_arg_list);
  2388. return true;
  2389. case tok::r_paren: // #define X(A)
  2390. MI->setParameterList(Parameters, BP);
  2391. return false;
  2392. case tok::comma: // #define X(A,
  2393. break;
  2394. case tok::ellipsis: // #define X(A... -> GCC extension
  2395. // Diagnose extension.
  2396. Diag(Tok, diag::ext_named_variadic_macro);
  2397. // Lex the token after the identifier.
  2398. LexUnexpandedToken(Tok);
  2399. if (Tok.isNot(tok::r_paren)) {
  2400. Diag(Tok, diag::err_pp_missing_rparen_in_macro_def);
  2401. return true;
  2402. }
  2403. MI->setIsGNUVarargs();
  2404. MI->setParameterList(Parameters, BP);
  2405. return false;
  2406. }
  2407. }
  2408. }
  2409. }
  2410. static bool isConfigurationPattern(Token &MacroName, MacroInfo *MI,
  2411. const LangOptions &LOptions) {
  2412. if (MI->getNumTokens() == 1) {
  2413. const Token &Value = MI->getReplacementToken(0);
  2414. // Macro that is identity, like '#define inline inline' is a valid pattern.
  2415. if (MacroName.getKind() == Value.getKind())
  2416. return true;
  2417. // Macro that maps a keyword to the same keyword decorated with leading/
  2418. // trailing underscores is a valid pattern:
  2419. // #define inline __inline
  2420. // #define inline __inline__
  2421. // #define inline _inline (in MS compatibility mode)
  2422. StringRef MacroText = MacroName.getIdentifierInfo()->getName();
  2423. if (IdentifierInfo *II = Value.getIdentifierInfo()) {
  2424. if (!II->isKeyword(LOptions))
  2425. return false;
  2426. StringRef ValueText = II->getName();
  2427. StringRef TrimmedValue = ValueText;
  2428. if (!ValueText.startswith("__")) {
  2429. if (ValueText.startswith("_"))
  2430. TrimmedValue = TrimmedValue.drop_front(1);
  2431. else
  2432. return false;
  2433. } else {
  2434. TrimmedValue = TrimmedValue.drop_front(2);
  2435. if (TrimmedValue.endswith("__"))
  2436. TrimmedValue = TrimmedValue.drop_back(2);
  2437. }
  2438. return TrimmedValue.equals(MacroText);
  2439. } else {
  2440. return false;
  2441. }
  2442. }
  2443. // #define inline
  2444. return MacroName.isOneOf(tok::kw_extern, tok::kw_inline, tok::kw_static,
  2445. tok::kw_const) &&
  2446. MI->getNumTokens() == 0;
  2447. }
  2448. // ReadOptionalMacroParameterListAndBody - This consumes all (i.e. the
  2449. // entire line) of the macro's tokens and adds them to MacroInfo, and while
  2450. // doing so performs certain validity checks including (but not limited to):
  2451. // - # (stringization) is followed by a macro parameter
  2452. //
  2453. // Returns a nullptr if an invalid sequence of tokens is encountered or returns
  2454. // a pointer to a MacroInfo object.
  2455. MacroInfo *Preprocessor::ReadOptionalMacroParameterListAndBody(
  2456. const Token &MacroNameTok, const bool ImmediatelyAfterHeaderGuard) {
  2457. Token LastTok = MacroNameTok;
  2458. // Create the new macro.
  2459. MacroInfo *const MI = AllocateMacroInfo(MacroNameTok.getLocation());
  2460. Token Tok;
  2461. LexUnexpandedToken(Tok);
  2462. // Ensure we consume the rest of the macro body if errors occur.
  2463. auto _ = llvm::make_scope_exit([&]() {
  2464. // The flag indicates if we are still waiting for 'eod'.
  2465. if (CurLexer->ParsingPreprocessorDirective)
  2466. DiscardUntilEndOfDirective();
  2467. });
  2468. // Used to un-poison and then re-poison identifiers of the __VA_ARGS__ ilk
  2469. // within their appropriate context.
  2470. VariadicMacroScopeGuard VariadicMacroScopeGuard(*this);
  2471. // If this is a function-like macro definition, parse the argument list,
  2472. // marking each of the identifiers as being used as macro arguments. Also,
  2473. // check other constraints on the first token of the macro body.
  2474. if (Tok.is(tok::eod)) {
  2475. if (ImmediatelyAfterHeaderGuard) {
  2476. // Save this macro information since it may part of a header guard.
  2477. CurPPLexer->MIOpt.SetDefinedMacro(MacroNameTok.getIdentifierInfo(),
  2478. MacroNameTok.getLocation());
  2479. }
  2480. // If there is no body to this macro, we have no special handling here.
  2481. } else if (Tok.hasLeadingSpace()) {
  2482. // This is a normal token with leading space. Clear the leading space
  2483. // marker on the first token to get proper expansion.
  2484. Tok.clearFlag(Token::LeadingSpace);
  2485. } else if (Tok.is(tok::l_paren)) {
  2486. // This is a function-like macro definition. Read the argument list.
  2487. MI->setIsFunctionLike();
  2488. if (ReadMacroParameterList(MI, LastTok))
  2489. return nullptr;
  2490. // If this is a definition of an ISO C/C++ variadic function-like macro (not
  2491. // using the GNU named varargs extension) inform our variadic scope guard
  2492. // which un-poisons and re-poisons certain identifiers (e.g. __VA_ARGS__)
  2493. // allowed only within the definition of a variadic macro.
  2494. if (MI->isC99Varargs()) {
  2495. VariadicMacroScopeGuard.enterScope();
  2496. }
  2497. // Read the first token after the arg list for down below.
  2498. LexUnexpandedToken(Tok);
  2499. } else if (LangOpts.C99 || LangOpts.CPlusPlus11) {
  2500. // C99 requires whitespace between the macro definition and the body. Emit
  2501. // a diagnostic for something like "#define X+".
  2502. Diag(Tok, diag::ext_c99_whitespace_required_after_macro_name);
  2503. } else {
  2504. // C90 6.8 TC1 says: "In the definition of an object-like macro, if the
  2505. // first character of a replacement list is not a character required by
  2506. // subclause 5.2.1, then there shall be white-space separation between the
  2507. // identifier and the replacement list.". 5.2.1 lists this set:
  2508. // "A-Za-z0-9!"#%&'()*+,_./:;<=>?[\]^_{|}~" as well as whitespace, which
  2509. // is irrelevant here.
  2510. bool isInvalid = false;
  2511. if (Tok.is(tok::at)) // @ is not in the list above.
  2512. isInvalid = true;
  2513. else if (Tok.is(tok::unknown)) {
  2514. // If we have an unknown token, it is something strange like "`". Since
  2515. // all of valid characters would have lexed into a single character
  2516. // token of some sort, we know this is not a valid case.
  2517. isInvalid = true;
  2518. }
  2519. if (isInvalid)
  2520. Diag(Tok, diag::ext_missing_whitespace_after_macro_name);
  2521. else
  2522. Diag(Tok, diag::warn_missing_whitespace_after_macro_name);
  2523. }
  2524. if (!Tok.is(tok::eod))
  2525. LastTok = Tok;
  2526. SmallVector<Token, 16> Tokens;
  2527. // Read the rest of the macro body.
  2528. if (MI->isObjectLike()) {
  2529. // Object-like macros are very simple, just read their body.
  2530. while (Tok.isNot(tok::eod)) {
  2531. LastTok = Tok;
  2532. Tokens.push_back(Tok);
  2533. // Get the next token of the macro.
  2534. LexUnexpandedToken(Tok);
  2535. }
  2536. } else {
  2537. // Otherwise, read the body of a function-like macro. While we are at it,
  2538. // check C99 6.10.3.2p1: ensure that # operators are followed by macro
  2539. // parameters in function-like macro expansions.
  2540. VAOptDefinitionContext VAOCtx(*this);
  2541. while (Tok.isNot(tok::eod)) {
  2542. LastTok = Tok;
  2543. if (!Tok.isOneOf(tok::hash, tok::hashat, tok::hashhash)) {
  2544. Tokens.push_back(Tok);
  2545. if (VAOCtx.isVAOptToken(Tok)) {
  2546. // If we're already within a VAOPT, emit an error.
  2547. if (VAOCtx.isInVAOpt()) {
  2548. Diag(Tok, diag::err_pp_vaopt_nested_use);
  2549. return nullptr;
  2550. }
  2551. // Ensure VAOPT is followed by a '(' .
  2552. LexUnexpandedToken(Tok);
  2553. if (Tok.isNot(tok::l_paren)) {
  2554. Diag(Tok, diag::err_pp_missing_lparen_in_vaopt_use);
  2555. return nullptr;
  2556. }
  2557. Tokens.push_back(Tok);
  2558. VAOCtx.sawVAOptFollowedByOpeningParens(Tok.getLocation());
  2559. LexUnexpandedToken(Tok);
  2560. if (Tok.is(tok::hashhash)) {
  2561. Diag(Tok, diag::err_vaopt_paste_at_start);
  2562. return nullptr;
  2563. }
  2564. continue;
  2565. } else if (VAOCtx.isInVAOpt()) {
  2566. if (Tok.is(tok::r_paren)) {
  2567. if (VAOCtx.sawClosingParen()) {
  2568. assert(Tokens.size() >= 3 &&
  2569. "Must have seen at least __VA_OPT__( "
  2570. "and a subsequent tok::r_paren");
  2571. if (Tokens[Tokens.size() - 2].is(tok::hashhash)) {
  2572. Diag(Tok, diag::err_vaopt_paste_at_end);
  2573. return nullptr;
  2574. }
  2575. }
  2576. } else if (Tok.is(tok::l_paren)) {
  2577. VAOCtx.sawOpeningParen(Tok.getLocation());
  2578. }
  2579. }
  2580. // Get the next token of the macro.
  2581. LexUnexpandedToken(Tok);
  2582. continue;
  2583. }
  2584. // If we're in -traditional mode, then we should ignore stringification
  2585. // and token pasting. Mark the tokens as unknown so as not to confuse
  2586. // things.
  2587. if (getLangOpts().TraditionalCPP) {
  2588. Tok.setKind(tok::unknown);
  2589. Tokens.push_back(Tok);
  2590. // Get the next token of the macro.
  2591. LexUnexpandedToken(Tok);
  2592. continue;
  2593. }
  2594. if (Tok.is(tok::hashhash)) {
  2595. // If we see token pasting, check if it looks like the gcc comma
  2596. // pasting extension. We'll use this information to suppress
  2597. // diagnostics later on.
  2598. // Get the next token of the macro.
  2599. LexUnexpandedToken(Tok);
  2600. if (Tok.is(tok::eod)) {
  2601. Tokens.push_back(LastTok);
  2602. break;
  2603. }
  2604. if (!Tokens.empty() && Tok.getIdentifierInfo() == Ident__VA_ARGS__ &&
  2605. Tokens[Tokens.size() - 1].is(tok::comma))
  2606. MI->setHasCommaPasting();
  2607. // Things look ok, add the '##' token to the macro.
  2608. Tokens.push_back(LastTok);
  2609. continue;
  2610. }
  2611. // Our Token is a stringization operator.
  2612. // Get the next token of the macro.
  2613. LexUnexpandedToken(Tok);
  2614. // Check for a valid macro arg identifier or __VA_OPT__.
  2615. if (!VAOCtx.isVAOptToken(Tok) &&
  2616. (Tok.getIdentifierInfo() == nullptr ||
  2617. MI->getParameterNum(Tok.getIdentifierInfo()) == -1)) {
  2618. // If this is assembler-with-cpp mode, we accept random gibberish after
  2619. // the '#' because '#' is often a comment character. However, change
  2620. // the kind of the token to tok::unknown so that the preprocessor isn't
  2621. // confused.
  2622. if (getLangOpts().AsmPreprocessor && Tok.isNot(tok::eod)) {
  2623. LastTok.setKind(tok::unknown);
  2624. Tokens.push_back(LastTok);
  2625. continue;
  2626. } else {
  2627. Diag(Tok, diag::err_pp_stringize_not_parameter)
  2628. << LastTok.is(tok::hashat);
  2629. return nullptr;
  2630. }
  2631. }
  2632. // Things look ok, add the '#' and param name tokens to the macro.
  2633. Tokens.push_back(LastTok);
  2634. // If the token following '#' is VAOPT, let the next iteration handle it
  2635. // and check it for correctness, otherwise add the token and prime the
  2636. // loop with the next one.
  2637. if (!VAOCtx.isVAOptToken(Tok)) {
  2638. Tokens.push_back(Tok);
  2639. LastTok = Tok;
  2640. // Get the next token of the macro.
  2641. LexUnexpandedToken(Tok);
  2642. }
  2643. }
  2644. if (VAOCtx.isInVAOpt()) {
  2645. assert(Tok.is(tok::eod) && "Must be at End Of preprocessing Directive");
  2646. Diag(Tok, diag::err_pp_expected_after)
  2647. << LastTok.getKind() << tok::r_paren;
  2648. Diag(VAOCtx.getUnmatchedOpeningParenLoc(), diag::note_matching) << tok::l_paren;
  2649. return nullptr;
  2650. }
  2651. }
  2652. MI->setDefinitionEndLoc(LastTok.getLocation());
  2653. MI->setTokens(Tokens, BP);
  2654. return MI;
  2655. }
  2656. /// HandleDefineDirective - Implements \#define. This consumes the entire macro
  2657. /// line then lets the caller lex the next real token.
  2658. void Preprocessor::HandleDefineDirective(
  2659. Token &DefineTok, const bool ImmediatelyAfterHeaderGuard) {
  2660. ++NumDefined;
  2661. Token MacroNameTok;
  2662. bool MacroShadowsKeyword;
  2663. ReadMacroName(MacroNameTok, MU_Define, &MacroShadowsKeyword);
  2664. // Error reading macro name? If so, diagnostic already issued.
  2665. if (MacroNameTok.is(tok::eod))
  2666. return;
  2667. IdentifierInfo *II = MacroNameTok.getIdentifierInfo();
  2668. // Issue a final pragma warning if we're defining a macro that was has been
  2669. // undefined and is being redefined.
  2670. if (!II->hasMacroDefinition() && II->hadMacroDefinition() && II->isFinal())
  2671. emitFinalMacroWarning(MacroNameTok, /*IsUndef=*/false);
  2672. // If we are supposed to keep comments in #defines, reenable comment saving
  2673. // mode.
  2674. if (CurLexer) CurLexer->SetCommentRetentionState(KeepMacroComments);
  2675. MacroInfo *const MI = ReadOptionalMacroParameterListAndBody(
  2676. MacroNameTok, ImmediatelyAfterHeaderGuard);
  2677. if (!MI) return;
  2678. if (MacroShadowsKeyword &&
  2679. !isConfigurationPattern(MacroNameTok, MI, getLangOpts())) {
  2680. Diag(MacroNameTok, diag::warn_pp_macro_hides_keyword);
  2681. }
  2682. // Check that there is no paste (##) operator at the beginning or end of the
  2683. // replacement list.
  2684. unsigned NumTokens = MI->getNumTokens();
  2685. if (NumTokens != 0) {
  2686. if (MI->getReplacementToken(0).is(tok::hashhash)) {
  2687. Diag(MI->getReplacementToken(0), diag::err_paste_at_start);
  2688. return;
  2689. }
  2690. if (MI->getReplacementToken(NumTokens-1).is(tok::hashhash)) {
  2691. Diag(MI->getReplacementToken(NumTokens-1), diag::err_paste_at_end);
  2692. return;
  2693. }
  2694. }
  2695. // When skipping just warn about macros that do not match.
  2696. if (SkippingUntilPCHThroughHeader) {
  2697. const MacroInfo *OtherMI = getMacroInfo(MacroNameTok.getIdentifierInfo());
  2698. if (!OtherMI || !MI->isIdenticalTo(*OtherMI, *this,
  2699. /*Syntactic=*/LangOpts.MicrosoftExt))
  2700. Diag(MI->getDefinitionLoc(), diag::warn_pp_macro_def_mismatch_with_pch)
  2701. << MacroNameTok.getIdentifierInfo();
  2702. // Issue the diagnostic but allow the change if msvc extensions are enabled
  2703. if (!LangOpts.MicrosoftExt)
  2704. return;
  2705. }
  2706. // Finally, if this identifier already had a macro defined for it, verify that
  2707. // the macro bodies are identical, and issue diagnostics if they are not.
  2708. if (const MacroInfo *OtherMI=getMacroInfo(MacroNameTok.getIdentifierInfo())) {
  2709. // Final macros are hard-mode: they always warn. Even if the bodies are
  2710. // identical. Even if they are in system headers. Even if they are things we
  2711. // would silently allow in the past.
  2712. if (MacroNameTok.getIdentifierInfo()->isFinal())
  2713. emitFinalMacroWarning(MacroNameTok, /*IsUndef=*/false);
  2714. // In Objective-C, ignore attempts to directly redefine the builtin
  2715. // definitions of the ownership qualifiers. It's still possible to
  2716. // #undef them.
  2717. auto isObjCProtectedMacro = [](const IdentifierInfo *II) -> bool {
  2718. return II->isStr("__strong") ||
  2719. II->isStr("__weak") ||
  2720. II->isStr("__unsafe_unretained") ||
  2721. II->isStr("__autoreleasing");
  2722. };
  2723. if (getLangOpts().ObjC &&
  2724. SourceMgr.getFileID(OtherMI->getDefinitionLoc())
  2725. == getPredefinesFileID() &&
  2726. isObjCProtectedMacro(MacroNameTok.getIdentifierInfo())) {
  2727. // Warn if it changes the tokens.
  2728. if ((!getDiagnostics().getSuppressSystemWarnings() ||
  2729. !SourceMgr.isInSystemHeader(DefineTok.getLocation())) &&
  2730. !MI->isIdenticalTo(*OtherMI, *this,
  2731. /*Syntactic=*/LangOpts.MicrosoftExt)) {
  2732. Diag(MI->getDefinitionLoc(), diag::warn_pp_objc_macro_redef_ignored);
  2733. }
  2734. assert(!OtherMI->isWarnIfUnused());
  2735. return;
  2736. }
  2737. // It is very common for system headers to have tons of macro redefinitions
  2738. // and for warnings to be disabled in system headers. If this is the case,
  2739. // then don't bother calling MacroInfo::isIdenticalTo.
  2740. if (!getDiagnostics().getSuppressSystemWarnings() ||
  2741. !SourceMgr.isInSystemHeader(DefineTok.getLocation())) {
  2742. if (!OtherMI->isUsed() && OtherMI->isWarnIfUnused())
  2743. Diag(OtherMI->getDefinitionLoc(), diag::pp_macro_not_used);
  2744. // Warn if defining "__LINE__" and other builtins, per C99 6.10.8/4 and
  2745. // C++ [cpp.predefined]p4, but allow it as an extension.
  2746. if (OtherMI->isBuiltinMacro())
  2747. Diag(MacroNameTok, diag::ext_pp_redef_builtin_macro);
  2748. // Macros must be identical. This means all tokens and whitespace
  2749. // separation must be the same. C99 6.10.3p2.
  2750. else if (!OtherMI->isAllowRedefinitionsWithoutWarning() &&
  2751. !MI->isIdenticalTo(*OtherMI, *this, /*Syntactic=*/LangOpts.MicrosoftExt)) {
  2752. Diag(MI->getDefinitionLoc(), diag::ext_pp_macro_redef)
  2753. << MacroNameTok.getIdentifierInfo();
  2754. Diag(OtherMI->getDefinitionLoc(), diag::note_previous_definition);
  2755. }
  2756. }
  2757. if (OtherMI->isWarnIfUnused())
  2758. WarnUnusedMacroLocs.erase(OtherMI->getDefinitionLoc());
  2759. }
  2760. DefMacroDirective *MD =
  2761. appendDefMacroDirective(MacroNameTok.getIdentifierInfo(), MI);
  2762. assert(!MI->isUsed());
  2763. // If we need warning for not using the macro, add its location in the
  2764. // warn-because-unused-macro set. If it gets used it will be removed from set.
  2765. if (getSourceManager().isInMainFile(MI->getDefinitionLoc()) &&
  2766. !Diags->isIgnored(diag::pp_macro_not_used, MI->getDefinitionLoc()) &&
  2767. !MacroExpansionInDirectivesOverride &&
  2768. getSourceManager().getFileID(MI->getDefinitionLoc()) !=
  2769. getPredefinesFileID()) {
  2770. MI->setIsWarnIfUnused(true);
  2771. WarnUnusedMacroLocs.insert(MI->getDefinitionLoc());
  2772. }
  2773. // If the callbacks want to know, tell them about the macro definition.
  2774. if (Callbacks)
  2775. Callbacks->MacroDefined(MacroNameTok, MD);
  2776. // If we're in MS compatibility mode and the macro being defined is the
  2777. // assert macro, implicitly add a macro definition for static_assert to work
  2778. // around their broken assert.h header file in C. Only do so if there isn't
  2779. // already a static_assert macro defined.
  2780. if (!getLangOpts().CPlusPlus && getLangOpts().MSVCCompat &&
  2781. MacroNameTok.getIdentifierInfo()->isStr("assert") &&
  2782. !isMacroDefined("static_assert")) {
  2783. MacroInfo *MI = AllocateMacroInfo(SourceLocation());
  2784. Token Tok;
  2785. Tok.startToken();
  2786. Tok.setKind(tok::kw__Static_assert);
  2787. Tok.setIdentifierInfo(getIdentifierInfo("_Static_assert"));
  2788. MI->setTokens({Tok}, BP);
  2789. (void)appendDefMacroDirective(getIdentifierInfo("static_assert"), MI);
  2790. }
  2791. }
  2792. /// HandleUndefDirective - Implements \#undef.
  2793. ///
  2794. void Preprocessor::HandleUndefDirective() {
  2795. ++NumUndefined;
  2796. Token MacroNameTok;
  2797. ReadMacroName(MacroNameTok, MU_Undef);
  2798. // Error reading macro name? If so, diagnostic already issued.
  2799. if (MacroNameTok.is(tok::eod))
  2800. return;
  2801. // Check to see if this is the last token on the #undef line.
  2802. CheckEndOfDirective("undef");
  2803. // Okay, we have a valid identifier to undef.
  2804. auto *II = MacroNameTok.getIdentifierInfo();
  2805. auto MD = getMacroDefinition(II);
  2806. UndefMacroDirective *Undef = nullptr;
  2807. if (II->isFinal())
  2808. emitFinalMacroWarning(MacroNameTok, /*IsUndef=*/true);
  2809. // If the macro is not defined, this is a noop undef.
  2810. if (const MacroInfo *MI = MD.getMacroInfo()) {
  2811. if (!MI->isUsed() && MI->isWarnIfUnused())
  2812. Diag(MI->getDefinitionLoc(), diag::pp_macro_not_used);
  2813. if (MI->isWarnIfUnused())
  2814. WarnUnusedMacroLocs.erase(MI->getDefinitionLoc());
  2815. Undef = AllocateUndefMacroDirective(MacroNameTok.getLocation());
  2816. }
  2817. // If the callbacks want to know, tell them about the macro #undef.
  2818. // Note: no matter if the macro was defined or not.
  2819. if (Callbacks)
  2820. Callbacks->MacroUndefined(MacroNameTok, MD, Undef);
  2821. if (Undef)
  2822. appendMacroDirective(II, Undef);
  2823. }
  2824. //===----------------------------------------------------------------------===//
  2825. // Preprocessor Conditional Directive Handling.
  2826. //===----------------------------------------------------------------------===//
  2827. /// HandleIfdefDirective - Implements the \#ifdef/\#ifndef directive. isIfndef
  2828. /// is true when this is a \#ifndef directive. ReadAnyTokensBeforeDirective is
  2829. /// true if any tokens have been returned or pp-directives activated before this
  2830. /// \#ifndef has been lexed.
  2831. ///
  2832. void Preprocessor::HandleIfdefDirective(Token &Result,
  2833. const Token &HashToken,
  2834. bool isIfndef,
  2835. bool ReadAnyTokensBeforeDirective) {
  2836. ++NumIf;
  2837. Token DirectiveTok = Result;
  2838. Token MacroNameTok;
  2839. ReadMacroName(MacroNameTok);
  2840. // Error reading macro name? If so, diagnostic already issued.
  2841. if (MacroNameTok.is(tok::eod)) {
  2842. // Skip code until we get to #endif. This helps with recovery by not
  2843. // emitting an error when the #endif is reached.
  2844. SkipExcludedConditionalBlock(HashToken.getLocation(),
  2845. DirectiveTok.getLocation(),
  2846. /*Foundnonskip*/ false, /*FoundElse*/ false);
  2847. return;
  2848. }
  2849. emitMacroExpansionWarnings(MacroNameTok);
  2850. // Check to see if this is the last token on the #if[n]def line.
  2851. CheckEndOfDirective(isIfndef ? "ifndef" : "ifdef");
  2852. IdentifierInfo *MII = MacroNameTok.getIdentifierInfo();
  2853. auto MD = getMacroDefinition(MII);
  2854. MacroInfo *MI = MD.getMacroInfo();
  2855. if (CurPPLexer->getConditionalStackDepth() == 0) {
  2856. // If the start of a top-level #ifdef and if the macro is not defined,
  2857. // inform MIOpt that this might be the start of a proper include guard.
  2858. // Otherwise it is some other form of unknown conditional which we can't
  2859. // handle.
  2860. if (!ReadAnyTokensBeforeDirective && !MI) {
  2861. assert(isIfndef && "#ifdef shouldn't reach here");
  2862. CurPPLexer->MIOpt.EnterTopLevelIfndef(MII, MacroNameTok.getLocation());
  2863. } else
  2864. CurPPLexer->MIOpt.EnterTopLevelConditional();
  2865. }
  2866. // If there is a macro, process it.
  2867. if (MI) // Mark it used.
  2868. markMacroAsUsed(MI);
  2869. if (Callbacks) {
  2870. if (isIfndef)
  2871. Callbacks->Ifndef(DirectiveTok.getLocation(), MacroNameTok, MD);
  2872. else
  2873. Callbacks->Ifdef(DirectiveTok.getLocation(), MacroNameTok, MD);
  2874. }
  2875. bool RetainExcludedCB = PPOpts->RetainExcludedConditionalBlocks &&
  2876. getSourceManager().isInMainFile(DirectiveTok.getLocation());
  2877. // Should we include the stuff contained by this directive?
  2878. if (PPOpts->SingleFileParseMode && !MI) {
  2879. // In 'single-file-parse mode' undefined identifiers trigger parsing of all
  2880. // the directive blocks.
  2881. CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
  2882. /*wasskip*/false, /*foundnonskip*/false,
  2883. /*foundelse*/false);
  2884. } else if (!MI == isIfndef || RetainExcludedCB) {
  2885. // Yes, remember that we are inside a conditional, then lex the next token.
  2886. CurPPLexer->pushConditionalLevel(DirectiveTok.getLocation(),
  2887. /*wasskip*/false, /*foundnonskip*/true,
  2888. /*foundelse*/false);
  2889. } else {
  2890. // No, skip the contents of this block.
  2891. SkipExcludedConditionalBlock(HashToken.getLocation(),
  2892. DirectiveTok.getLocation(),
  2893. /*Foundnonskip*/ false,
  2894. /*FoundElse*/ false);
  2895. }
  2896. }
  2897. /// HandleIfDirective - Implements the \#if directive.
  2898. ///
  2899. void Preprocessor::HandleIfDirective(Token &IfToken,
  2900. const Token &HashToken,
  2901. bool ReadAnyTokensBeforeDirective) {
  2902. ++NumIf;
  2903. // Parse and evaluate the conditional expression.
  2904. IdentifierInfo *IfNDefMacro = nullptr;
  2905. const DirectiveEvalResult DER = EvaluateDirectiveExpression(IfNDefMacro);
  2906. const bool ConditionalTrue = DER.Conditional;
  2907. // Lexer might become invalid if we hit code completion point while evaluating
  2908. // expression.
  2909. if (!CurPPLexer)
  2910. return;
  2911. // If this condition is equivalent to #ifndef X, and if this is the first
  2912. // directive seen, handle it for the multiple-include optimization.
  2913. if (CurPPLexer->getConditionalStackDepth() == 0) {
  2914. if (!ReadAnyTokensBeforeDirective && IfNDefMacro && ConditionalTrue)
  2915. // FIXME: Pass in the location of the macro name, not the 'if' token.
  2916. CurPPLexer->MIOpt.EnterTopLevelIfndef(IfNDefMacro, IfToken.getLocation());
  2917. else
  2918. CurPPLexer->MIOpt.EnterTopLevelConditional();
  2919. }
  2920. if (Callbacks)
  2921. Callbacks->If(
  2922. IfToken.getLocation(), DER.ExprRange,
  2923. (ConditionalTrue ? PPCallbacks::CVK_True : PPCallbacks::CVK_False));
  2924. bool RetainExcludedCB = PPOpts->RetainExcludedConditionalBlocks &&
  2925. getSourceManager().isInMainFile(IfToken.getLocation());
  2926. // Should we include the stuff contained by this directive?
  2927. if (PPOpts->SingleFileParseMode && DER.IncludedUndefinedIds) {
  2928. // In 'single-file-parse mode' undefined identifiers trigger parsing of all
  2929. // the directive blocks.
  2930. CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
  2931. /*foundnonskip*/false, /*foundelse*/false);
  2932. } else if (ConditionalTrue || RetainExcludedCB) {
  2933. // Yes, remember that we are inside a conditional, then lex the next token.
  2934. CurPPLexer->pushConditionalLevel(IfToken.getLocation(), /*wasskip*/false,
  2935. /*foundnonskip*/true, /*foundelse*/false);
  2936. } else {
  2937. // No, skip the contents of this block.
  2938. SkipExcludedConditionalBlock(HashToken.getLocation(), IfToken.getLocation(),
  2939. /*Foundnonskip*/ false,
  2940. /*FoundElse*/ false);
  2941. }
  2942. }
  2943. /// HandleEndifDirective - Implements the \#endif directive.
  2944. ///
  2945. void Preprocessor::HandleEndifDirective(Token &EndifToken) {
  2946. ++NumEndif;
  2947. // Check that this is the whole directive.
  2948. CheckEndOfDirective("endif");
  2949. PPConditionalInfo CondInfo;
  2950. if (CurPPLexer->popConditionalLevel(CondInfo)) {
  2951. // No conditionals on the stack: this is an #endif without an #if.
  2952. Diag(EndifToken, diag::err_pp_endif_without_if);
  2953. return;
  2954. }
  2955. // If this the end of a top-level #endif, inform MIOpt.
  2956. if (CurPPLexer->getConditionalStackDepth() == 0)
  2957. CurPPLexer->MIOpt.ExitTopLevelConditional();
  2958. assert(!CondInfo.WasSkipping && !CurPPLexer->LexingRawMode &&
  2959. "This code should only be reachable in the non-skipping case!");
  2960. if (Callbacks)
  2961. Callbacks->Endif(EndifToken.getLocation(), CondInfo.IfLoc);
  2962. }
  2963. /// HandleElseDirective - Implements the \#else directive.
  2964. ///
  2965. void Preprocessor::HandleElseDirective(Token &Result, const Token &HashToken) {
  2966. ++NumElse;
  2967. // #else directive in a non-skipping conditional... start skipping.
  2968. CheckEndOfDirective("else");
  2969. PPConditionalInfo CI;
  2970. if (CurPPLexer->popConditionalLevel(CI)) {
  2971. Diag(Result, diag::pp_err_else_without_if);
  2972. return;
  2973. }
  2974. // If this is a top-level #else, inform the MIOpt.
  2975. if (CurPPLexer->getConditionalStackDepth() == 0)
  2976. CurPPLexer->MIOpt.EnterTopLevelConditional();
  2977. // If this is a #else with a #else before it, report the error.
  2978. if (CI.FoundElse) Diag(Result, diag::pp_err_else_after_else);
  2979. if (Callbacks)
  2980. Callbacks->Else(Result.getLocation(), CI.IfLoc);
  2981. bool RetainExcludedCB = PPOpts->RetainExcludedConditionalBlocks &&
  2982. getSourceManager().isInMainFile(Result.getLocation());
  2983. if ((PPOpts->SingleFileParseMode && !CI.FoundNonSkip) || RetainExcludedCB) {
  2984. // In 'single-file-parse mode' undefined identifiers trigger parsing of all
  2985. // the directive blocks.
  2986. CurPPLexer->pushConditionalLevel(CI.IfLoc, /*wasskip*/false,
  2987. /*foundnonskip*/false, /*foundelse*/true);
  2988. return;
  2989. }
  2990. // Finally, skip the rest of the contents of this block.
  2991. SkipExcludedConditionalBlock(HashToken.getLocation(), CI.IfLoc,
  2992. /*Foundnonskip*/ true,
  2993. /*FoundElse*/ true, Result.getLocation());
  2994. }
  2995. /// Implements the \#elif, \#elifdef, and \#elifndef directives.
  2996. void Preprocessor::HandleElifFamilyDirective(Token &ElifToken,
  2997. const Token &HashToken,
  2998. tok::PPKeywordKind Kind) {
  2999. PPElifDiag DirKind = Kind == tok::pp_elif ? PED_Elif
  3000. : Kind == tok::pp_elifdef ? PED_Elifdef
  3001. : PED_Elifndef;
  3002. ++NumElse;
  3003. // Warn if using `#elifdef` & `#elifndef` in not C2x & C++2b mode.
  3004. switch (DirKind) {
  3005. case PED_Elifdef:
  3006. case PED_Elifndef:
  3007. unsigned DiagID;
  3008. if (LangOpts.CPlusPlus)
  3009. DiagID = LangOpts.CPlusPlus2b ? diag::warn_cxx2b_compat_pp_directive
  3010. : diag::ext_cxx2b_pp_directive;
  3011. else
  3012. DiagID = LangOpts.C2x ? diag::warn_c2x_compat_pp_directive
  3013. : diag::ext_c2x_pp_directive;
  3014. Diag(ElifToken, DiagID) << DirKind;
  3015. break;
  3016. default:
  3017. break;
  3018. }
  3019. // #elif directive in a non-skipping conditional... start skipping.
  3020. // We don't care what the condition is, because we will always skip it (since
  3021. // the block immediately before it was included).
  3022. SourceRange ConditionRange = DiscardUntilEndOfDirective();
  3023. PPConditionalInfo CI;
  3024. if (CurPPLexer->popConditionalLevel(CI)) {
  3025. Diag(ElifToken, diag::pp_err_elif_without_if) << DirKind;
  3026. return;
  3027. }
  3028. // If this is a top-level #elif, inform the MIOpt.
  3029. if (CurPPLexer->getConditionalStackDepth() == 0)
  3030. CurPPLexer->MIOpt.EnterTopLevelConditional();
  3031. // If this is a #elif with a #else before it, report the error.
  3032. if (CI.FoundElse)
  3033. Diag(ElifToken, diag::pp_err_elif_after_else) << DirKind;
  3034. if (Callbacks) {
  3035. switch (Kind) {
  3036. case tok::pp_elif:
  3037. Callbacks->Elif(ElifToken.getLocation(), ConditionRange,
  3038. PPCallbacks::CVK_NotEvaluated, CI.IfLoc);
  3039. break;
  3040. case tok::pp_elifdef:
  3041. Callbacks->Elifdef(ElifToken.getLocation(), ConditionRange, CI.IfLoc);
  3042. break;
  3043. case tok::pp_elifndef:
  3044. Callbacks->Elifndef(ElifToken.getLocation(), ConditionRange, CI.IfLoc);
  3045. break;
  3046. default:
  3047. assert(false && "unexpected directive kind");
  3048. break;
  3049. }
  3050. }
  3051. bool RetainExcludedCB = PPOpts->RetainExcludedConditionalBlocks &&
  3052. getSourceManager().isInMainFile(ElifToken.getLocation());
  3053. if ((PPOpts->SingleFileParseMode && !CI.FoundNonSkip) || RetainExcludedCB) {
  3054. // In 'single-file-parse mode' undefined identifiers trigger parsing of all
  3055. // the directive blocks.
  3056. CurPPLexer->pushConditionalLevel(ElifToken.getLocation(), /*wasskip*/false,
  3057. /*foundnonskip*/false, /*foundelse*/false);
  3058. return;
  3059. }
  3060. // Finally, skip the rest of the contents of this block.
  3061. SkipExcludedConditionalBlock(
  3062. HashToken.getLocation(), CI.IfLoc, /*Foundnonskip*/ true,
  3063. /*FoundElse*/ CI.FoundElse, ElifToken.getLocation());
  3064. }