cmFortranParserImpl.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmFortranParser.h"
  4. #include "cmFortranLexer.h"
  5. #include "cmSystemTools.h"
  6. #include <assert.h>
  7. #include <cmConfigure.h>
  8. #include <set>
  9. #include <stack>
  10. #include <stdio.h>
  11. #include <string>
  12. #include <vector>
  13. bool cmFortranParser_s::FindIncludeFile(const char* dir,
  14. const char* includeName,
  15. std::string& fileName)
  16. {
  17. // If the file is a full path, include it directly.
  18. if (cmSystemTools::FileIsFullPath(includeName)) {
  19. fileName = includeName;
  20. return cmSystemTools::FileExists(fileName.c_str(), true);
  21. }
  22. // Check for the file in the directory containing the including
  23. // file.
  24. std::string fullName = dir;
  25. fullName += "/";
  26. fullName += includeName;
  27. if (cmSystemTools::FileExists(fullName.c_str(), true)) {
  28. fileName = fullName;
  29. return true;
  30. }
  31. // Search the include path for the file.
  32. for (std::vector<std::string>::const_iterator i = this->IncludePath.begin();
  33. i != this->IncludePath.end(); ++i) {
  34. fullName = *i;
  35. fullName += "/";
  36. fullName += includeName;
  37. if (cmSystemTools::FileExists(fullName.c_str(), true)) {
  38. fileName = fullName;
  39. return true;
  40. }
  41. }
  42. return false;
  43. }
  44. cmFortranParser_s::cmFortranParser_s(std::vector<std::string> const& includes,
  45. std::set<std::string> const& defines,
  46. cmFortranSourceInfo& info)
  47. : IncludePath(includes)
  48. , PPDefinitions(defines)
  49. , Info(info)
  50. {
  51. this->InInterface = false;
  52. this->InPPFalseBranch = 0;
  53. // Initialize the lexical scanner.
  54. cmFortran_yylex_init(&this->Scanner);
  55. cmFortran_yyset_extra(this, this->Scanner);
  56. // Create a dummy buffer that is never read but is the fallback
  57. // buffer when the last file is popped off the stack.
  58. YY_BUFFER_STATE buffer =
  59. cmFortran_yy_create_buffer(CM_NULLPTR, 4, this->Scanner);
  60. cmFortran_yy_switch_to_buffer(buffer, this->Scanner);
  61. }
  62. cmFortranParser_s::~cmFortranParser_s()
  63. {
  64. cmFortran_yylex_destroy(this->Scanner);
  65. }
  66. bool cmFortranParser_FilePush(cmFortranParser* parser, const char* fname)
  67. {
  68. // Open the new file and push it onto the stack. Save the old
  69. // buffer with it on the stack.
  70. if (FILE* file = cmsys::SystemTools::Fopen(fname, "rb")) {
  71. YY_BUFFER_STATE current = cmFortranLexer_GetCurrentBuffer(parser->Scanner);
  72. std::string dir = cmSystemTools::GetParentDirectory(fname);
  73. cmFortranFile f(file, current, dir);
  74. YY_BUFFER_STATE buffer =
  75. cmFortran_yy_create_buffer(CM_NULLPTR, 16384, parser->Scanner);
  76. cmFortran_yy_switch_to_buffer(buffer, parser->Scanner);
  77. parser->FileStack.push(f);
  78. return true;
  79. }
  80. return false;
  81. }
  82. bool cmFortranParser_FilePop(cmFortranParser* parser)
  83. {
  84. // Pop one file off the stack and close it. Switch the lexer back
  85. // to the next one on the stack.
  86. if (parser->FileStack.empty()) {
  87. return false;
  88. }
  89. cmFortranFile f = parser->FileStack.top();
  90. parser->FileStack.pop();
  91. fclose(f.File);
  92. YY_BUFFER_STATE current = cmFortranLexer_GetCurrentBuffer(parser->Scanner);
  93. cmFortran_yy_delete_buffer(current, parser->Scanner);
  94. cmFortran_yy_switch_to_buffer(f.Buffer, parser->Scanner);
  95. return true;
  96. }
  97. int cmFortranParser_Input(cmFortranParser* parser, char* buffer,
  98. size_t bufferSize)
  99. {
  100. // Read from the file on top of the stack. If the stack is empty,
  101. // the end of the translation unit has been reached.
  102. if (!parser->FileStack.empty()) {
  103. cmFortranFile& ff = parser->FileStack.top();
  104. FILE* file = ff.File;
  105. size_t n = fread(buffer, 1, bufferSize, file);
  106. if (n > 0) {
  107. ff.LastCharWasNewline = buffer[n - 1] == '\n';
  108. } else if (!ff.LastCharWasNewline) {
  109. // The file ended without a newline. Inject one so
  110. // that the file always ends in an end-of-statement.
  111. buffer[0] = '\n';
  112. n = 1;
  113. ff.LastCharWasNewline = true;
  114. }
  115. return (int)n;
  116. }
  117. return 0;
  118. }
  119. void cmFortranParser_StringStart(cmFortranParser* parser)
  120. {
  121. parser->TokenString = "";
  122. }
  123. const char* cmFortranParser_StringEnd(cmFortranParser* parser)
  124. {
  125. return parser->TokenString.c_str();
  126. }
  127. void cmFortranParser_StringAppend(cmFortranParser* parser, char c)
  128. {
  129. parser->TokenString += c;
  130. }
  131. void cmFortranParser_SetInInterface(cmFortranParser* parser, bool in)
  132. {
  133. if (parser->InPPFalseBranch) {
  134. return;
  135. }
  136. parser->InInterface = in;
  137. }
  138. bool cmFortranParser_GetInInterface(cmFortranParser* parser)
  139. {
  140. return parser->InInterface;
  141. }
  142. void cmFortranParser_SetOldStartcond(cmFortranParser* parser, int arg)
  143. {
  144. parser->OldStartcond = arg;
  145. }
  146. int cmFortranParser_GetOldStartcond(cmFortranParser* parser)
  147. {
  148. return parser->OldStartcond;
  149. }
  150. void cmFortranParser_Error(cmFortranParser* parser, const char* msg)
  151. {
  152. parser->Error = msg ? msg : "unknown error";
  153. }
  154. void cmFortranParser_RuleUse(cmFortranParser* parser, const char* name)
  155. {
  156. if (!parser->InPPFalseBranch) {
  157. parser->Info.Requires.insert(cmSystemTools::LowerCase(name));
  158. }
  159. }
  160. void cmFortranParser_RuleLineDirective(cmFortranParser* parser,
  161. const char* filename)
  162. {
  163. // This is a #line directive naming a file encountered during preprocessing.
  164. std::string included = filename;
  165. // Skip #line directives referencing non-files like
  166. // "<built-in>" or "<command-line>".
  167. if (included.empty() || included[0] == '<') {
  168. return;
  169. }
  170. // Fix windows file path separators since our lexer does not
  171. // process escape sequences in string literals.
  172. cmSystemTools::ReplaceString(included, "\\\\", "\\");
  173. cmSystemTools::ConvertToUnixSlashes(included);
  174. // Save the named file as included in the source.
  175. if (cmSystemTools::FileExists(included, true)) {
  176. parser->Info.Includes.insert(included);
  177. }
  178. }
  179. void cmFortranParser_RuleInclude(cmFortranParser* parser, const char* name)
  180. {
  181. if (parser->InPPFalseBranch) {
  182. return;
  183. }
  184. // If processing an include statement there must be an open file.
  185. assert(!parser->FileStack.empty());
  186. // Get the directory containing the source in which the include
  187. // statement appears. This is always the first search location for
  188. // Fortran include files.
  189. std::string dir = parser->FileStack.top().Directory;
  190. // Find the included file. If it cannot be found just ignore the
  191. // problem because either the source will not compile or the user
  192. // does not care about depending on this included source.
  193. std::string fullName;
  194. if (parser->FindIncludeFile(dir.c_str(), name, fullName)) {
  195. // Found the included file. Save it in the set of included files.
  196. parser->Info.Includes.insert(fullName);
  197. // Parse it immediately to translate the source inline.
  198. cmFortranParser_FilePush(parser, fullName.c_str());
  199. }
  200. }
  201. void cmFortranParser_RuleModule(cmFortranParser* parser, const char* name)
  202. {
  203. if (!parser->InPPFalseBranch && !parser->InInterface) {
  204. parser->Info.Provides.insert(cmSystemTools::LowerCase(name));
  205. }
  206. }
  207. void cmFortranParser_RuleDefine(cmFortranParser* parser, const char* macro)
  208. {
  209. if (!parser->InPPFalseBranch) {
  210. parser->PPDefinitions.insert(macro);
  211. }
  212. }
  213. void cmFortranParser_RuleUndef(cmFortranParser* parser, const char* macro)
  214. {
  215. if (!parser->InPPFalseBranch) {
  216. std::set<std::string>::iterator match;
  217. match = parser->PPDefinitions.find(macro);
  218. if (match != parser->PPDefinitions.end()) {
  219. parser->PPDefinitions.erase(match);
  220. }
  221. }
  222. }
  223. void cmFortranParser_RuleIfdef(cmFortranParser* parser, const char* macro)
  224. {
  225. // A new PP branch has been opened
  226. parser->SkipToEnd.push(false);
  227. if (parser->InPPFalseBranch) {
  228. parser->InPPFalseBranch++;
  229. } else if (parser->PPDefinitions.find(macro) ==
  230. parser->PPDefinitions.end()) {
  231. parser->InPPFalseBranch = 1;
  232. } else {
  233. parser->SkipToEnd.top() = true;
  234. }
  235. }
  236. void cmFortranParser_RuleIfndef(cmFortranParser* parser, const char* macro)
  237. {
  238. // A new PP branch has been opened
  239. parser->SkipToEnd.push(false);
  240. if (parser->InPPFalseBranch) {
  241. parser->InPPFalseBranch++;
  242. } else if (parser->PPDefinitions.find(macro) !=
  243. parser->PPDefinitions.end()) {
  244. parser->InPPFalseBranch = 1;
  245. } else {
  246. // ignore other branches
  247. parser->SkipToEnd.top() = true;
  248. }
  249. }
  250. void cmFortranParser_RuleIf(cmFortranParser* parser)
  251. {
  252. /* Note: The current parser is _not_ able to get statements like
  253. * #if 0
  254. * #if 1
  255. * #if MYSMBOL
  256. * #if defined(MYSYMBOL)
  257. * #if defined(MYSYMBOL) && ...
  258. * right. The same for #elif. Thus in
  259. * #if SYMBOL_1
  260. * ..
  261. * #elif SYMBOL_2
  262. * ...
  263. * ...
  264. * #elif SYMBOL_N
  265. * ..
  266. * #else
  267. * ..
  268. * #endif
  269. * _all_ N+1 branches are considered. If you got something like this
  270. * #if defined(MYSYMBOL)
  271. * #if !defined(MYSYMBOL)
  272. * use
  273. * #ifdef MYSYMBOL
  274. * #ifndef MYSYMBOL
  275. * instead.
  276. */
  277. // A new PP branch has been opened
  278. // Never skip! See note above.
  279. parser->SkipToEnd.push(false);
  280. }
  281. void cmFortranParser_RuleElif(cmFortranParser* parser)
  282. {
  283. /* Note: There are parser limitations. See the note at
  284. * cmFortranParser_RuleIf(..)
  285. */
  286. // Always taken unless an #ifdef or #ifndef-branch has been taken
  287. // already. If the second condition isn't meet already
  288. // (parser->InPPFalseBranch == 0) correct it.
  289. if (!parser->SkipToEnd.empty() && parser->SkipToEnd.top() &&
  290. !parser->InPPFalseBranch) {
  291. parser->InPPFalseBranch = 1;
  292. }
  293. }
  294. void cmFortranParser_RuleElse(cmFortranParser* parser)
  295. {
  296. // if the parent branch is false do nothing!
  297. if (parser->InPPFalseBranch > 1) {
  298. return;
  299. }
  300. // parser->InPPFalseBranch is either 0 or 1. We change it depending on
  301. // parser->SkipToEnd.top()
  302. if (!parser->SkipToEnd.empty() && parser->SkipToEnd.top()) {
  303. parser->InPPFalseBranch = 1;
  304. } else {
  305. parser->InPPFalseBranch = 0;
  306. }
  307. }
  308. void cmFortranParser_RuleEndif(cmFortranParser* parser)
  309. {
  310. if (!parser->SkipToEnd.empty()) {
  311. parser->SkipToEnd.pop();
  312. }
  313. // #endif doesn't know if there was a "#else" in before, so it
  314. // always decreases InPPFalseBranch
  315. if (parser->InPPFalseBranch) {
  316. parser->InPPFalseBranch--;
  317. }
  318. }