cmFortranParserImpl.cxx 10 KB

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