summary refs log tree commit diff
diff options
context:
space:
mode:
authorNguyễn Gia Phong <cnx@loang.net>2024-09-25 15:28:04 +0900
committerNguyễn Gia Phong <cnx@loang.net>2024-09-25 15:28:04 +0900
commiteec22486f3680d43d5e58d2c75428ee418c6ae98 (patch)
tree457a9638e056597649392aac05c9d19456c3faa7
parent2b4218212198403eaa91fd99200611276b1f457a (diff)
downloadgoatc-main.tar.gz
Use C++ namespace HEAD main
-rw-r--r--goatc.cc40
-rw-r--r--goatc.hh53
2 files changed, 53 insertions, 40 deletions
diff --git a/goatc.cc b/goatc.cc
index 354d377..607d056 100644
--- a/goatc.cc
+++ b/goatc.cc
@@ -19,35 +19,43 @@
 #include "goatc.hh"
 
 #include <clang/AST/AST.h>
-#include <clang/AST/RecursiveASTVisitor.h>
 #include <clang/Frontend/CompilerInstance.h>
 #include <clang/Sema/Sema.h>
 #include <llvm/Support/raw_ostream.h>
 
 bool
-GoatCConsumer::HandleTopLevelDecl (clang::DeclGroupRef dg)
+goatc::Visitor::VisitDecl (clang::Decl* decl)
 {
-  if (this->noop)
-    return true;
-
-  for (auto const& d : dg)
-    if (auto const& nd = llvm::dyn_cast <clang::NamedDecl> (d))
-      llvm::errs() << "top-level-decl: \"" << nd->getNameAsString() << "\"\n";
+  if (auto* named_decl = llvm::dyn_cast <clang::NamedDecl> (decl))
+    {
+      clang::FullSourceLoc loc = this->ctx->getFullLoc (decl->getBeginLoc ());
+      if (loc.isValid ())
+        llvm::outs ()
+          << "Found " << named_decl->getQualifiedNameAsString ()
+          << " at " << loc.getSpellingLineNumber ()
+          << ':' << loc.getSpellingColumnNumber () << '\n';
+    }
   return true;
 }
 
+void
+goatc::Consumer::HandleTranslationUnit (clang::ASTContext& ctx)
+{
+  this->visitor.TraverseDecl (ctx.getTranslationUnitDecl ());
+}
+
 clang::PluginASTAction::ActionType
-GoatCAction::getActionType ()
+goatc::Action::getActionType ()
 {
   return AddBeforeMainAction;
 }
 
 std::unique_ptr <clang::ASTConsumer>
-GoatCAction::CreateASTConsumer (clang::CompilerInstance& ci,
-                                llvm::StringRef in_file)
+goatc::Action::CreateASTConsumer (clang::CompilerInstance& ci,
+                                  llvm::StringRef in_file)
 {
-  llvm::errs () << in_file << '\n';
-  return std::make_unique <GoatCConsumer> (ci, in_file != this->source_file);
+  return std::make_unique <goatc::Consumer> (&ci.getASTContext (),
+                                             in_file != this->source_file);
 }
 
 void
@@ -59,14 +67,14 @@ printHelp (llvm::raw_ostream& ros)
 }
 
 bool
-GoatCAction::ParseArgs (const clang::CompilerInstance& ci,
-                        const std::vector <std::string> &args)
+goatc::Action::ParseArgs (const clang::CompilerInstance& ci,
+                          const std::vector <std::string> &args)
 {
   auto& d = ci.getDiagnostics ();
   for (unsigned i = 0, e = args.size(); i < e; ++i)
     if (args[i] == "-h" || args[i] == "--help")
       {
-        printHelp (llvm::errs ());
+        printHelp (llvm::outs ());
         // FIXME: exit with status 0
         return false;
       }
diff --git a/goatc.hh b/goatc.hh
index 76e1525..5eed297 100644
--- a/goatc.hh
+++ b/goatc.hh
@@ -19,37 +19,42 @@
 #include <clang/AST/RecursiveASTVisitor.h>
 #include <clang/Frontend/FrontendPluginRegistry.h>
 
-class GoatCVisitor : public clang::RecursiveASTVisitor<GoatCVisitor> {
-  clang::ASTContext* ctx;
+namespace goatc
+{
+  class Visitor : public clang::RecursiveASTVisitor <Visitor> {
+    clang::ASTContext* ctx;
 
-public:
-  GoatCVisitor(clang::ASTContext* ctx) : ctx {ctx}
-  { }
-};
+  public:
+    Visitor(clang::ASTContext* ctx) : ctx {ctx}
+    { }
 
-class GoatCConsumer : public clang::ASTConsumer {
-  clang::CompilerInstance& ci;
-  /// Consuming source file that is not patched
-  bool noop;
+    bool VisitDecl (clang::Decl*);
+  };
 
-  bool HandleTopLevelDecl (clang::DeclGroupRef) override;
+  class Consumer : public clang::ASTConsumer {
+    Visitor visitor;
+    /// Consuming source file that is not patched
+    bool noop;
 
-public:
-  GoatCConsumer (clang::CompilerInstance& ci, bool noop) : ci {ci}, noop {noop}
-  { }
-};
+    void HandleTranslationUnit (clang::ASTContext&) override;
 
-class GoatCAction : public clang::PluginASTAction {
-  std::string source_file;
+  public:
+    Consumer (clang::ASTContext* ctx, bool noop) : visitor {ctx}, noop {noop}
+    { }
+  };
 
-  std::unique_ptr <clang::ASTConsumer> CreateASTConsumer
-    (clang::CompilerInstance&, llvm::StringRef) override;
+  class Action : public clang::PluginASTAction {
+    std::string source_file;
 
-  bool ParseArgs (const clang::CompilerInstance&,
-                  const std::vector <std::string>&) override;
+    std::unique_ptr <clang::ASTConsumer> CreateASTConsumer
+      (clang::CompilerInstance&, llvm::StringRef) override;
 
-  clang::PluginASTAction::ActionType getActionType () override;
-};
+    bool ParseArgs (const clang::CompilerInstance&,
+                    const std::vector <std::string>&) override;
 
-static clang::FrontendPluginRegistry::Add <GoatCAction>
+    clang::PluginASTAction::ActionType getActionType () override;
+  };
+} // namespace goatc
+
+static clang::FrontendPluginRegistry::Add <goatc::Action>
 X {"goatc", "chimera breeder"};