From 1c768695a231f901d244b3d88fa9570c40ba8214 Mon Sep 17 00:00:00 2001
From: Virgile Prevosto <virgile.prevosto@m4x.org>
Date: Thu, 8 Feb 2024 17:04:21 +0100
Subject: [PATCH] [irgen] Better location for ghost declarations

---
 framaCIRGen_src/ACSLComment.cpp | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/framaCIRGen_src/ACSLComment.cpp b/framaCIRGen_src/ACSLComment.cpp
index e5c18ec6..72e4b97a 100644
--- a/framaCIRGen_src/ACSLComment.cpp
+++ b/framaCIRGen_src/ACSLComment.cpp
@@ -32,6 +32,7 @@
 #include "clang/Parse/Parser.h"
 #include "clang/AST/ASTConsumer.h"
 #include "clang/Sema/Scope.h"
+#include <sstream>
 #include <string>
 
 bool
@@ -261,12 +262,18 @@ void ACSLComment::parseGhostGlobal(
   code = code.substr(_startOfGhost);
   clang::SourceLocation loc =
     getSourceLocation().getLocWithOffset(_startOfGhost);  // FIXME - check this for correct location
+  auto& source_manager = compilerInstance.getSourceManager();
+  auto filename = source_manager.getFilename(loc);
+  auto line = source_manager.getSpellingLineNumber(loc);
+  std::ostringstream content;
+  content << "#line " << line << " \"" << filename.str() << "\"\n";
+  auto buffer = content.str();
+  buffer+=code;
   clang::Preprocessor& cpp = compilerInstance.getPreprocessor();
   clang::Parser* parser =
     static_cast<clang::Parser*>(cpp.getCodeCompletionHandler());
   std::unique_ptr<llvm::MemoryBuffer> buff =
-    llvm::MemoryBuffer::getMemBufferCopy(code, "ghost.cxx");
-  clang::SourceManager& source_manager = compilerInstance.getSourceManager();
+    llvm::MemoryBuffer::getMemBufferCopy(buffer, filename);
   clang::FileID file =
     source_manager.createFileID(
 	    std::move(buff),clang::SrcMgr::C_User,0,0,loc);
-- 
GitLab