cmFortranParserImpl.cxx 10 KB

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