2
0

cppPreprocessor.cxx 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873
  1. /**
  2. * PANDA 3D SOFTWARE
  3. * Copyright (c) Carnegie Mellon University. All rights reserved.
  4. *
  5. * All use of this software is subject to the terms of the revised BSD
  6. * license. You should have received a copy of this license along
  7. * with this source code in a file named "LICENSE."
  8. *
  9. * @file cppPreprocessor.cxx
  10. * @author drose
  11. * @date 1999-10-22
  12. */
  13. #include "cppPreprocessor.h"
  14. #include "cppExpressionParser.h"
  15. #include "cppExpression.h"
  16. #include "cppScope.h"
  17. #include "cppIdentifier.h"
  18. #include "cppTemplateScope.h"
  19. #include "cppTemplateParameterList.h"
  20. #include "cppConstType.h"
  21. #include "cppFunctionGroup.h"
  22. #include "cppFunctionType.h"
  23. #include "cppPointerType.h"
  24. #include "cppParameterList.h"
  25. #include "cppSimpleType.h"
  26. #include "cppGlobals.h"
  27. #include "cppCommentBlock.h"
  28. #include "cppBison.h"
  29. #include "indent.h"
  30. #include "pstrtod.h"
  31. #include "string_utils.h"
  32. #include <assert.h>
  33. #include <ctype.h>
  34. // We manage our own visibility counter, in addition to that managed by
  35. // cppBison.y. We do this just so we can define manifests with the correct
  36. // visibility when they are declared. (Asking the parser for the current
  37. // visibility is prone to error, since the parser might be several tokens
  38. // behind the preprocessor.)
  39. static CPPVisibility preprocessor_vis = V_public;
  40. static int
  41. hex_val(int c) {
  42. switch (c) {
  43. case '0':
  44. case '1':
  45. case '2':
  46. case '3':
  47. case '4':
  48. case '5':
  49. case '6':
  50. case '7':
  51. case '8':
  52. case '9':
  53. return (c - '0');
  54. default:
  55. return (tolower(c) - 'a' + 10);
  56. }
  57. }
  58. static string
  59. trim_blanks(const string &str) {
  60. size_t first, last;
  61. if(str.empty())
  62. return str;
  63. first = 0;
  64. while (first < str.length() && isspace(str[first])) {
  65. first++;
  66. }
  67. last = str.length() - 1;
  68. while (last > first && isspace(str[last])) {
  69. last--;
  70. }
  71. return str.substr(first, last - first + 1);
  72. }
  73. /**
  74. *
  75. */
  76. CPPPreprocessor::InputFile::
  77. InputFile() {
  78. _in = NULL;
  79. _ignore_manifest = NULL;
  80. _line_number = 0;
  81. _col_number = 0;
  82. _next_line_number = 1;
  83. _next_col_number = 1;
  84. _lock_position = false;
  85. }
  86. /**
  87. *
  88. */
  89. CPPPreprocessor::InputFile::
  90. ~InputFile() {
  91. if (_in != NULL) {
  92. // For some reason--compiler bug in gcc 3.2?--explicitly deleting the
  93. // stream pointer does not call the appropriate global delete function;
  94. // instead apparently calling the system delete function. So we call the
  95. // delete function by hand instead.
  96. #if !defined(USE_MEMORY_NOWRAPPERS) && defined(REDEFINE_GLOBAL_OPERATOR_NEW)
  97. _in->~istream();
  98. (*global_operator_delete)(_in);
  99. #else
  100. delete _in;
  101. #endif
  102. }
  103. }
  104. /**
  105. *
  106. */
  107. bool CPPPreprocessor::InputFile::
  108. open(const CPPFile &file) {
  109. assert(_in == NULL);
  110. _file = file;
  111. pifstream *in = new pifstream;
  112. _in = in;
  113. return _file._filename.open_read(*in);
  114. }
  115. /**
  116. *
  117. */
  118. bool CPPPreprocessor::InputFile::
  119. connect_input(const string &input) {
  120. assert(_in == NULL);
  121. _input = input;
  122. _in = new istringstream(_input);
  123. return !_in->fail();
  124. }
  125. /**
  126. * Fetches a single character from the source file.
  127. */
  128. int CPPPreprocessor::InputFile::
  129. get() {
  130. assert(_in != NULL);
  131. if (!_lock_position) {
  132. _line_number = _next_line_number;
  133. _col_number = _next_col_number;
  134. }
  135. int c = _in->get();
  136. // Quietly skip over embedded carriage-return characters. We shouldn't see
  137. // any of these unless there was some DOS-to-Unix file conversion problem.
  138. while (c == '\r') {
  139. c = _in->get();
  140. }
  141. switch (c) {
  142. case EOF:
  143. break;
  144. case '\n':
  145. if (!_lock_position) {
  146. ++_next_line_number;
  147. _next_col_number = 1;
  148. }
  149. break;
  150. default:
  151. if (!_lock_position) {
  152. ++_next_col_number;
  153. }
  154. }
  155. return c;
  156. }
  157. /**
  158. * Like get(), but does not advance the file pointer.
  159. */
  160. int CPPPreprocessor::InputFile::
  161. peek() {
  162. assert(_in != NULL);
  163. int c = _in->peek();
  164. // Quietly skip over embedded carriage-return characters. We shouldn't see
  165. // any of these unless there was some DOS-to-Unix file conversion problem.
  166. while (c == '\r') {
  167. _in->get();
  168. c = _in->peek();
  169. }
  170. return c;
  171. }
  172. /**
  173. *
  174. */
  175. CPPPreprocessor::
  176. CPPPreprocessor() {
  177. _noangles = false;
  178. _state = S_eof;
  179. _paren_nesting = 0;
  180. _parsing_template_params = false;
  181. _unget = '\0';
  182. _last_c = '\0';
  183. _start_of_line = true;
  184. _last_cpp_comment = false;
  185. _save_comments = true;
  186. _resolve_identifiers = true;
  187. _warning_count = 0;
  188. _error_count = 0;
  189. _error_abort = false;
  190. #ifdef CPP_VERBOSE_LEX
  191. _token_index = 0;
  192. #endif
  193. _verbose = 1;
  194. }
  195. /**
  196. * Sets the verbosity level of the parser. At 0, no warnings will be
  197. * reported; at 1 or higher, expect to get spammed.
  198. */
  199. void CPPPreprocessor::
  200. set_verbose(int verbose) {
  201. _verbose = verbose;
  202. }
  203. /**
  204. * Returns the verbosity level of the parser.
  205. */
  206. int CPPPreprocessor::
  207. get_verbose() const {
  208. return _verbose;
  209. }
  210. /**
  211. *
  212. */
  213. void CPPPreprocessor::
  214. copy_filepos(const CPPPreprocessor &other) {
  215. assert(!_files.empty());
  216. _files.back()._file = other.get_file();
  217. _files.back()._line_number = other.get_line_number();
  218. _files.back()._col_number = other.get_col_number();
  219. }
  220. /**
  221. *
  222. */
  223. CPPFile CPPPreprocessor::
  224. get_file() const {
  225. if (_files.empty()) {
  226. return CPPFile("");
  227. }
  228. return _files.back()._file;
  229. }
  230. /**
  231. * Returns the line number of the last character returned by get().
  232. */
  233. int CPPPreprocessor::
  234. get_line_number() const {
  235. if (_files.empty()) {
  236. return 0;
  237. }
  238. return _files.back()._line_number;
  239. }
  240. /**
  241. * Returns the column number of the last character returned by get().
  242. */
  243. int CPPPreprocessor::
  244. get_col_number() const {
  245. if (_files.empty()) {
  246. return 0;
  247. }
  248. return _files.back()._col_number;
  249. }
  250. /**
  251. *
  252. */
  253. CPPToken CPPPreprocessor::
  254. get_next_token() {
  255. #ifdef CPP_VERBOSE_LEX
  256. CPPToken tok = get_next_token0();
  257. indent(cerr, _files.size() * 2)
  258. << _token_index++ << ". " << tok << "\n";
  259. return tok;
  260. }
  261. CPPToken CPPPreprocessor::
  262. get_next_token0() {
  263. #endif
  264. // We make a nested call to internal_get_next_token(), so we can combine
  265. // sequences of identifiers and scoping symbols into a single identifier,
  266. // for yacc's convenience.
  267. CPPToken token(0);
  268. if (!_saved_tokens.empty()) {
  269. token = _saved_tokens.back();
  270. _saved_tokens.pop_back();
  271. } else {
  272. token = internal_get_next_token();
  273. }
  274. YYLTYPE loc = token._lloc;
  275. if (_resolve_identifiers &&
  276. (token._token == SIMPLE_IDENTIFIER || token._token == SCOPE)) {
  277. // We will be returning a scoped identifier, or a scoping. Keep pulling
  278. // off tokens until we reach the end of the scopeidentifier sequence.
  279. string name;
  280. // If we started the ball with an identifier, use it and get the next
  281. // token. Otherwise, we started with :: (global scope), and we indicate
  282. // this with an empty string at the beginning of the scoping sequence.
  283. if (token._token == SIMPLE_IDENTIFIER) {
  284. name = token._lval.str;
  285. token = internal_get_next_token();
  286. }
  287. CPPIdentifier *ident = new CPPIdentifier(name, loc);
  288. YYSTYPE result;
  289. result.u.identifier = ident;
  290. if (token._token == '<') {
  291. // If the next token is an angle bracket and the current identifier
  292. // wants template instantiation, assume the angle bracket begins the
  293. // instantiation and call yacc recursively to parse the template
  294. // parameters.
  295. CPPDeclaration *decl = ident->find_template(current_scope, global_scope);
  296. if (decl != NULL) {
  297. ident->_names.back().set_templ
  298. (nested_parse_template_instantiation(decl->get_template_scope()));
  299. token = internal_get_next_token();
  300. } else {
  301. error(string("unknown template '") + ident->get_fully_scoped_name() + "'", loc);
  302. }
  303. }
  304. while (token._token == SCOPE) {
  305. loc.last_line = token._lloc.last_line;
  306. loc.last_column = token._lloc.last_column;
  307. name += "::";
  308. token = internal_get_next_token();
  309. string token_prefix;
  310. if (token._token == '~') {
  311. // A scoping operator followed by a tilde can only be the start of a
  312. // scoped destructor name. Make the tilde be part of the name.
  313. name += "~";
  314. token_prefix = "~";
  315. token = internal_get_next_token();
  316. }
  317. if (token._token != SIMPLE_IDENTIFIER) {
  318. // The last useful token was a SCOPE, thus this is a scoping token.
  319. if (token._token == KW_OPERATOR) {
  320. // Unless the last token we came across was the "operator" keyword.
  321. // We make a special case for this, because it's occasionally scoped
  322. // in normal use.
  323. token._lval = result;
  324. _last_token_loc = token._lloc;
  325. return token;
  326. }
  327. _saved_tokens.push_back(token);
  328. _last_token_loc = loc;
  329. return CPPToken(SCOPING, loc, name, result);
  330. }
  331. name += token._lval.str;
  332. ident->_names.push_back(token_prefix + token._lval.str);
  333. loc.last_line = token._lloc.last_line;
  334. loc.last_column = token._lloc.last_column;
  335. ident->_loc.last_line = loc.last_line;
  336. ident->_loc.last_column = loc.last_column;
  337. token = internal_get_next_token();
  338. if (token._token == '<') {
  339. // If the next token is an angle bracket and the current indentifier
  340. // wants template instantiation, assume the angle bracket begins the
  341. // instantiation and call yacc recursively to parse the template
  342. // parameters.
  343. CPPDeclaration *decl =
  344. ident->find_template(current_scope, global_scope);
  345. if (decl != NULL) {
  346. ident->_names.back().set_templ
  347. (nested_parse_template_instantiation(decl->get_template_scope()));
  348. token = internal_get_next_token();
  349. } else {
  350. error(string("unknown template '") + ident->get_fully_scoped_name() + "'", loc);
  351. }
  352. }
  353. }
  354. // The last useful token was a SIMPLE_IDENTIFIER, thus this is a normal
  355. // scoped identifier.
  356. _saved_tokens.push_back(token);
  357. int token_type = IDENTIFIER;
  358. CPPDeclaration *decl = ident->find_symbol(current_scope, global_scope);
  359. if (decl != NULL && decl->as_type() != NULL) {
  360. token_type = TYPENAME_IDENTIFIER;
  361. }
  362. _last_token_loc = loc;
  363. return CPPToken(token_type, loc, name, result);
  364. }
  365. // This is the normal case: just pass through whatever token we got.
  366. _last_token_loc = loc;
  367. return token;
  368. }
  369. /**
  370. *
  371. */
  372. CPPToken CPPPreprocessor::
  373. peek_next_token() {
  374. CPPToken token(0);
  375. if (!_saved_tokens.empty()) {
  376. token = _saved_tokens.back();
  377. } else {
  378. token = internal_get_next_token();
  379. _saved_tokens.push_back(token);
  380. }
  381. return token;
  382. }
  383. /**
  384. *
  385. */
  386. void CPPPreprocessor::
  387. warning(const string &message) {
  388. if (_verbose < 2) {
  389. return;
  390. }
  391. int line = get_line_number();
  392. int col = get_col_number();
  393. YYLTYPE loc;
  394. loc.first_line = line;
  395. loc.first_column = col;
  396. loc.last_line = line;
  397. loc.last_column = col;
  398. loc.file = get_file();
  399. warning(message, loc);
  400. }
  401. /**
  402. *
  403. */
  404. void CPPPreprocessor::
  405. warning(const string &message, const YYLTYPE &loc) {
  406. if (_verbose >= 2) {
  407. if (_verbose >= 3) {
  408. indent(cerr, _files.size() * 2);
  409. }
  410. if (!loc.file.empty()) {
  411. cerr << loc.file << ':';
  412. }
  413. if (loc.first_line) {
  414. cerr << loc.first_line << ':';
  415. if (loc.first_column) {
  416. cerr << loc.first_column << ':';
  417. }
  418. }
  419. cerr << " warning: " << message << "\n";
  420. show_line(loc);
  421. }
  422. _warning_count++;
  423. }
  424. /**
  425. *
  426. */
  427. void CPPPreprocessor::
  428. error(const string &message) {
  429. int line = get_line_number();
  430. int col = get_col_number();
  431. YYLTYPE loc;
  432. loc.first_line = line;
  433. loc.first_column = col;
  434. loc.last_line = line;
  435. loc.last_column = col;
  436. loc.file = get_file();
  437. error(message, loc);
  438. }
  439. /**
  440. *
  441. */
  442. void CPPPreprocessor::
  443. error(const string &message, const YYLTYPE &loc) {
  444. if (_state == S_nested || _state == S_end_nested) {
  445. // Don't report or log errors in the nested state. These will be reported
  446. // when the nesting level collapses.
  447. return;
  448. }
  449. if (_verbose >= 1) {
  450. if (_verbose >= 3) {
  451. indent(cerr, _files.size() * 2);
  452. }
  453. if (!loc.file.empty()) {
  454. cerr << loc.file << ':';
  455. }
  456. if (loc.first_line) {
  457. cerr << loc.first_line << ':';
  458. if (loc.first_column) {
  459. cerr << loc.first_column << ':';
  460. }
  461. }
  462. cerr << " error: " << message << "\n";
  463. show_line(loc);
  464. if (_error_abort) {
  465. cerr << "Aborting.\n";
  466. abort();
  467. }
  468. }
  469. _error_count++;
  470. }
  471. /**
  472. * Shows the indicated line, useful for error messages.
  473. */
  474. void CPPPreprocessor::
  475. show_line(const YYLTYPE &loc) {
  476. if (loc.file._filename.empty()) {
  477. return;
  478. }
  479. int indent_level = 0;
  480. if (_verbose >= 3) {
  481. indent_level = _files.size() * 2;
  482. }
  483. // Seek to the offending line in the file.
  484. ifstream stream;
  485. if (loc.file._filename.open_read(stream)) {
  486. int l = 0;
  487. string linestr;
  488. while (l < loc.first_line) {
  489. getline(stream, linestr);
  490. ++l;
  491. }
  492. // Strip off trailing whitespace.
  493. size_t last = linestr.length();
  494. while (isspace(linestr[--last])) {
  495. linestr = linestr.substr(0, last);
  496. }
  497. indent(cerr, indent_level) << linestr << "\n";
  498. // Point the user at the offending column.
  499. if (loc.first_column) {
  500. int last_column;
  501. if (loc.first_line == loc.last_line && loc.last_column) {
  502. last_column = loc.last_column;
  503. } else {
  504. last_column = linestr.length();
  505. }
  506. indent(cerr, indent_level);
  507. int i = 0;
  508. for (; i < loc.first_column - 1; ++i) {
  509. cerr.put(' ');
  510. }
  511. cerr.put('^');
  512. while (++i < last_column) {
  513. cerr.put('~');
  514. }
  515. cerr << "\n";
  516. }
  517. }
  518. }
  519. /**
  520. *
  521. */
  522. int CPPPreprocessor::
  523. get_warning_count() const {
  524. return _warning_count;
  525. }
  526. /**
  527. *
  528. */
  529. int CPPPreprocessor::
  530. get_error_count() const {
  531. return _error_count;
  532. }
  533. /**
  534. * Returns the CPPCommentBlock immediately preceding the indicated line, if
  535. * any. If there is no such comment, returns NULL.
  536. */
  537. CPPCommentBlock *CPPPreprocessor::
  538. get_comment_before(int line, CPPFile file) {
  539. CPPComments::reverse_iterator ci;
  540. ci = _comments.rbegin();
  541. int wrong_file_count = 0;
  542. while (ci != _comments.rend()) {
  543. CPPCommentBlock *comment = (*ci);
  544. if (comment->_file == file) {
  545. wrong_file_count = 0;
  546. if (comment->_last_line == line || comment->_last_line == line - 1) {
  547. return comment;
  548. }
  549. if (comment->_last_line < line) {
  550. return (CPPCommentBlock *)NULL;
  551. }
  552. } else {
  553. wrong_file_count++;
  554. if (wrong_file_count > 10) {
  555. return (CPPCommentBlock *)NULL;
  556. }
  557. }
  558. ++ci;
  559. }
  560. return (CPPCommentBlock *)NULL;
  561. }
  562. /**
  563. * Returns the CPPCommentBlock that starts on the indicated line, if any. If
  564. * there is no such comment, returns NULL.
  565. */
  566. CPPCommentBlock *CPPPreprocessor::
  567. get_comment_on(int line, CPPFile file) {
  568. CPPComments::reverse_iterator ci;
  569. ci = _comments.rbegin();
  570. while (ci != _comments.rend()) {
  571. CPPCommentBlock *comment = (*ci);
  572. if (comment->_file == file) {
  573. if (comment->_line_number == line) {
  574. return comment;
  575. } else if (comment->_line_number < line) {
  576. return (CPPCommentBlock *)NULL;
  577. }
  578. }
  579. ++ci;
  580. }
  581. return (CPPCommentBlock *)NULL;
  582. }
  583. /**
  584. *
  585. */
  586. bool CPPPreprocessor::
  587. init_cpp(const CPPFile &file) {
  588. _state = S_normal;
  589. _saved_tokens.push_back(CPPToken(START_CPP));
  590. _last_c = '\0';
  591. return push_file(file);
  592. }
  593. /**
  594. *
  595. */
  596. bool CPPPreprocessor::
  597. init_const_expr(const string &expr) {
  598. _state = S_normal;
  599. _saved_tokens.push_back(CPPToken(START_CONST_EXPR));
  600. return push_string(expr, false);
  601. }
  602. /**
  603. *
  604. */
  605. bool CPPPreprocessor::
  606. init_type(const string &type) {
  607. _state = S_normal;
  608. _saved_tokens.push_back(CPPToken(START_TYPE));
  609. return push_string(type, false);
  610. }
  611. /**
  612. *
  613. */
  614. bool CPPPreprocessor::
  615. push_file(const CPPFile &file) {
  616. if (_verbose >= 3) {
  617. indent(cerr, _files.size() * 2)
  618. << "Reading " << file << "\n";
  619. }
  620. assert(_last_c == 0);
  621. _files.push_back(InputFile());
  622. InputFile &infile = _files.back();
  623. if (infile.open(file)) {
  624. // Record the fact that we opened the file for the benefit of user code.
  625. _parsed_files.insert(file);
  626. infile._prev_last_c = _last_c;
  627. _last_c = '\0';
  628. _start_of_line = true;
  629. return true;
  630. }
  631. _files.pop_back();
  632. return false;
  633. }
  634. /**
  635. *
  636. */
  637. bool CPPPreprocessor::
  638. push_string(const string &input, bool lock_position) {
  639. #ifdef CPP_VERBOSE_LEX
  640. indent(cerr, _files.size() * 2)
  641. << "Pushing to string \"" << input
  642. << "\"\nlock_position = " << lock_position << "\n";
  643. #endif
  644. CPPFile first_file = get_file();
  645. int first_line = get_line_number();
  646. int first_col = get_col_number();
  647. _files.push_back(InputFile());
  648. InputFile &infile = _files.back();
  649. if (infile.connect_input(input)) {
  650. if (lock_position) {
  651. infile._file = first_file;
  652. infile._line_number = first_line;
  653. infile._col_number = first_col;
  654. infile._lock_position = true;
  655. }
  656. infile._prev_last_c = _last_c;
  657. _last_c = '\0';
  658. return true;
  659. }
  660. #ifdef CPP_VERBOSE_LEX
  661. indent(cerr, _files.size() * 2)
  662. << "Unable to read string\n";
  663. #endif
  664. _files.pop_back();
  665. return false;
  666. }
  667. /**
  668. * Given a string, expand all manifests within the string and return the new
  669. * string.
  670. */
  671. string CPPPreprocessor::
  672. expand_manifests(const string &input_expr, bool expand_undefined,
  673. const YYLTYPE &loc) {
  674. // Get a copy of the expression string we can modify.
  675. string expr = input_expr;
  676. // Repeatedly scan the expr for any manifest names or defined() function.
  677. bool manifest_found;
  678. do {
  679. manifest_found = false;
  680. size_t p = 0;
  681. while (p < expr.size()) {
  682. if (isalpha(expr[p]) || expr[p] == '_') {
  683. size_t q = p;
  684. while (p < expr.size() && (isalnum(expr[p]) || expr[p] == '_')) {
  685. p++;
  686. }
  687. string ident = expr.substr(q, p - q);
  688. // Here's an identifier. Is it "defined"?
  689. if (ident == "defined") {
  690. expand_defined_function(expr, q, p);
  691. } else {
  692. // Is it a manifest?
  693. Manifests::const_iterator mi = _manifests.find(ident);
  694. if (mi != _manifests.end()) {
  695. const CPPManifest *manifest = (*mi).second;
  696. expand_manifest_inline(expr, q, p, (*mi).second);
  697. manifest_found = true;
  698. } else if (expand_undefined && ident != "true" && ident != "false") {
  699. // It is not found. Expand it to 0, but only if we are currently
  700. // parsing an #if expression.
  701. expr = expr.substr(0, q) + "0" + expr.substr(p);
  702. p = q + 1;
  703. }
  704. }
  705. } else if (expr[p] == '\'' || expr[p] == '"') {
  706. // Skip the next part until we find a closing quotation mark.
  707. char quote = expr[p];
  708. p++;
  709. while (p < expr.size() && expr[p] != quote) {
  710. if (expr[p] == '\\') {
  711. // This might be an escaped quote. Skip an extra char.
  712. p++;
  713. }
  714. p++;
  715. }
  716. if (p >= expr.size()) {
  717. // Unclosed string.
  718. warning("missing terminating " + string(1, quote) + " character", loc);
  719. }
  720. p++;
  721. } else {
  722. p++;
  723. }
  724. }
  725. // If we expanded any manifests at all that time, then go back through the
  726. // string and look again--we might have a manifest that expands to another
  727. // manifest.
  728. } while (manifest_found);
  729. return expr;
  730. }
  731. /**
  732. * Given a string, expand all manifests within the string and evaluate it as
  733. * an expression. Returns NULL if the string is not a valid expression.
  734. *
  735. * This is an internal support function for CPPPreprocessor; however, there is
  736. * a public variant of this function defined for CPPParser.
  737. */
  738. CPPExpression *CPPPreprocessor::
  739. parse_expr(const string &input_expr, CPPScope *current_scope,
  740. CPPScope *global_scope, const YYLTYPE &loc) {
  741. string expr = expand_manifests(input_expr, false, loc);
  742. CPPExpressionParser ep(current_scope, global_scope);
  743. ep._verbose = 0;
  744. if (ep.parse_expr(expr, *this)) {
  745. return ep._expr;
  746. } else {
  747. return (CPPExpression *)NULL;
  748. }
  749. }
  750. /**
  751. *
  752. */
  753. CPPToken CPPPreprocessor::
  754. internal_get_next_token() {
  755. if (_state == S_eof || _state == S_end_nested) {
  756. return CPPToken::eof();
  757. }
  758. int c = _last_c;
  759. _last_c = '\0';
  760. if (c == '\0' || c == EOF) {
  761. c = get();
  762. }
  763. // Skip any whitespace, comments, and preprocessor directives before the
  764. // token.
  765. c = skip_whitespace(c);
  766. while (c == '#' && _start_of_line && !should_ignore_preprocessor()) {
  767. c = skip_whitespace(process_directive(c));
  768. }
  769. if (c == '\'') {
  770. return get_quoted_char(c);
  771. } else if (c == '"') {
  772. return get_quoted_string(c);
  773. } else if (isalpha(c) || c == '_') {
  774. return get_identifier(c);
  775. } else if (isdigit(c)) {
  776. return get_number(c);
  777. }
  778. if (c == EOF) {
  779. _state = S_eof;
  780. return CPPToken::eof();
  781. }
  782. // Check for a number beginning with a decimal point.
  783. int next_c = peek();
  784. if (c == '.' && isdigit(next_c)) {
  785. return get_number(c);
  786. }
  787. YYLTYPE loc;
  788. loc.file = get_file();
  789. loc.first_line = get_line_number();
  790. loc.first_column = get_col_number();
  791. loc.last_line = loc.first_line;
  792. loc.last_column = loc.first_column;
  793. // Check for two- or three-character tokens.
  794. int di = check_digraph(c);
  795. if (di != 0) {
  796. c = di;
  797. ++loc.last_column;
  798. get();
  799. int tri = check_trigraph(di);
  800. if (tri != 0) {
  801. ++loc.last_column;
  802. get();
  803. return CPPToken(tri, loc);
  804. }
  805. return CPPToken(di, loc);
  806. }
  807. if (_state == S_nested) {
  808. // If we're running a nested lexer, keep track of the paren levels. When
  809. // we encounter a comma or closing angle bracket at the bottom level, we
  810. // stop.
  811. switch (c) {
  812. case '(':
  813. case '[':
  814. _paren_nesting++;
  815. break;
  816. case ')':
  817. case ']':
  818. _paren_nesting--;
  819. break;
  820. case ',':
  821. if (_paren_nesting <= 0) {
  822. _state = S_end_nested;
  823. return CPPToken::eof();
  824. }
  825. break;
  826. case '>':
  827. if (_paren_nesting <= 0) {
  828. _parsing_template_params = false;
  829. _state = S_end_nested;
  830. return CPPToken::eof();
  831. }
  832. }
  833. }
  834. // Look for an end-of-line comment, and parse it before we finish this
  835. // token. This is not strictly necessary, but it allows us to pick up
  836. // docstrings from comments after enum values.
  837. while (next_c != EOF && isspace(next_c)) {
  838. get();
  839. next_c = peek();
  840. }
  841. if (next_c == '/') {
  842. _last_c = skip_whitespace(get());
  843. }
  844. return CPPToken(c, loc);
  845. }
  846. /**
  847. * Checks the next character in the stream to see if this might be a two-
  848. * character token. Returns 0 if it is only a single-character token.
  849. */
  850. int CPPPreprocessor::
  851. check_digraph(int c) {
  852. int next_c = peek();
  853. switch (c) {
  854. case '+':
  855. if (next_c == '+') return PLUSPLUS;
  856. if (next_c == '=') return PLUSEQUAL;
  857. break;
  858. case '-':
  859. if (next_c == '-') return MINUSMINUS;
  860. if (next_c == '=') return MINUSEQUAL;
  861. if (next_c == '>') return POINTSAT;
  862. break;
  863. case '<':
  864. if (next_c == '<') return LSHIFT;
  865. if (next_c == '=') return LECOMPARE;
  866. if (next_c == ':') return '[';
  867. if (next_c == '%') return '{';
  868. break;
  869. case '>':
  870. if (_parsing_template_params && _paren_nesting <= 0) {
  871. // Don't parse >> as right-shift when parsing a template list, as per
  872. // C++11, to allow a syntax like A<B>>. However, nested >> must be
  873. // preserved, such as in A<(2>>1)>
  874. break;
  875. }
  876. if (next_c == '>') return RSHIFT;
  877. if (next_c == '=') return GECOMPARE;
  878. break;
  879. case '|':
  880. if (next_c == '|') return OROR;
  881. if (next_c == '=') return OREQUAL;
  882. break;
  883. case '&':
  884. if (next_c == '&') return ANDAND;
  885. if (next_c == '=') return ANDEQUAL;
  886. break;
  887. case '^':
  888. if (next_c == '=') return XOREQUAL;
  889. break;
  890. case '=':
  891. if (next_c == '=') return EQCOMPARE;
  892. break;
  893. case '!':
  894. if (next_c == '=') return NECOMPARE;
  895. break;
  896. case '.':
  897. if (next_c == '*') return DOT_STAR;
  898. if (next_c == '.') {
  899. get();
  900. if (peek() == '.') {
  901. return ELLIPSIS;
  902. } else {
  903. unget('.');
  904. }
  905. }
  906. break;
  907. case ':':
  908. if (next_c == ':') return SCOPE;
  909. if (next_c == '>') return ']';
  910. break;
  911. case '*':
  912. if (next_c == '=') return TIMESEQUAL;
  913. break;
  914. case '/':
  915. if (next_c == '=') return DIVIDEEQUAL;
  916. break;
  917. case '%':
  918. if (next_c == '=') return MODEQUAL;
  919. if (next_c == '>') return '}';
  920. break;
  921. }
  922. return 0;
  923. }
  924. /**
  925. * Checks the next character in the stream to see if this might be a three-
  926. * character token; usually called in conjunction with check_digraph. Returns
  927. * 0 if it is not a three-character token.
  928. */
  929. int CPPPreprocessor::
  930. check_trigraph(int c) {
  931. int next_c = peek();
  932. switch (c) {
  933. case POINTSAT:
  934. if (next_c == '*') return POINTSAT_STAR;
  935. break;
  936. case LSHIFT:
  937. if (next_c == '=') return LSHIFTEQUAL;
  938. break;
  939. case RSHIFT:
  940. if (next_c == '=') return RSHIFTEQUAL;
  941. break;
  942. }
  943. return 0;
  944. }
  945. /**
  946. *
  947. */
  948. int CPPPreprocessor::
  949. skip_whitespace(int c) {
  950. while (c != EOF) {
  951. c = skip_comment(c);
  952. if (c == '\\') {
  953. // This does not usually occur in the middle of unquoted C++ code,
  954. // except before a newline character.
  955. if (peek() != '\n') {
  956. return '\\';
  957. }
  958. c = get();
  959. }
  960. if (!isspace(c)) {
  961. return c;
  962. }
  963. c = get();
  964. }
  965. return c;
  966. }
  967. /**
  968. *
  969. */
  970. int CPPPreprocessor::
  971. skip_comment(int c) {
  972. while (c == '/') {
  973. int next_c = peek();
  974. if (next_c == '*') {
  975. get();
  976. _last_cpp_comment = false;
  977. c = skip_c_comment(get());
  978. } else if (next_c == '/') {
  979. get();
  980. c = skip_cpp_comment(get());
  981. break;
  982. } else {
  983. _last_cpp_comment = false;
  984. return c;
  985. }
  986. }
  987. if (!isspace(c)) {
  988. _last_cpp_comment = false;
  989. }
  990. return c;
  991. }
  992. /**
  993. *
  994. */
  995. int CPPPreprocessor::
  996. skip_c_comment(int c) {
  997. YYLTYPE loc;
  998. loc.file = get_file();
  999. loc.first_line = get_line_number();
  1000. loc.first_column = get_col_number() - 2;
  1001. loc.last_line = 0;
  1002. loc.last_column = 0;
  1003. if (_save_comments) {
  1004. CPPCommentBlock *comment = new CPPCommentBlock;
  1005. _comments.push_back(comment);
  1006. comment->_file = loc.file;
  1007. comment->_line_number = loc.first_line;
  1008. comment->_last_line = loc.last_line;
  1009. comment->_col_number = loc.first_column;
  1010. comment->_c_style = true;
  1011. comment->_comment = "/*";
  1012. while (c != EOF) {
  1013. if (c == '*') {
  1014. comment->_comment += c;
  1015. c = get();
  1016. if (c == '/') {
  1017. comment->_comment += c;
  1018. comment->_last_line = get_line_number();
  1019. return get();
  1020. }
  1021. } else {
  1022. comment->_comment += c;
  1023. c = get();
  1024. }
  1025. }
  1026. loc.last_line = get_line_number();
  1027. comment->_last_line = loc.last_line;
  1028. warning("Comment is unterminated", loc);
  1029. } else {
  1030. CPPFile first_file = get_file();
  1031. int first_line_number = get_line_number();
  1032. int first_col_number = get_col_number() - 2;
  1033. while (c != EOF) {
  1034. if (c == '*') {
  1035. c = get();
  1036. if (c == '/') {
  1037. return get();
  1038. }
  1039. } else {
  1040. c = get();
  1041. }
  1042. }
  1043. loc.last_line = get_line_number();
  1044. warning("Comment is unterminated", loc);
  1045. }
  1046. return c;
  1047. }
  1048. /**
  1049. *
  1050. */
  1051. int CPPPreprocessor::
  1052. skip_cpp_comment(int c) {
  1053. if (_save_comments) {
  1054. CPPCommentBlock *comment;
  1055. int line_number = get_line_number();
  1056. if (c == '\n') {
  1057. // We have to subtract one from the line number as we just fetched a
  1058. // newline.
  1059. --line_number;
  1060. }
  1061. if (_last_cpp_comment && !_comments.empty() &&
  1062. _comments.back()->_last_line >= line_number - 1) {
  1063. // If the last non-whitespace character read was also part of a C++
  1064. // comment, then this is just a continuation of that comment block.
  1065. // However, if there was a line without comment in between, it starts a
  1066. // new block anyway.
  1067. comment = _comments.back();
  1068. assert(!comment->_c_style);
  1069. comment->_comment += "//";
  1070. } else {
  1071. // Otherwise, this begins a new comment block.
  1072. comment = new CPPCommentBlock;
  1073. comment->_file = get_file();
  1074. comment->_line_number = line_number;
  1075. comment->_last_line = line_number;
  1076. comment->_col_number = get_col_number() - 2;
  1077. comment->_c_style = false;
  1078. comment->_comment = "//";
  1079. _comments.push_back(comment);
  1080. }
  1081. while (c != EOF && c != '\n') {
  1082. comment->_comment += c;
  1083. c = get();
  1084. }
  1085. comment->_comment += '\n';
  1086. comment->_last_line = line_number;
  1087. _last_cpp_comment = true;
  1088. } else {
  1089. while (c != EOF && c != '\n') {
  1090. c = get();
  1091. }
  1092. }
  1093. return c;
  1094. }
  1095. /**
  1096. * Skips a C++14 digit separator that has just been found through peek().
  1097. */
  1098. int CPPPreprocessor::
  1099. skip_digit_separator(int c) {
  1100. if (c != '\'') {
  1101. return c;
  1102. }
  1103. get();
  1104. c = peek();
  1105. if (isdigit(c)) {
  1106. return c;
  1107. }
  1108. YYLTYPE loc;
  1109. loc.file = get_file();
  1110. loc.first_line = get_line_number();
  1111. loc.first_column = get_col_number();
  1112. loc.last_line = loc.first_line;
  1113. loc.last_column = loc.first_column;
  1114. if (c != '\'') {
  1115. // This assumes that this isn't a character constant directly follows a
  1116. // digit sequence, like 123'a' -- I can't think of a situation where
  1117. // that's legal anyway, though.
  1118. error("digit separator cannot occur at end of digit sequence", loc);
  1119. return c;
  1120. }
  1121. while (c == '\'') {
  1122. get();
  1123. ++loc.last_column;
  1124. c = peek();
  1125. }
  1126. error("adjacent digit separators", loc);
  1127. return c;
  1128. }
  1129. /**
  1130. *
  1131. */
  1132. int CPPPreprocessor::
  1133. process_directive(int c) {
  1134. assert(c == '#');
  1135. c = skip_whitespace(get());
  1136. int begin_line = get_line_number();
  1137. int begin_column = get_col_number();
  1138. string command, args;
  1139. c = get_preprocessor_command(c, command);
  1140. YYLTYPE loc;
  1141. loc.file = get_file();
  1142. loc.first_line = get_line_number();
  1143. loc.first_column = get_col_number();
  1144. c = get_preprocessor_args(c, args);
  1145. loc.last_line = get_line_number();
  1146. loc.last_column = 0;
  1147. #ifdef CPP_VERBOSE_LEX
  1148. indent(cerr, _files.size() * 2)
  1149. << "#" << command << " " << args << "\n";
  1150. #endif
  1151. if (command == "define") {
  1152. handle_define_directive(args, loc);
  1153. } else if (command == "undef") {
  1154. handle_undef_directive(args, loc);
  1155. } else if (command == "ifdef") {
  1156. handle_ifdef_directive(args, loc);
  1157. } else if (command == "ifndef") {
  1158. handle_ifndef_directive(args, loc);
  1159. } else if (command == "if") {
  1160. handle_if_directive(args, loc);
  1161. } else if (command == "else" || command == "elif") {
  1162. // Presumably this follows some #if or #ifdef. We don't bother to check
  1163. // this, however.
  1164. skip_false_if_block(false);
  1165. } else if (command == "endif") {
  1166. // Presumably this follows some #if or #ifdef. We don't bother to check
  1167. // this, however.
  1168. } else if (command == "include") {
  1169. handle_include_directive(args, loc);
  1170. } else if (command == "pragma") {
  1171. handle_pragma_directive(args, loc);
  1172. } else if (command == "ident") {
  1173. // Quietly ignore idents.
  1174. } else if (command == "error") {
  1175. handle_error_directive(args, loc);
  1176. } else {
  1177. loc.first_line = begin_line;
  1178. loc.first_column = begin_column;
  1179. loc.last_line = begin_line;
  1180. loc.last_column = begin_column + command.size() - 1;
  1181. warning("Ignoring unknown directive #" + command, loc);
  1182. }
  1183. _start_of_line = true;
  1184. return '\n';
  1185. }
  1186. /**
  1187. *
  1188. */
  1189. int CPPPreprocessor::
  1190. get_preprocessor_command(int c, string &command) {
  1191. // The next sequence of characters is the command.
  1192. while (c != EOF && (isalnum(c) || c == '_')) {
  1193. command += c;
  1194. c = get();
  1195. }
  1196. while (c != EOF && c != '\n' && isspace(c)) {
  1197. c = get();
  1198. }
  1199. return c;
  1200. }
  1201. /**
  1202. *
  1203. */
  1204. int CPPPreprocessor::
  1205. get_preprocessor_args(int c, string &args) {
  1206. // Following the command, the rest of the line, as well as any text on
  1207. // successive lines, is part of the arguments to the command.
  1208. while (c != EOF && c != '\n') {
  1209. if (c == '\\') {
  1210. int next_c = get();
  1211. if (next_c == '\n') {
  1212. // Here we have an escaped newline: a continuation.
  1213. args += '\n';
  1214. } else {
  1215. // Just a backslash followed by some non-backslash, keep both.
  1216. args += c;
  1217. if (next_c != EOF) {
  1218. args += next_c;
  1219. }
  1220. }
  1221. } else {
  1222. args += c;
  1223. }
  1224. c = skip_comment(get());
  1225. }
  1226. // Remove any leading and trailing whitespace from the args.
  1227. args = trim_blanks(args);
  1228. return c;
  1229. }
  1230. /**
  1231. *
  1232. */
  1233. void CPPPreprocessor::
  1234. handle_define_directive(const string &args, const YYLTYPE &loc) {
  1235. if (args.empty()) {
  1236. warning("Ignoring empty #define directive", loc);
  1237. } else {
  1238. CPPManifest *manifest = new CPPManifest(args, loc);
  1239. manifest->_vis = preprocessor_vis;
  1240. if (!manifest->_has_parameters) {
  1241. string expr_string = manifest->expand();
  1242. if (!expr_string.empty()) {
  1243. manifest->_expr = parse_expr(expr_string, global_scope, global_scope, loc);
  1244. }
  1245. }
  1246. pair<Manifests::iterator, bool> result =
  1247. _manifests.insert(Manifests::value_type(manifest->_name, manifest));
  1248. if (!result.second) {
  1249. // There was already a macro with this name. Delete the old.
  1250. CPPManifest *other = result.first->second;
  1251. warning("redefinition of macro '" + manifest->_name + "'", loc);
  1252. warning("previous definition is here", other->_loc);
  1253. delete other;
  1254. result.first->second = manifest;
  1255. }
  1256. }
  1257. }
  1258. /**
  1259. *
  1260. */
  1261. void CPPPreprocessor::
  1262. handle_undef_directive(const string &args, const YYLTYPE &loc) {
  1263. if (args.empty()) {
  1264. warning("Ignoring empty #undef directive", loc);
  1265. } else {
  1266. Manifests::iterator mi = _manifests.find(args);
  1267. if (mi != _manifests.end()) {
  1268. _manifests.erase(mi);
  1269. }
  1270. }
  1271. }
  1272. /**
  1273. *
  1274. */
  1275. void CPPPreprocessor::
  1276. handle_ifdef_directive(const string &args, const YYLTYPE &loc) {
  1277. Manifests::const_iterator mi = _manifests.find(args);
  1278. if (mi != _manifests.end()) {
  1279. // The macro is defined. We continue.
  1280. return;
  1281. }
  1282. // The macro is undefined. Skip stuff.
  1283. skip_false_if_block(true);
  1284. }
  1285. /**
  1286. *
  1287. */
  1288. void CPPPreprocessor::
  1289. handle_ifndef_directive(const string &args, const YYLTYPE &loc) {
  1290. Manifests::const_iterator mi = _manifests.find(args);
  1291. if (mi == _manifests.end()) {
  1292. // The macro is undefined. We continue.
  1293. return;
  1294. }
  1295. // The macro is defined. Skip stuff.
  1296. skip_false_if_block(true);
  1297. }
  1298. /**
  1299. *
  1300. */
  1301. void CPPPreprocessor::
  1302. handle_if_directive(const string &args, const YYLTYPE &loc) {
  1303. // When expanding manifests, we should replace unknown macros with 0.
  1304. string expr = expand_manifests(args, true, loc);
  1305. int expression_result = 0;
  1306. CPPExpressionParser ep(current_scope, global_scope);
  1307. ep._verbose = 0;
  1308. if (ep.parse_expr(expr, *this)) {
  1309. CPPExpression::Result result = ep._expr->evaluate();
  1310. if (result._type == CPPExpression::RT_error) {
  1311. ostringstream strm;
  1312. strm << *ep._expr;
  1313. warning("Ignoring invalid expression " + strm.str(), loc);
  1314. } else {
  1315. expression_result = result.as_integer();
  1316. }
  1317. } else {
  1318. warning("Ignoring invalid expression " + args, loc);
  1319. }
  1320. if (expression_result) {
  1321. // The expression result is true. We continue.
  1322. return;
  1323. }
  1324. // The expression result is false. Skip stuff.
  1325. skip_false_if_block(true);
  1326. }
  1327. /**
  1328. *
  1329. */
  1330. void CPPPreprocessor::
  1331. handle_include_directive(const string &args, const YYLTYPE &loc) {
  1332. bool okflag = false;
  1333. Filename filename;
  1334. Filename filename_as_referenced;
  1335. bool angle_quotes = false;
  1336. string expr = args;
  1337. // The filename to include might actually be hidden within a manifest
  1338. // definition. Wow. FreeType depends on this.
  1339. // Just to play things safe, since our manifest-expansion logic might not
  1340. // filter out quotes and angle brackets properly, we'll only expand
  1341. // manifests if we don't begin with a quote or bracket.
  1342. if (!expr.empty() && (expr[0] != '"' && expr[0] != '<')) {
  1343. expr = expand_manifests(expr, false, loc);
  1344. }
  1345. if (!expr.empty()) {
  1346. if (expr[0] == '"' && expr[expr.size() - 1] == '"') {
  1347. filename = expr.substr(1, expr.size() - 2);
  1348. okflag = true;
  1349. if (_files.size() == 1) {
  1350. // If we're currently processing a top-level file, record the include
  1351. // directive. We don't need to record includes from included files.
  1352. _quote_includes.insert(filename);
  1353. }
  1354. } else if (expr[0] == '<' && expr[expr.size() - 1] == '>') {
  1355. filename = expr.substr(1, expr.size() - 2);
  1356. if (!_noangles) {
  1357. // If _noangles is true, we don't make a distinction between angle
  1358. // brackets and quote marks--all #include statements are treated the
  1359. // same, as if they used quote marks.
  1360. angle_quotes = true;
  1361. }
  1362. okflag = true;
  1363. if (_files.size() == 1) {
  1364. // If we're currently processing a top-level file, record the include
  1365. // directive. We don't need to record includes from included files.
  1366. _angle_includes.insert(filename);
  1367. }
  1368. }
  1369. }
  1370. filename.set_text();
  1371. filename_as_referenced = filename;
  1372. // Now look for the filename. If we didn't use angle quotes, look first in
  1373. // the current directory.
  1374. bool found_file = false;
  1375. CPPFile::Source source = CPPFile::S_none;
  1376. if (okflag) {
  1377. found_file = false;
  1378. // Search the current directory.
  1379. if (!angle_quotes && !found_file && filename.exists()) {
  1380. found_file = true;
  1381. source = CPPFile::S_local;
  1382. }
  1383. // Search the same directory as the includer.
  1384. if (!angle_quotes && !found_file) {
  1385. Filename match(get_file()._filename.get_dirname(), filename);
  1386. if (match.exists()) {
  1387. filename = match;
  1388. found_file = true;
  1389. source = CPPFile::S_alternate;
  1390. }
  1391. }
  1392. // Now search the angle-include-path
  1393. if (angle_quotes && !found_file && filename.resolve_filename(_angle_include_path)) {
  1394. found_file = true;
  1395. source = CPPFile::S_system;
  1396. }
  1397. // Now search the quote-include-path
  1398. if (!angle_quotes && !found_file) {
  1399. for (size_t dir=0; dir<_quote_include_path.get_num_directories(); dir++) {
  1400. Filename match(_quote_include_path.get_directory(dir), filename);
  1401. if (match.exists()) {
  1402. filename = match;
  1403. found_file = true;
  1404. source = _quote_include_kind[dir];
  1405. }
  1406. }
  1407. }
  1408. if (!found_file) {
  1409. warning("Cannot find " + filename.get_fullpath(), loc);
  1410. } else {
  1411. _last_c = '\0';
  1412. // If it was explicitly named on the command-line, mark it S_local.
  1413. filename.make_absolute();
  1414. if (_explicit_files.count(filename)) {
  1415. source = CPPFile::S_local;
  1416. }
  1417. CPPFile file(filename, filename_as_referenced, source);
  1418. // Don't include it if we included it before and it had #pragma once.
  1419. ParsedFiles::const_iterator it = _parsed_files.find(file);
  1420. if (it != _parsed_files.end() && it->_pragma_once) {
  1421. return;
  1422. }
  1423. if (!push_file(file)) {
  1424. warning("Unable to read " + filename.get_fullpath(), loc);
  1425. }
  1426. }
  1427. } else {
  1428. warning("Ignoring invalid #include directive", loc);
  1429. }
  1430. }
  1431. /**
  1432. *
  1433. */
  1434. void CPPPreprocessor::
  1435. handle_pragma_directive(const string &args, const YYLTYPE &loc) {
  1436. if (args == "once") {
  1437. ParsedFiles::iterator it = _parsed_files.find(loc.file);
  1438. assert(it != _parsed_files.end());
  1439. it->_pragma_once = true;
  1440. }
  1441. }
  1442. /**
  1443. *
  1444. */
  1445. void CPPPreprocessor::
  1446. handle_error_directive(const string &args, const YYLTYPE &loc) {
  1447. error(args, loc);
  1448. }
  1449. /**
  1450. * We come here when we fail an #if or an #ifdef test, or when we reach the
  1451. * #else clause to something we didn't fail. This function skips all text up
  1452. * until the matching #endif.
  1453. */
  1454. void CPPPreprocessor::
  1455. skip_false_if_block(bool consider_elifs) {
  1456. int level = 0;
  1457. _save_comments = false;
  1458. int c = skip_comment(get());
  1459. while (c != EOF) {
  1460. if (c == '#' && _start_of_line) {
  1461. c = skip_whitespace(get());
  1462. YYLTYPE loc;
  1463. loc.file = get_file();
  1464. loc.first_line = get_line_number();
  1465. loc.first_column = get_col_number();
  1466. loc.last_line = loc.first_line;
  1467. loc.last_column = loc.first_column;
  1468. // Is this it?
  1469. string command, args;
  1470. c = get_preprocessor_command(c, command);
  1471. c = get_preprocessor_args(c, args);
  1472. if (command == "if" || command == "ifdef" || command == "ifndef") {
  1473. // Hmm, a nested if block. Even more to skip.
  1474. level++;
  1475. } else if (command == "else") {
  1476. if (level == 0 && consider_elifs) {
  1477. // This will do!
  1478. _save_comments = true;
  1479. return;
  1480. }
  1481. } else if (command == "elif") {
  1482. if (level == 0 && consider_elifs) {
  1483. // If we pass this test, we're in.
  1484. _save_comments = true;
  1485. handle_if_directive(args, loc);
  1486. return;
  1487. }
  1488. } else if (command == "endif") {
  1489. // Skip any args.
  1490. if (level == 0) {
  1491. // Here's the end!
  1492. _save_comments = true;
  1493. return;
  1494. }
  1495. level--;
  1496. }
  1497. } else {
  1498. c = skip_comment(get());
  1499. }
  1500. }
  1501. _save_comments = true;
  1502. }
  1503. /**
  1504. *
  1505. */
  1506. CPPToken CPPPreprocessor::
  1507. get_quoted_char(int c) {
  1508. YYLTYPE loc;
  1509. loc.file = get_file();
  1510. loc.first_line = get_line_number();
  1511. loc.first_column = get_col_number();
  1512. string str = scan_quoted(c);
  1513. YYSTYPE result;
  1514. if (!str.empty()) {
  1515. result.u.integer = (int)str[0];
  1516. } else {
  1517. result.u.integer = 0;
  1518. }
  1519. return get_literal(CHAR_TOK, loc, str, result);
  1520. }
  1521. /**
  1522. *
  1523. */
  1524. CPPToken CPPPreprocessor::
  1525. get_quoted_string(int c) {
  1526. YYLTYPE loc;
  1527. loc.file = get_file();
  1528. loc.first_line = get_line_number();
  1529. loc.first_column = get_col_number();
  1530. string str = scan_quoted(c);
  1531. return get_literal(SIMPLE_STRING, loc, str);
  1532. }
  1533. /**
  1534. *
  1535. */
  1536. CPPToken CPPPreprocessor::
  1537. get_identifier(int c) {
  1538. YYLTYPE loc;
  1539. loc.file = get_file();
  1540. loc.first_line = get_line_number();
  1541. loc.first_column = get_col_number();
  1542. loc.last_line = loc.first_line;
  1543. loc.last_column = loc.first_column;
  1544. string name(1, (char)c);
  1545. c = peek();
  1546. while (c != EOF && (isalnum(c) || c == '_')) {
  1547. name += get();
  1548. c = peek();
  1549. }
  1550. loc.last_line = get_line_number();
  1551. loc.last_column = get_col_number();
  1552. if ((c == '\'' || c == '"') &&
  1553. (name == "L" || name == "u8" ||
  1554. name == "u" || name == "U")) {
  1555. // This is actually a wide-character or wide-string literal or some such.
  1556. // Figure out the correct character type to use.
  1557. CPPExpression::Type type;
  1558. if (name == "L") {
  1559. type = CPPExpression::T_wstring;
  1560. } else if (name == "u8") {
  1561. type = CPPExpression::T_u8string;
  1562. } else if (name == "u") {
  1563. type = CPPExpression::T_u16string;
  1564. } else if (name == "U") {
  1565. type = CPPExpression::T_u32string;
  1566. }
  1567. get();
  1568. string str = scan_quoted(c);
  1569. loc.last_line = get_line_number();
  1570. loc.last_column = get_col_number();
  1571. YYSTYPE result;
  1572. if (c == '\'') {
  1573. // We don't really care about the type for now.
  1574. if (!str.empty()) {
  1575. result.u.integer = (int)str[0];
  1576. } else {
  1577. result.u.integer = 0;
  1578. }
  1579. return get_literal(CHAR_TOK, loc, str, result);
  1580. } else {
  1581. result.u.expr = new CPPExpression(str);
  1582. result.u.expr->_type = type;
  1583. return get_literal(STRING_LITERAL, loc, str, result);
  1584. }
  1585. }
  1586. _last_c = 0;
  1587. // Is it a manifest?
  1588. Manifests::const_iterator mi = _manifests.find(name);
  1589. if (mi != _manifests.end() && !should_ignore_manifest((*mi).second)) {
  1590. return expand_manifest((*mi).second);
  1591. }
  1592. // Check for keywords.
  1593. int kw = check_keyword(name);
  1594. // Update our internal visibility flag.
  1595. switch (kw) {
  1596. case KW_BEGIN_PUBLISH:
  1597. preprocessor_vis = V_published;
  1598. break;
  1599. case KW_END_PUBLISH:
  1600. preprocessor_vis = V_public;
  1601. break;
  1602. }
  1603. if (kw != 0) {
  1604. YYSTYPE result;
  1605. result.u.identifier = (CPPIdentifier *)NULL;
  1606. return CPPToken(kw, loc, name, result);
  1607. }
  1608. return CPPToken(SIMPLE_IDENTIFIER, loc, name);
  1609. }
  1610. /**
  1611. * Under the assumption that we've just parsed a string or real constant,
  1612. * parse a following custom literal, and returns a token for it.
  1613. */
  1614. CPPToken CPPPreprocessor::
  1615. get_literal(int token, YYLTYPE loc, const string &str, const YYSTYPE &value) {
  1616. string suffix;
  1617. int c = peek();
  1618. if (isalpha(c) || c == '_') {
  1619. // A literal seems to be following directly.
  1620. while (c != EOF && (isalnum(c) || c == '_')) {
  1621. suffix += get();
  1622. c = peek();
  1623. }
  1624. }
  1625. loc.last_line = get_line_number();
  1626. loc.last_column = get_col_number();
  1627. if (suffix.empty()) {
  1628. // There is no suffix.
  1629. return CPPToken(token, loc, str, value);
  1630. }
  1631. // Handle built-in literal suffixes.
  1632. if (token == INTEGER) {
  1633. if (cmp_nocase(suffix, "u") == 0 ||
  1634. cmp_nocase(suffix, "l") == 0 ||
  1635. cmp_nocase(suffix, "ul") == 0 || cmp_nocase(suffix, "lu") == 0 ||
  1636. cmp_nocase(suffix, "ll") == 0 ||
  1637. cmp_nocase(suffix, "ull") == 0 || cmp_nocase(suffix, "llu") == 0) {
  1638. // These are built-in integer suffixes. Right now, we don't try to
  1639. // distinguish between them.
  1640. return CPPToken(INTEGER, loc, str, value);
  1641. }
  1642. } else if (token == REAL) {
  1643. if (suffix == "f" || suffix == "F" ||
  1644. suffix == "l" || suffix == "L") {
  1645. return CPPToken(REAL, loc, str, value);
  1646. }
  1647. }
  1648. // Find the literal operator for this literal.
  1649. CPPIdentifier *ident = new CPPIdentifier("operator \"\" " + suffix);
  1650. CPPDeclaration *decl = ident->find_symbol(current_scope, global_scope, this);
  1651. if (decl == NULL || decl->get_subtype() != CPPDeclaration::ST_function_group) {
  1652. error("unknown literal suffix " + suffix, loc);
  1653. return CPPToken(token, loc, str, value);
  1654. }
  1655. // Find the overload with the appropriate signature.
  1656. CPPExpression *expr = NULL;
  1657. CPPInstance *instance = NULL;
  1658. CPPInstance *raw_instance = NULL;
  1659. CPPFunctionGroup *fgroup = decl->as_function_group();
  1660. CPPFunctionGroup::Instances::iterator it;
  1661. for (it = fgroup->_instances.begin(); it != fgroup->_instances.end(); ++it) {
  1662. if ((*it)->_type == NULL) {
  1663. continue;
  1664. }
  1665. CPPFunctionType *ftype = (*it)->_type->as_function_type();
  1666. if (ftype == NULL || ftype->_parameters == NULL) {
  1667. continue;
  1668. }
  1669. CPPParameterList::Parameters &params = ftype->_parameters->_parameters;
  1670. if (token == STRING_LITERAL || token == SIMPLE_STRING) {
  1671. // A custom string literal must take a second size_t argument.
  1672. if (params.size() != 2) continue;
  1673. } else {
  1674. if (params.size() != 1) continue;
  1675. }
  1676. CPPInstance *param = params[0];
  1677. if (param == NULL || param->_type == NULL) {
  1678. continue;
  1679. }
  1680. CPPType *type = param->_type;
  1681. while (type->get_subtype() == CPPDeclaration::ST_const) {
  1682. type = type->as_const_type()->_wrapped_around;
  1683. }
  1684. if (type->get_subtype() == CPPDeclaration::ST_simple) {
  1685. // It's a primitive type. Check that it matches the appropriate token.
  1686. CPPSimpleType::Type simple = type->as_simple_type()->_type;
  1687. if (token == INTEGER && simple == CPPSimpleType::T_int) {
  1688. expr = new CPPExpression(value.u.integer);
  1689. instance = (*it);
  1690. break;
  1691. } else if (token == REAL && simple == CPPSimpleType::T_double) {
  1692. expr = new CPPExpression(value.u.real);
  1693. instance = (*it);
  1694. break;
  1695. } else if (token == CHAR_TOK && (simple == CPPSimpleType::T_char ||
  1696. simple == CPPSimpleType::T_wchar_t ||
  1697. simple == CPPSimpleType::T_char16_t ||
  1698. simple == CPPSimpleType::T_char32_t)) {
  1699. // We currently don't have the means to check the exact character
  1700. // type.
  1701. expr = new CPPExpression(value.u.integer);
  1702. instance = (*it);
  1703. break;
  1704. }
  1705. } else if (type->get_subtype() == CPPDeclaration::ST_pointer) {
  1706. // Must be a const pointer. Unwrap it.
  1707. type = type->as_pointer_type()->_pointing_at;
  1708. if (type == NULL || type->get_subtype() != CPPDeclaration::ST_const) {
  1709. continue;
  1710. }
  1711. type = type->as_const_type()->_wrapped_around;
  1712. if (type == NULL || type->get_subtype() != CPPDeclaration::ST_simple) {
  1713. continue;
  1714. }
  1715. CPPSimpleType::Type simple = type->as_simple_type()->_type;
  1716. if (simple == CPPSimpleType::T_char && params.size() == 1) {
  1717. // This is the raw literal operator. Store it, but don't break; a
  1718. // non-raw version of the operator might follow, which we'd prefer.
  1719. raw_instance = (*it);
  1720. } else if (token == SIMPLE_STRING && simple == CPPSimpleType::T_char) {
  1721. expr = new CPPExpression(str);
  1722. instance = (*it);
  1723. break;
  1724. } else if (token == STRING_LITERAL) {
  1725. // Verify that the character type of the string literal matches the
  1726. // character type of the parameter.
  1727. CPPExpression::Type str_type = value.u.expr->_type;
  1728. if ((str_type == CPPExpression::T_string && simple == CPPSimpleType::T_char) ||
  1729. (str_type == CPPExpression::T_wstring && simple == CPPSimpleType::T_wchar_t) ||
  1730. (str_type == CPPExpression::T_u8string && simple == CPPSimpleType::T_char) ||
  1731. (str_type == CPPExpression::T_u16string && simple == CPPSimpleType::T_char16_t) ||
  1732. (str_type == CPPExpression::T_u32string && simple == CPPSimpleType::T_char32_t)) {
  1733. expr = value.u.expr;
  1734. instance = (*it);
  1735. break;
  1736. }
  1737. }
  1738. }
  1739. }
  1740. YYSTYPE result;
  1741. if (instance != NULL) {
  1742. result.u.expr = new CPPExpression(CPPExpression::literal(expr, instance));
  1743. return CPPToken(CUSTOM_LITERAL, loc, str, result);
  1744. }
  1745. if ((token == REAL || token == INTEGER) && raw_instance != NULL) {
  1746. // For numeric constants, we can fall back to a raw literal operator.
  1747. result.u.expr = new CPPExpression(CPPExpression::raw_literal(str, instance));
  1748. return CPPToken(CUSTOM_LITERAL, loc, str, result);
  1749. }
  1750. error(fgroup->_name + " has no suitable overload for literal of this type", loc);
  1751. result.u.expr = NULL;
  1752. return CPPToken(CUSTOM_LITERAL, loc, str, result);
  1753. }
  1754. /**
  1755. *
  1756. */
  1757. CPPToken CPPPreprocessor::
  1758. expand_manifest(const CPPManifest *manifest) {
  1759. vector_string args;
  1760. if (manifest->_has_parameters) {
  1761. // Hmm, we're expecting arguments.
  1762. extract_manifest_args(manifest->_name, manifest->_num_parameters,
  1763. manifest->_variadic_param, args);
  1764. }
  1765. string expanded = " " + manifest->expand(args) + " ";
  1766. push_string(expanded, true);
  1767. if (!manifest->_has_parameters) {
  1768. // If the manifest does not use arguments, then disallow recursive
  1769. // expansion.
  1770. _files.back()._ignore_manifest = manifest;
  1771. }
  1772. #ifdef CPP_VERBOSE_LEX
  1773. indent(cerr, _files.size() * 2)
  1774. << "Expanding " << manifest->_name << " to " << expanded << "\n";
  1775. #endif
  1776. return internal_get_next_token();
  1777. }
  1778. /**
  1779. *
  1780. */
  1781. void CPPPreprocessor::
  1782. extract_manifest_args(const string &name, int num_args, int va_arg,
  1783. vector_string &args) {
  1784. CPPFile first_file = get_file();
  1785. int first_line = get_line_number();
  1786. int first_col = get_col_number();
  1787. // Skip whitespace till paren.
  1788. int c = _last_c;
  1789. _last_c = '\0';
  1790. if (c == 0) {
  1791. c = get();
  1792. }
  1793. while (c != EOF && isspace(c)) {
  1794. c = get();
  1795. }
  1796. if (c != '(') {
  1797. // No paren, so we have only one arg.
  1798. string arg;
  1799. while (c != EOF && (isalnum(c) || c == '_')) {
  1800. arg += c;
  1801. c = get();
  1802. }
  1803. args.push_back(arg);
  1804. } else {
  1805. // Skip paren.
  1806. c = skip_whitespace(get());
  1807. int paren_level = 1;
  1808. string arg;
  1809. while (c != EOF) {
  1810. if (c == ',' && paren_level == 1) {
  1811. args.push_back(arg);
  1812. arg = "";
  1813. c = get();
  1814. } else if (c == '"' || c == '\'') {
  1815. // Quoted string or character.
  1816. int quote_mark = c;
  1817. arg += c;
  1818. c = get();
  1819. while (c != EOF && c != quote_mark && c != '\n') {
  1820. if (c == '\\') {
  1821. arg += c;
  1822. c = get();
  1823. }
  1824. if (c != EOF) {
  1825. arg += c;
  1826. c = get();
  1827. }
  1828. }
  1829. arg += c;
  1830. c = get();
  1831. } else if (c == '(') {
  1832. arg += '(';
  1833. ++paren_level;
  1834. c = get();
  1835. } else if (c == ')') {
  1836. --paren_level;
  1837. if (paren_level == 0) {
  1838. break;
  1839. }
  1840. arg += ')';
  1841. c = get();
  1842. } else if (isspace(c)) {
  1843. // Skip extra whitespace.
  1844. c = skip_whitespace(c);
  1845. if (!arg.empty()) {
  1846. arg += ' ';
  1847. }
  1848. } else if (c == '\\') {
  1849. // It could be a slash before a newline. If so, that's whitespace as
  1850. // well.
  1851. c = get();
  1852. if (c != '\n') {
  1853. arg += '\\';
  1854. } else if (!arg.empty()) {
  1855. arg += ' ';
  1856. c = skip_whitespace(get());
  1857. }
  1858. } else {
  1859. arg += c;
  1860. c = get();
  1861. }
  1862. }
  1863. if (num_args != 0 || !arg.empty()) {
  1864. args.push_back(arg);
  1865. }
  1866. }
  1867. YYLTYPE loc;
  1868. loc.first_line = first_line;
  1869. loc.first_column = first_col;
  1870. loc.last_line = first_line;
  1871. loc.last_column = first_col;
  1872. loc.file = first_file;
  1873. if ((int)args.size() < num_args) {
  1874. warning("Not enough arguments for manifest " + name, loc);
  1875. } else if (va_arg < 0 && (int)args.size() > num_args) {
  1876. warning("Too many arguments for manifest " + name, loc);
  1877. }
  1878. }
  1879. /**
  1880. * Expands the defined(manifest) function to either 1 or 0, depending on
  1881. * whether the manifest exists.
  1882. */
  1883. void CPPPreprocessor::
  1884. expand_defined_function(string &expr, size_t q, size_t &p) {
  1885. string result;
  1886. vector_string args;
  1887. extract_manifest_args_inline("defined", 1, -1, args, expr, p);
  1888. if (args.size() >= 1) {
  1889. const string &manifest_name = args[0];
  1890. Manifests::const_iterator mi = _manifests.find(manifest_name);
  1891. if (mi != _manifests.end()) {
  1892. // The macro is defined; the result is "1".
  1893. result = "1";
  1894. } else {
  1895. // The macro is undefined; the result is "0".
  1896. result = "0";
  1897. }
  1898. }
  1899. expr = expr.substr(0, q) + result + expr.substr(p);
  1900. p = q + result.size();
  1901. }
  1902. /**
  1903. *
  1904. */
  1905. void CPPPreprocessor::
  1906. expand_manifest_inline(string &expr, size_t q, size_t &p,
  1907. const CPPManifest *manifest) {
  1908. vector_string args;
  1909. if (manifest->_has_parameters) {
  1910. extract_manifest_args_inline(manifest->_name, manifest->_num_parameters,
  1911. manifest->_variadic_param, args, expr, p);
  1912. }
  1913. string result = manifest->expand(args);
  1914. expr = expr.substr(0, q) + result + expr.substr(p);
  1915. p = q + result.size();
  1916. }
  1917. /**
  1918. *
  1919. */
  1920. void CPPPreprocessor::
  1921. extract_manifest_args_inline(const string &name, int num_args,
  1922. int va_arg, vector_string &args,
  1923. const string &expr, size_t &p) {
  1924. // Skip whitespace till paren.
  1925. while (p < expr.size() && isspace(expr[p])) {
  1926. p++;
  1927. }
  1928. if (p >= expr.size() || expr[p] != '(') {
  1929. // No paren, so we have only one arg.
  1930. size_t q = p;
  1931. while (p < expr.size() && (isalnum(expr[p]) || expr[p] == '_')) {
  1932. p++;
  1933. }
  1934. args.push_back(expr.substr(q, p - q));
  1935. } else if (expr[p] == '"' || expr[p] == '\'') {
  1936. // Quoted string or character.
  1937. int quote_mark = expr[p];
  1938. p++;
  1939. while (p < expr.size() && expr[p] != quote_mark && expr[p] != '\n') {
  1940. if (expr[p] == '\\') {
  1941. p++;
  1942. }
  1943. if (p < expr.size()) {
  1944. p++;
  1945. }
  1946. }
  1947. p++;
  1948. } else {
  1949. // Skip paren.
  1950. p++;
  1951. size_t q = p;
  1952. while (p < expr.size() && expr[p] != ')') {
  1953. if (expr[p] == ',') {
  1954. args.push_back(expr.substr(q, p - q));
  1955. q = p+1;
  1956. } else if (expr[p] == '(') {
  1957. // Nested parens.
  1958. int paren_level = 1;
  1959. while (p+1 < expr.size() && paren_level > 0) {
  1960. p++;
  1961. if (expr[p] == '(') {
  1962. paren_level++;
  1963. } else if (expr[p] == ')') {
  1964. paren_level--;
  1965. }
  1966. }
  1967. }
  1968. p++;
  1969. }
  1970. args.push_back(expr.substr(q, p - q));
  1971. if (p < expr.size() && expr[p] == ')') {
  1972. p++;
  1973. }
  1974. }
  1975. if ((int)args.size() < num_args) {
  1976. warning("Not enough arguments for manifest " + name);
  1977. } else if (va_arg < 0 && (int)args.size() > num_args) {
  1978. warning("Too many arguments for manifest " + name);
  1979. }
  1980. }
  1981. /**
  1982. * Assuming that we've just read a digit or a period indicating the start of a
  1983. * number, read the rest.
  1984. */
  1985. CPPToken CPPPreprocessor::
  1986. get_number(int c) {
  1987. YYLTYPE loc;
  1988. loc.file = get_file();
  1989. loc.first_line = get_line_number();
  1990. loc.first_column = get_col_number();
  1991. loc.last_line = loc.first_line;
  1992. loc.last_column = loc.first_column;
  1993. string num(1, (char)c);
  1994. bool leading_zero = (c == '0');
  1995. bool decimal_point = (c == '.');
  1996. c = skip_digit_separator(peek());
  1997. if (leading_zero && (c == 'x' || c == 'X')) {
  1998. // Here we have a hex number.
  1999. num += get();
  2000. c = peek();
  2001. while (c != EOF && (isdigit(c) || (tolower(c) >= 'a' && tolower(c) <= 'f'))) {
  2002. num += get();
  2003. c = skip_digit_separator(peek());
  2004. }
  2005. loc.last_line = get_line_number();
  2006. loc.last_column = get_col_number();
  2007. YYSTYPE result;
  2008. result.u.integer = strtol(num.c_str(), (char **)NULL, 16);
  2009. return get_literal(INTEGER, loc, num, result);
  2010. } else if (leading_zero && (c == 'b' || c == 'B')) {
  2011. // A C++14-style binary number.
  2012. get();
  2013. c = peek();
  2014. string bin(1, (char)c);
  2015. while (c != EOF && (c == '0' || c == '1')) {
  2016. bin += get();
  2017. c = skip_digit_separator(peek());
  2018. }
  2019. loc.last_line = get_line_number();
  2020. loc.last_column = get_col_number();
  2021. YYSTYPE result;
  2022. result.u.integer = strtol(bin.c_str(), (char **)NULL, 2);
  2023. return get_literal(INTEGER, loc, bin, result);
  2024. }
  2025. while (c != EOF && isdigit(c)) {
  2026. num += get();
  2027. c = skip_digit_separator(peek());
  2028. }
  2029. if (c == '.' && !decimal_point) {
  2030. // Now we have a floating-point number.
  2031. decimal_point = true;
  2032. num += get();
  2033. c = peek();
  2034. while (c != EOF && isdigit(c)) {
  2035. num += get();
  2036. c = peek();
  2037. }
  2038. }
  2039. if (decimal_point || c == 'e' || c == 'E') {
  2040. if (tolower(c) == 'e') {
  2041. // An exponent is allowed.
  2042. num += get();
  2043. c = peek();
  2044. if (c == '-' || c == '+') {
  2045. num += get();
  2046. c = peek();
  2047. }
  2048. while (c != EOF && isdigit(c)) {
  2049. num += get();
  2050. c = skip_digit_separator(peek());
  2051. }
  2052. }
  2053. loc.last_line = get_line_number();
  2054. loc.last_column = get_col_number();
  2055. YYSTYPE result;
  2056. result.u.real = pstrtod(num.c_str(), (char **)NULL);
  2057. return get_literal(REAL, loc, num, result);
  2058. }
  2059. // This is a decimal or octal integer number.
  2060. loc.last_line = get_line_number();
  2061. loc.last_column = get_col_number();
  2062. YYSTYPE result;
  2063. if (leading_zero) {
  2064. // A leading zero implies an octal number. strtol() is supposed to be
  2065. // able to make this distinction by itself, but we'll do it explicitly
  2066. // just to be sure.
  2067. result.u.integer = strtol(num.c_str(), (char **)NULL, 8);
  2068. } else {
  2069. // A decimal (base 10) integer.
  2070. result.u.integer = strtol(num.c_str(), (char **)NULL, 10);
  2071. }
  2072. return get_literal(INTEGER, loc, num, result);
  2073. }
  2074. /**
  2075. *
  2076. */
  2077. int CPPPreprocessor::
  2078. check_keyword(const string &name) {
  2079. if (name == "alignas") return KW_ALIGNAS;
  2080. if (name == "alignof") return KW_ALIGNOF;
  2081. if (name == "__alignof") return KW_ALIGNOF;
  2082. if (name == "__alignof__") return KW_ALIGNOF;
  2083. if (name == "auto") return KW_AUTO;
  2084. if (name == "__begin_publish") return KW_BEGIN_PUBLISH;
  2085. if (name == "__blocking") return KW_BLOCKING;
  2086. if (name == "bool") return KW_BOOL;
  2087. if (name == "catch") return KW_CATCH;
  2088. if (name == "char") return KW_CHAR;
  2089. if (name == "char16_t") return KW_CHAR16_T;
  2090. if (name == "char32_t") return KW_CHAR32_T;
  2091. if (name == "class") return KW_CLASS;
  2092. if (name == "const") return KW_CONST;
  2093. if (name == "__const") return KW_CONST;
  2094. if (name == "__const__") return KW_CONST;
  2095. if (name == "constexpr") return KW_CONSTEXPR;
  2096. if (name == "const_cast") return KW_CONST_CAST;
  2097. if (name == "decltype") return KW_DECLTYPE;
  2098. if (name == "default") return KW_DEFAULT;
  2099. if (name == "delete") return KW_DELETE;
  2100. if (name == "double") return KW_DOUBLE;
  2101. if (name == "dynamic_cast") return KW_DYNAMIC_CAST;
  2102. if (name == "else") return KW_ELSE;
  2103. if (name == "__end_publish") return KW_END_PUBLISH;
  2104. if (name == "enum") return KW_ENUM;
  2105. if (name == "extern") return KW_EXTERN;
  2106. if (name == "__extension") return KW_EXTENSION;
  2107. if (name == "explicit") return KW_EXPLICIT;
  2108. if (name == "__published") return KW_PUBLISHED;
  2109. if (name == "false") return KW_FALSE;
  2110. if (name == "final") return KW_FINAL;
  2111. if (name == "float") return KW_FLOAT;
  2112. if (name == "friend") return KW_FRIEND;
  2113. if (name == "for") return KW_FOR;
  2114. if (name == "goto") return KW_GOTO;
  2115. if (name == "if") return KW_IF;
  2116. if (name == "inline") return KW_INLINE;
  2117. if (name == "__inline") return KW_INLINE;
  2118. if (name == "__inline__") return KW_INLINE;
  2119. if (name == "int") return KW_INT;
  2120. if (name == "long") return KW_LONG;
  2121. if (name == "__make_map_property") return KW_MAKE_MAP_PROPERTY;
  2122. if (name == "__make_property") return KW_MAKE_PROPERTY;
  2123. if (name == "__make_property2") return KW_MAKE_PROPERTY2;
  2124. if (name == "__make_seq") return KW_MAKE_SEQ;
  2125. if (name == "__make_seq_property") return KW_MAKE_SEQ_PROPERTY;
  2126. if (name == "mutable") return KW_MUTABLE;
  2127. if (name == "namespace") return KW_NAMESPACE;
  2128. if (name == "noexcept") return KW_NOEXCEPT;
  2129. if (name == "nullptr") return KW_NULLPTR;
  2130. if (name == "new") return KW_NEW;
  2131. if (name == "operator") return KW_OPERATOR;
  2132. if (name == "override") return KW_OVERRIDE;
  2133. if (name == "private") return KW_PRIVATE;
  2134. if (name == "protected") return KW_PROTECTED;
  2135. if (name == "public") return KW_PUBLIC;
  2136. if (name == "register") return KW_REGISTER;
  2137. if (name == "reinterpret_cast") return KW_REINTERPRET_CAST;
  2138. if (name == "return") return KW_RETURN;
  2139. if (name == "short") return KW_SHORT;
  2140. if (name == "signed") return KW_SIGNED;
  2141. if (name == "sizeof") return KW_SIZEOF;
  2142. if (name == "static") return KW_STATIC;
  2143. if (name == "static_assert") return KW_STATIC_ASSERT;
  2144. if (name == "static_cast") return KW_STATIC_CAST;
  2145. if (name == "struct") return KW_STRUCT;
  2146. if (name == "template") return KW_TEMPLATE;
  2147. if (name == "thread_local") return KW_THREAD_LOCAL;
  2148. if (name == "throw") return KW_THROW;
  2149. if (name == "true") return KW_TRUE;
  2150. if (name == "try") return KW_TRY;
  2151. if (name == "typedef") return KW_TYPEDEF;
  2152. if (name == "typeid") return KW_TYPEID;
  2153. if (name == "typename") return KW_TYPENAME;
  2154. if (name == "union") return KW_UNION;
  2155. if (name == "unsigned") return KW_UNSIGNED;
  2156. if (name == "using") return KW_USING;
  2157. if (name == "virtual") return KW_VIRTUAL;
  2158. if (name == "void") return KW_VOID;
  2159. if (name == "volatile") return KW_VOLATILE;
  2160. if (name == "wchar_t") return KW_WCHAR_T;
  2161. if (name == "while") return KW_WHILE;
  2162. // These are alternative ways to refer to built-in operators.
  2163. if (name == "and") return ANDAND;
  2164. if (name == "and_eq") return ANDEQUAL;
  2165. if (name == "bitand") return '&';
  2166. if (name == "bitor") return '|';
  2167. if (name == "compl") return '~';
  2168. if (name == "not") return '!';
  2169. if (name == "not_eq") return NECOMPARE;
  2170. if (name == "or") return OROR;
  2171. if (name == "or_eq") return OREQUAL;
  2172. if (name == "xor") return '^';
  2173. if (name == "xor_eq") return XOREQUAL;
  2174. return 0;
  2175. }
  2176. /**
  2177. *
  2178. */
  2179. int CPPPreprocessor::
  2180. scan_escape_sequence(int c) {
  2181. if (c != '\\') {
  2182. return c;
  2183. }
  2184. c = get();
  2185. switch (c) {
  2186. case 'a':
  2187. return '\a';
  2188. case 'b':
  2189. return '\b';
  2190. case 'f':
  2191. return '\f';
  2192. case 'n':
  2193. return '\n';
  2194. case 'r':
  2195. return '\r';
  2196. case 't':
  2197. return '\t';
  2198. case 'v':
  2199. return '\v';
  2200. case 'e':
  2201. // \e is non-standard, but GCC supports it.
  2202. return '\x1B';
  2203. case 'x':
  2204. // hex character.
  2205. c = get();
  2206. if (isxdigit(c)) {
  2207. int val = hex_val(c);
  2208. if (isxdigit(peek())) {
  2209. val = (val << 4) | hex_val(get());
  2210. }
  2211. return val;
  2212. }
  2213. break;
  2214. case '0':
  2215. case '1':
  2216. case '2':
  2217. case '3':
  2218. case '4':
  2219. case '5':
  2220. case '6':
  2221. case '7':
  2222. // Octal character.
  2223. {
  2224. int val = (c - '0');
  2225. c = peek();
  2226. if (c >= '0' && c <= '7') {
  2227. val = (val << 3) | (get() - '0');
  2228. c = peek();
  2229. if (c >= '0' && c <= '7') {
  2230. val = (val << 3) | (get() - '0');
  2231. }
  2232. }
  2233. return val;
  2234. }
  2235. }
  2236. // Simply output the following character.
  2237. return c;
  2238. }
  2239. /**
  2240. *
  2241. */
  2242. string CPPPreprocessor::
  2243. scan_quoted(int c) {
  2244. int quote_mark = c;
  2245. string str;
  2246. c = get();
  2247. while (c != EOF && c != '\n' && c != quote_mark) {
  2248. if (c == '\\') {
  2249. // Backslash means a special character follows.
  2250. c = scan_escape_sequence(c);
  2251. }
  2252. str += c;
  2253. c = get();
  2254. }
  2255. if (c != quote_mark) {
  2256. warning("Unclosed string");
  2257. }
  2258. return str;
  2259. }
  2260. /**
  2261. * Returns true if the manifest is one that is being ignored right now
  2262. * (presumably because we are presently expanding it).
  2263. */
  2264. bool CPPPreprocessor::
  2265. should_ignore_manifest(const CPPManifest *manifest) const {
  2266. Files::const_iterator fi;
  2267. for (fi = _files.begin(); fi != _files.end(); ++fi) {
  2268. if ((*fi)._ignore_manifest == manifest) {
  2269. return true;
  2270. }
  2271. }
  2272. return false;
  2273. }
  2274. /**
  2275. * Returns true if we should ignore any preprocessor directives (e.g. we're
  2276. * presently expanding a manifest).
  2277. */
  2278. bool CPPPreprocessor::
  2279. should_ignore_preprocessor() const {
  2280. Files::const_iterator fi;
  2281. for (fi = _files.begin(); fi != _files.end(); ++fi) {
  2282. if ((*fi)._ignore_manifest != NULL) {
  2283. return true;
  2284. }
  2285. }
  2286. return false;
  2287. }
  2288. /**
  2289. *
  2290. */
  2291. int CPPPreprocessor::
  2292. get() {
  2293. if (_unget != '\0') {
  2294. int c = _unget;
  2295. _unget = '\0';
  2296. return c;
  2297. }
  2298. if (_files.empty()) {
  2299. return EOF;
  2300. }
  2301. int c = _files.back().get();
  2302. while (c == EOF && !_files.empty()) {
  2303. #ifdef CPP_VERBOSE_LEX
  2304. indent(cerr, _files.size() * 2)
  2305. << "End of input stream, restoring to previous input\n";
  2306. #endif
  2307. _files.pop_back();
  2308. // Synthesize a newline, just in case the file doesn't already end with
  2309. // one.
  2310. c = '\n';
  2311. }
  2312. if (c == '\n') {
  2313. _start_of_line = true;
  2314. } else if (!isspace(c) && c != '#') {
  2315. _start_of_line = false;
  2316. }
  2317. return c;
  2318. }
  2319. /**
  2320. * Like get(), but does not alter the current state.
  2321. */
  2322. int CPPPreprocessor::
  2323. peek() {
  2324. if (_unget != '\0') {
  2325. return _unget;
  2326. }
  2327. if (_files.empty()) {
  2328. return EOF;
  2329. }
  2330. Files::reverse_iterator it = _files.rbegin();
  2331. int c = (*it).peek();
  2332. while (c == EOF && it != _files.rend()) {
  2333. int last_c = (*it)._prev_last_c;
  2334. ++it;
  2335. if (last_c != '\0') {
  2336. c = last_c;
  2337. } else if (it != _files.rend()) {
  2338. c = (*it).peek();
  2339. }
  2340. }
  2341. return c;
  2342. }
  2343. /**
  2344. * Undoes the effects of a previous get(). Not recommended, use peek()
  2345. * instead where possible, as it doesn't cause the column index to be off.
  2346. */
  2347. void CPPPreprocessor::
  2348. unget(int c) {
  2349. assert(_unget == '\0');
  2350. _unget = c;
  2351. }
  2352. /**
  2353. * Recursively invokes yacc to parse the stuff within angle brackets that's
  2354. * the template instantiation part of an identifier. This involves setting
  2355. * and restoring some state flags so we can return EOF when we reach the
  2356. * closing bracket.
  2357. */
  2358. CPPTemplateParameterList *CPPPreprocessor::
  2359. nested_parse_template_instantiation(CPPTemplateScope *scope) {
  2360. #ifdef CPP_VERBOSE_LEX
  2361. indent(cerr, _files.size() * 2)
  2362. << "Beginning nested parse\n";
  2363. #endif
  2364. assert(scope != NULL);
  2365. State old_state = _state;
  2366. int old_nesting = _paren_nesting;
  2367. bool old_parsing_params = _parsing_template_params;
  2368. const CPPTemplateParameterList &formal_params = scope->_parameters;
  2369. CPPTemplateParameterList::Parameters::const_iterator pi;
  2370. _state = S_nested;
  2371. _paren_nesting = 0;
  2372. _parsing_template_params = true;
  2373. CPPToken token = internal_get_next_token();
  2374. if (token._token == '>') {
  2375. _parsing_template_params = false;
  2376. } else {
  2377. _saved_tokens.push_back(token);
  2378. }
  2379. CPPTemplateParameterList *actual_params = new CPPTemplateParameterList;
  2380. for (pi = formal_params._parameters.begin();
  2381. pi != formal_params._parameters.end() && _parsing_template_params;
  2382. ++pi) {
  2383. CPPToken token = peek_next_token();
  2384. YYLTYPE loc = token._lloc;
  2385. CPPDeclaration *decl = (*pi);
  2386. if (decl->as_type()) {
  2387. // Parse a typename template parameter.
  2388. _saved_tokens.push_back(CPPToken(START_TYPE));
  2389. CPPType *type = ::parse_type(this, current_scope, global_scope);
  2390. if (type == NULL) {
  2391. loc.last_line = get_line_number();
  2392. loc.last_column = get_col_number() - 1;
  2393. warning("Invalid type", loc);
  2394. skip_to_end_nested();
  2395. type = CPPType::new_type(new CPPSimpleType(CPPSimpleType::T_unknown));
  2396. }
  2397. actual_params->_parameters.push_back(type);
  2398. } else {
  2399. // Parse a constant expression template parameter.
  2400. _saved_tokens.push_back(CPPToken(START_CONST_EXPR));
  2401. CPPExpression *expr = parse_const_expr(this, current_scope, global_scope);
  2402. if (expr == NULL) {
  2403. loc.last_line = get_line_number();
  2404. loc.last_column = get_col_number() - 1;
  2405. warning("Invalid expression", loc);
  2406. skip_to_end_nested();
  2407. expr = new CPPExpression(0);
  2408. }
  2409. actual_params->_parameters.push_back(expr);
  2410. }
  2411. _state = S_nested;
  2412. _paren_nesting = 0;
  2413. }
  2414. if (_parsing_template_params) {
  2415. warning("Ignoring extra parameters in template instantiation");
  2416. skip_to_angle_bracket();
  2417. }
  2418. _state = old_state;
  2419. _paren_nesting = old_nesting;
  2420. _parsing_template_params = old_parsing_params;
  2421. #ifdef CPP_VERBOSE_LEX
  2422. indent(cerr, _files.size() * 2)
  2423. << "Ending nested parse\n";
  2424. #endif
  2425. return actual_params;
  2426. }
  2427. /**
  2428. * This is an error-recovery function, called after returning from a nested
  2429. * parse. If the state is not S_end_nested, there was an error in parsing the
  2430. * nested tokens, and not all of the nested tokens may have been consumed.
  2431. * This function will consume the rest of the nested tokens.
  2432. */
  2433. void CPPPreprocessor::
  2434. skip_to_end_nested() {
  2435. #ifdef CPP_VERBOSE_LEX
  2436. indent(cerr, _files.size() * 2)
  2437. << "Skipping tokens:\n";
  2438. #endif
  2439. // Eat any eof tokens on the pushback stack.
  2440. while (!_saved_tokens.empty() && _saved_tokens.back().is_eof()) {
  2441. _saved_tokens.pop_back();
  2442. }
  2443. while (_state != S_end_nested && _state != S_eof) {
  2444. get_next_token();
  2445. }
  2446. #ifdef CPP_VERBOSE_LEX
  2447. indent(cerr, _files.size() * 2)
  2448. << "Done skipping tokens.\n";
  2449. #endif
  2450. }
  2451. /**
  2452. * This is an error-recovery function, called after returning from a nested
  2453. * parse. If we haven't yet consumed the closing angle bracket on the
  2454. * template instantiation, keep consuming tokens until we do.
  2455. */
  2456. void CPPPreprocessor::
  2457. skip_to_angle_bracket() {
  2458. #ifdef CPP_VERBOSE_LEX
  2459. indent(cerr, _files.size() * 2)
  2460. << "Skipping tokens:\n";
  2461. #endif
  2462. while (_parsing_template_params && _state != S_eof) {
  2463. _state = S_nested;
  2464. while (_state != S_end_nested && _state != S_eof) {
  2465. get_next_token();
  2466. }
  2467. }
  2468. // Eat any eof tokens on the pushback stack.
  2469. while (!_saved_tokens.empty() && _saved_tokens.back().is_eof()) {
  2470. _saved_tokens.pop_back();
  2471. }
  2472. #ifdef CPP_VERBOSE_LEX
  2473. indent(cerr, _files.size() * 2)
  2474. << "Done skipping tokens.\n";
  2475. #endif
  2476. }