Internals: Use standard function for include guards. No functional change intended.

This commit is contained in:
Wilson Snyder 2019-12-23 19:00:17 -05:00
parent 5c59fde92e
commit 5089f997cc
7 changed files with 45 additions and 22 deletions

View File

@ -1073,8 +1073,6 @@ class EmitCImp : EmitCStmts {
}
ofp->puts("// See "+v3Global.opt.prefix()+".h for the primary calling header\n");
}
ofp->puts("\n");
return ofp;
}
@ -2536,8 +2534,7 @@ void EmitCImp::emitMTaskState() {
void EmitCImp::emitInt(AstNodeModule* modp) {
// Always have this first; gcc has short circuiting if #ifdef is first in a file
puts("#ifndef _"+modClassName(modp)+"_H_\n");
puts("#define _"+modClassName(modp)+"_H_\n");
ofp()->putsGuard();
puts("\n");
ofp()->putsIntTopInclude();
@ -2757,12 +2754,13 @@ void EmitCImp::emitInt(AstNodeModule* modp) {
}
// finish up h-file
puts("#endif // guard\n");
ofp()->putsEndGuard();
}
//----------------------------------------------------------------------
void EmitCImp::emitImp(AstNodeModule* modp) {
puts("\n");
puts("#include \""+modClassName(modp)+".h\"\n");
puts("#include \""+symClassName()+".h\"\n");
@ -2770,8 +2768,8 @@ void EmitCImp::emitImp(AstNodeModule* modp) {
puts("\n");
puts("#include \"verilated_dpi.h\"\n");
}
puts("\n");
puts("\n");
emitTextSection(AstType::atScImpHdr);
if (m_slow && splitFilenum()==0) {

View File

@ -101,8 +101,7 @@ void EmitCInlines::emitInt() {
m_ofp = &hf;
ofp()->putsHeader();
puts("#ifndef _"+topClassName()+"__Inlines_H_\n");
puts("#define _"+topClassName()+"__Inlines_H_\n");
ofp()->putsGuard();
puts("\n");
puts("#include \"verilated.h\"\n");
@ -112,7 +111,7 @@ void EmitCInlines::emitInt() {
// Placeholder - v3Global.needHInlines(true) currently not used
puts("//======================\n\n");
puts("#endif // guard\n");
ofp()->putsEndGuard();
}
//######################################################################

View File

@ -372,12 +372,10 @@ void EmitCSyms::emitSymHdr() {
puts("//\n");
puts("// Internal details; most calling programs do not need this header,\n");
puts("// unless using verilator public meta comments.\n");
puts("\n");
puts("#ifndef _"+symClassName()+"_H_\n");
puts("#define _"+symClassName()+"_H_\n");
puts("\n");
ofp()->putsGuard();
puts("\n");
ofp()->putsIntTopInclude();
if (v3Global.needHeavy()) {
puts("#include \"verilated_heavy.h\"\n");
@ -474,8 +472,8 @@ void EmitCSyms::emitSymHdr() {
}
puts("\n");
puts("} VL_ATTR_ALIGNED(64);\n");
puts("\n");
puts("#endif // guard\n");
ofp()->putsEndGuard();
}
void EmitCSyms::closeSplit() {

View File

@ -25,6 +25,7 @@
#include "V3File.h"
#include "V3Os.h"
#include "V3PreShell.h"
#include "V3String.h"
#include "V3Ast.h"
#include <cerrno>
@ -942,6 +943,17 @@ void V3OutFile::putsForceIncs() {
}
}
void V3OutCFile::putsGuard() {
UASSERT(!m_guard, "Already called putsGuard in emit file");
m_guard = true;
string var = VString::upcase(string("_") + V3Os::filenameNonDir(filename()) + "_");
for (string::iterator pos = var.begin(); pos != var.end(); ++pos) {
if (!isalnum(*pos)) *pos = '_';
}
puts("\n#ifndef " + var + "\n");
puts("#define " + var + " // guard\n");
}
//######################################################################
// VIdProtect

View File

@ -138,6 +138,7 @@ public:
V3OutFormatter(const string& filename, Language lang);
virtual ~V3OutFormatter() {}
// ACCESSORS
string filename() const { return m_filename; }
int column() const { return m_column; }
int blockIndent() const { return m_blockIndent; }
void blockIndent(int flag) { m_blockIndent = flag; }
@ -186,23 +187,28 @@ private:
};
class V3OutCFile : public V3OutFile {
int m_private;
int m_guard; // Created header guard
int m_private; // 1 = Most recently emitted private:, 2 = public:
public:
explicit V3OutCFile(const string& filename) : V3OutFile(filename, V3OutFormatter::LA_C) {
explicit V3OutCFile(const string& filename)
: V3OutFile(filename, V3OutFormatter::LA_C)
, m_guard(false) {
resetPrivate();
}
virtual ~V3OutCFile() {}
virtual void putsHeader() { puts("// Verilated -*- C++ -*-\n"); }
virtual void putsIntTopInclude() {
putsForceIncs();
virtual void putsIntTopInclude() { putsForceIncs(); }
virtual void putsGuard();
virtual void putsEndGuard() {
if (m_guard) puts("\n#endif // guard\n");
}
// Print out public/privates
void resetPrivate() { m_private = 0; }
void putsPrivate(bool setPrivate) {
if (setPrivate && m_private!=1) {
if (setPrivate && m_private != 1) {
puts("private:\n");
m_private = 1;
} else if (!setPrivate && m_private!=2) {
} else if (!setPrivate && m_private != 2) {
puts("public:\n");
m_private = 2;
}

View File

@ -88,6 +88,14 @@ string VString::downcase(const string& str) {
return out;
}
string VString::upcase(const string& str) {
string out = str;
for (string::iterator pos = out.begin(); pos != out.end(); ++pos) {
*pos = toupper(*pos);
}
return out;
}
string VString::quotePercent(const string& str) {
string out;
for (string::const_iterator pos = str.begin(); pos != str.end(); ++pos) {

View File

@ -65,8 +65,10 @@ public:
static bool wildmatch(const char* s, const char* p);
// Return {a}{dot}{b}, omitting dot if a or b are empty
static string dot(const string& a, const string& dot, const string& b);
// Convert string to lowercase
// Convert string to lowercase (tolower)
static string downcase(const string& str);
// Convert string to upper case (toupper)
static string upcase(const string& str);
// Replace any %'s with %%
static string quotePercent(const string& str);
// Replace any unprintable with space