cmFortranParserImpl.cxx 10 KB

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