cmFortranParserImpl.cxx 9.9 KB

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