Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 43 additions & 31 deletions src/verilog/verilog_preprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ Author: Daniel Kroening, kroening@kroening.com

\*******************************************************************/

#include <fstream>
#include "verilog_preprocessor.h"

#include <util/config.h>

#include "verilog_preprocessor.h"
#include "verilog_preprocessor_error.h"

#include <fstream>

/*******************************************************************\

Expand Down Expand Up @@ -245,44 +247,55 @@ Function: verilog_preprocessort::preprocessor

void verilog_preprocessort::preprocessor()
{
files.emplace_back(false, &in, filename);

while(!files.empty())
try
{
files.back().print_line(out, files.size()==1?0:2);
files.emplace_back(false, &in, filename);

char ch, last_out=0;

while(files.back().get(ch))
while(!files.empty())
{
switch(ch)
{
case '`':
directive();
break;
files.back().print_line(out, files.size() == 1 ? 0 : 2);

default:
if(condition)
char ch, last_out = 0;

while(files.back().get(ch))
{
switch(ch)
{
filet &file=files.back();
case '`':
directive();
break;

if(last_out=='\n' && file.last_line!=file.line &&
ch!='\n')
default:
if(condition)
{
file.print_line(out, 0);
file.last_line=file.line;
}
filet &file = files.back();

if(last_out == '\n' && file.last_line != file.line && ch != '\n')
{
file.print_line(out, 0);
file.last_line = file.line;
}

out << ch;
last_out=ch;
out << ch;
last_out = ch;

if(ch=='\n') file.last_line++;
if(ch == '\n')
file.last_line++;
}
}
}
}

if(last_out!='\n') out << '\n';
files.pop_back();
if(last_out != '\n')
out << '\n';
files.pop_back();
}
}
catch(const verilog_preprocessor_errort &e)
{
if(!files.empty())
error().source_location = files.back().make_source_location();
error() << e.what() << eom;
throw 0;
}
}

Expand Down Expand Up @@ -591,9 +604,8 @@ void verilog_preprocessort::directive()

if(it==defines.end())
{
error().source_location = source_location;
error() << "unknown preprocessor directive \"" << text << "\"" << eom;
throw 0;
throw verilog_preprocessor_errort()
<< "unknown preprocessor directive \"" << text << "\"";
Comment on lines +607 to +608
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I accept that this satisfies RAII, but I am still wondering whether a constructor that takes a std::string wouldn't be the better way forward.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be a bit weird, e.g., in this case you would have
throw verilog_preprocessor_errort("unknown preprocessor directive \"") << text << "\"";

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"Looks" wrong.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would have used + instead of << and have passed the entire string to the constructor. But it’s just a matter of taste, no big deal.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but then you'll want numbers, tokens (which have a << operator, but no +), etc.

}

// found it! replace it!
Expand Down
41 changes: 41 additions & 0 deletions src/verilog/verilog_preprocessor_error.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*******************************************************************\

Module: Verilog Preprocessor Error Class

Author: Daniel Kroening, kroening@kroening.com

\*******************************************************************/

#ifndef VERILOG_PREPROCESSOR_ERROR_H
#define VERILOG_PREPROCESSOR_ERROR_H

#include <sstream>
#include <string>

class verilog_preprocessor_errort
{
public:
std::string what() const
{
return message.str();
}

std::ostringstream &message_ostream()
{
return message;
}

protected:
std::ostringstream message;
};

/// add to the diagnostic information in the given verilog_preprocessor_error exception
template <typename T>
verilog_preprocessor_errort
operator<<(verilog_preprocessor_errort &&e, const T &message)
{
e.message_ostream() << message;
return std::move(e);
}

#endif // VERILOG_PREPROCESSOR_ERROR_H