satya-das / cppparser

A library to parse C/C++ source as AST
Other
278 stars 36 forks source link

using namespace support #22

Closed agladilin closed 3 weeks ago

agladilin commented 1 year ago

Could you add the code below in order to support the using namespace directive?

diff --git a/pub/cppwriter.h b/pub/cppwriter.h
index 5b9e3a81..e2541d48 100644
--- a/pub/cppwriter.h
+++ b/pub/cppwriter.h
@@ -66,6 +66,9 @@ public:
   virtual void emitUsingDecl(const CppUsingDecl* usingDecl,
                              std::ostream&       stm,
                              CppIndent           indentation = CppIndent()) const;
+  virtual void emitUsingNamespaceDecl(const CppUsingNamespaceDecl* usingDecl,
+                                      std::ostream&                stm,
+                                      CppIndent                    indentation = CppIndent()) const;
   virtual void emitTypedefList(const CppTypedefList* typedefList,
                                std::ostream&         stm,
                                CppIndent             indentation = CppIndent()) const;
diff --git a/src/cppwriter.cpp b/src/cppwriter.cpp
index 89034bcd..2ed6a738 100644
--- a/src/cppwriter.cpp
+++ b/src/cppwriter.cpp
@@ -106,6 +106,8 @@ void CppWriter::emit(const CppObj* cppObj, std::ostream& stm, CppIndent indentat
       return emitDocComment((CppDocComment*) cppObj, stm, indentation);
     case CppObjType::kUsingDecl:
       return emitUsingDecl((CppUsingDecl*) cppObj, stm, indentation);
+    case CppObjType::kUsingNamespaceDecl:
+      return emitUsingNamespaceDecl((CppUsingNamespaceDecl*) cppObj, stm, indentation);
     case CppObjType::kTypedefName:
       return emitTypedef((CppTypedefName*) cppObj, stm, indentation);
     case CppObjType::kTypedefNameList:
@@ -411,6 +413,13 @@ void CppWriter::emitUsingDecl(const CppUsingDecl* usingDecl,
   stm << ";\n";
 }

+void CppWriter::emitUsingNamespaceDecl(const CppUsingNamespaceDecl* usingDecl,
+                                       std::ostream&                stm,
+                                       CppIndent                    indentation /* = CppIndent()*/) const
+{
+  stm << indentation << "using namespace " << usingDecl->name_ << ";\n";
+}
+
 void CppWriter::emitTypedefList(const CppTypedefList* typedefList,
                                 std::ostream&         stm,
                                 CppIndent             indentation /* = CppIndent()*/) const
satya-das commented 3 weeks ago

With the latest update this is already supported.