From 596d2a801dd9e5066e27afa2849852ffb321a61b Mon Sep 17 00:00:00 2001
From: sebastien <sebastien@ac1d8469-bf42-47a9-8791-bf33cf982152>
Date: Thu, 12 Feb 2009 13:38:01 +0000
Subject: [PATCH] trunk preprocessor: fixed a memory management bug related to
 the filename being parsed

git-svn-id: https://www.dynare.org/svn/dynare/trunk@2412 ac1d8469-bf42-47a9-8791-bf33cf982152
---
 preprocessor/DynareBison.yy           | 4 ++--
 preprocessor/DynareFlex.ll            | 4 +---
 preprocessor/ParsingDriver.cc         | 6 ------
 preprocessor/include/ParsingDriver.hh | 4 ++++
 4 files changed, 7 insertions(+), 11 deletions(-)

diff --git a/preprocessor/DynareBison.yy b/preprocessor/DynareBison.yy
index 9ba11294c7..be5e33a1a5 100644
--- a/preprocessor/DynareBison.yy
+++ b/preprocessor/DynareBison.yy
@@ -55,8 +55,8 @@ class ParsingDriver;
 %locations
 %initial-action
 {
-  // Initialize the location filenames to NULL
-  @$.begin.filename = @$.end.filename = NULL;
+  // Initialize the locations' filenames to the filename maintained by the lexer
+  @$.begin.filename = @$.end.filename = &(driver.lexer->filename);
 }
 
 %debug
diff --git a/preprocessor/DynareFlex.ll b/preprocessor/DynareFlex.ll
index ec2b276930..8924d79661 100644
--- a/preprocessor/DynareFlex.ll
+++ b/preprocessor/DynareFlex.ll
@@ -73,9 +73,7 @@ int sigma_e = 0;
  /* Rules for matching $line directives */
 <*>^@#line\ \"  { line_caller = YYSTATE; BEGIN(LINE1); }
 <LINE1>[^\"]*   {
-                  if (yylloc->begin.filename)
-                    delete yylloc->begin.filename;
-                  yylloc->begin.filename = yylloc->end.filename = new string(yytext);
+                  filename = string(yytext);
                   BEGIN(LINE2);
                 }
 <LINE2>\"       BEGIN(LINE3);
diff --git a/preprocessor/ParsingDriver.cc b/preprocessor/ParsingDriver.cc
index d9381a7763..06b87379a4 100644
--- a/preprocessor/ParsingDriver.cc
+++ b/preprocessor/ParsingDriver.cc
@@ -72,12 +72,6 @@ ParsingDriver::parse(istream &in, bool debug)
 
   delete lexer;
 
-  /* Deleting filename in DynareFlex::yyterminate() is prematurate,
-     because if there is an unexpected end of file, the call to
-     ParsingDriver::error() needs filename */
-  /*if (location.begin.filename)
-    delete location.begin.filename;*/
-
   return mod_file;
 }
 
diff --git a/preprocessor/include/ParsingDriver.hh b/preprocessor/include/ParsingDriver.hh
index 97f790c450..a752a86711 100644
--- a/preprocessor/include/ParsingDriver.hh
+++ b/preprocessor/include/ParsingDriver.hh
@@ -60,6 +60,10 @@ public:
   Dynare::parser::token_type lex(Dynare::parser::semantic_type *yylval,
                                  Dynare::parser::location_type *yylloc,
                                  ParsingDriver &driver);
+
+  //! The filename being parsed
+  /*! The bison parser locations (begin and end) contain a pointer to that string */
+  string filename;
 };
 
 //! Drives the scanning and parsing of the .mod file, and constructs its abstract representation
-- 
GitLab