cmFortranParserImpl.cxx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. }
  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 = 0;
  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 1;
  79. }
  80. return 0;
  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 0;
  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 1;
  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. FILE* file = parser->FileStack.top().File;
  104. return (int)fread(buffer, 1, bufferSize, file);
  105. }
  106. return 0;
  107. }
  108. void cmFortranParser_StringStart(cmFortranParser* parser)
  109. {
  110. parser->TokenString = "";
  111. }
  112. const char* cmFortranParser_StringEnd(cmFortranParser* parser)
  113. {
  114. return parser->TokenString.c_str();
  115. }
  116. void cmFortranParser_StringAppend(cmFortranParser* parser, char c)
  117. {
  118. parser->TokenString += c;
  119. }
  120. void cmFortranParser_SetInInterface(cmFortranParser* parser, bool in)
  121. {
  122. if (parser->InPPFalseBranch) {
  123. return;
  124. }
  125. parser->InInterface = in;
  126. }
  127. bool cmFortranParser_GetInInterface(cmFortranParser* parser)
  128. {
  129. return parser->InInterface;
  130. }
  131. void cmFortranParser_SetOldStartcond(cmFortranParser* parser, int arg)
  132. {
  133. parser->OldStartcond = arg;
  134. }
  135. int cmFortranParser_GetOldStartcond(cmFortranParser* parser)
  136. {
  137. return parser->OldStartcond;
  138. }
  139. void cmFortranParser_Error(cmFortranParser* /*unused*/, const char* /*unused*/)
  140. {
  141. // If there is a parser error just ignore it. The source will not
  142. // compile and the user will edit it. Then dependencies will have
  143. // to be regenerated anyway.
  144. }
  145. void cmFortranParser_RuleUse(cmFortranParser* parser, const char* name)
  146. {
  147. if (!parser->InPPFalseBranch) {
  148. parser->Info.Requires.insert(cmSystemTools::LowerCase(name));
  149. }
  150. }
  151. void cmFortranParser_RuleLineDirective(cmFortranParser* parser,
  152. const char* filename)
  153. {
  154. // This is a #line directive naming a file encountered during preprocessing.
  155. std::string included = filename;
  156. // Skip #line directives referencing non-files like
  157. // "<built-in>" or "<command-line>".
  158. if (included.empty() || included[0] == '<') {
  159. return;
  160. }
  161. // Fix windows file path separators since our lexer does not
  162. // process escape sequences in string literals.
  163. cmSystemTools::ReplaceString(included, "\\\\", "\\");
  164. cmSystemTools::ConvertToUnixSlashes(included);
  165. // Save the named file as included in the source.
  166. if (cmSystemTools::FileExists(included, true)) {
  167. parser->Info.Includes.insert(included);
  168. }
  169. }
  170. void cmFortranParser_RuleInclude(cmFortranParser* parser, const char* name)
  171. {
  172. if (parser->InPPFalseBranch) {
  173. return;
  174. }
  175. // If processing an include statement there must be an open file.
  176. assert(!parser->FileStack.empty());
  177. // Get the directory containing the source in which the include
  178. // statement appears. This is always the first search location for
  179. // Fortran include files.
  180. std::string dir = parser->FileStack.top().Directory;
  181. // Find the included file. If it cannot be found just ignore the
  182. // problem because either the source will not compile or the user
  183. // does not care about depending on this included source.
  184. std::string fullName;
  185. if (parser->FindIncludeFile(dir.c_str(), name, fullName)) {
  186. // Found the included file. Save it in the set of included files.
  187. parser->Info.Includes.insert(fullName);
  188. // Parse it immediately to translate the source inline.
  189. cmFortranParser_FilePush(parser, fullName.c_str());
  190. }
  191. }
  192. void cmFortranParser_RuleModule(cmFortranParser* parser, const char* name)
  193. {
  194. if (!parser->InPPFalseBranch && !parser->InInterface) {
  195. parser->Info.Provides.insert(cmSystemTools::LowerCase(name));
  196. }
  197. }
  198. void cmFortranParser_RuleDefine(cmFortranParser* parser, const char* macro)
  199. {
  200. if (!parser->InPPFalseBranch) {
  201. parser->PPDefinitions.insert(macro);
  202. }
  203. }
  204. void cmFortranParser_RuleUndef(cmFortranParser* parser, const char* macro)
  205. {
  206. if (!parser->InPPFalseBranch) {
  207. std::set<std::string>::iterator match;
  208. match = parser->PPDefinitions.find(macro);
  209. if (match != parser->PPDefinitions.end()) {
  210. parser->PPDefinitions.erase(match);
  211. }
  212. }
  213. }
  214. void cmFortranParser_RuleIfdef(cmFortranParser* parser, const char* macro)
  215. {
  216. // A new PP branch has been opened
  217. parser->SkipToEnd.push(false);
  218. if (parser->InPPFalseBranch) {
  219. parser->InPPFalseBranch++;
  220. } else if (parser->PPDefinitions.find(macro) ==
  221. parser->PPDefinitions.end()) {
  222. parser->InPPFalseBranch = 1;
  223. } else {
  224. parser->SkipToEnd.top() = true;
  225. }
  226. }
  227. void cmFortranParser_RuleIfndef(cmFortranParser* parser, const char* macro)
  228. {
  229. // A new PP branch has been opened
  230. parser->SkipToEnd.push(false);
  231. if (parser->InPPFalseBranch) {
  232. parser->InPPFalseBranch++;
  233. } else if (parser->PPDefinitions.find(macro) !=
  234. parser->PPDefinitions.end()) {
  235. parser->InPPFalseBranch = 1;
  236. } else {
  237. // ignore other branches
  238. parser->SkipToEnd.top() = true;
  239. }
  240. }
  241. void cmFortranParser_RuleIf(cmFortranParser* parser)
  242. {
  243. /* Note: The current parser is _not_ able to get statements like
  244. * #if 0
  245. * #if 1
  246. * #if MYSMBOL
  247. * #if defined(MYSYMBOL)
  248. * #if defined(MYSYMBOL) && ...
  249. * right. The same for #elif. Thus in
  250. * #if SYMBOL_1
  251. * ..
  252. * #elif SYMBOL_2
  253. * ...
  254. * ...
  255. * #elif SYMBOL_N
  256. * ..
  257. * #else
  258. * ..
  259. * #endif
  260. * _all_ N+1 branches are considered. If you got something like this
  261. * #if defined(MYSYMBOL)
  262. * #if !defined(MYSYMBOL)
  263. * use
  264. * #ifdef MYSYMBOL
  265. * #ifndef MYSYMBOL
  266. * instead.
  267. */
  268. // A new PP branch has been opened
  269. // Never skip! See note above.
  270. parser->SkipToEnd.push(false);
  271. }
  272. void cmFortranParser_RuleElif(cmFortranParser* parser)
  273. {
  274. /* Note: There are parser limitations. See the note at
  275. * cmFortranParser_RuleIf(..)
  276. */
  277. // Always taken unless an #ifdef or #ifndef-branch has been taken
  278. // already. If the second condition isn't meet already
  279. // (parser->InPPFalseBranch == 0) correct it.
  280. if (!parser->SkipToEnd.empty() && parser->SkipToEnd.top() &&
  281. !parser->InPPFalseBranch) {
  282. parser->InPPFalseBranch = 1;
  283. }
  284. }
  285. void cmFortranParser_RuleElse(cmFortranParser* parser)
  286. {
  287. // if the parent branch is false do nothing!
  288. if (parser->InPPFalseBranch > 1) {
  289. return;
  290. }
  291. // parser->InPPFalseBranch is either 0 or 1. We change it depending on
  292. // parser->SkipToEnd.top()
  293. if (!parser->SkipToEnd.empty() && parser->SkipToEnd.top()) {
  294. parser->InPPFalseBranch = 1;
  295. } else {
  296. parser->InPPFalseBranch = 0;
  297. }
  298. }
  299. void cmFortranParser_RuleEndif(cmFortranParser* parser)
  300. {
  301. if (!parser->SkipToEnd.empty()) {
  302. parser->SkipToEnd.pop();
  303. }
  304. // #endif doesn't know if there was a "#else" in before, so it
  305. // always decreases InPPFalseBranch
  306. if (parser->InPPFalseBranch) {
  307. parser->InPPFalseBranch--;
  308. }
  309. }