Commit 2dfa76a1a7

Andrew Kelley <andrew@ziglang.org>
2019-02-16 22:17:30
fix regressions from previous commit when building with clang
1 parent 356cfa0
src/translate_c.cpp
@@ -4,7 +4,6 @@
  * This file is part of zig, which is MIT licensed.
  * See http://opensource.org/licenses/MIT
  */
-
 #include "all_types.hpp"
 #include "analyze.hpp"
 #include "c_tokenizer.hpp"
@@ -13,6 +12,7 @@
 #include "os.hpp"
 #include "translate_c.hpp"
 #include "parser.hpp"
+#include "zig_clang.h"
 
 #if __GNUC__ >= 8
 #pragma GCC diagnostic push
@@ -27,13 +27,6 @@
 #pragma GCC diagnostic pop
 #endif
 
-
-// Before the #include of zig_clang.h
-// Temporary transitional thing: override ZigClangSourceLocation with clang::SourceLocation
-#define ZigClangSourceLocation clang::SourceLocation
-#define ZIG_CLANG_SOURCE_LOCATION ZigClangABISourceLocation
-#include "zig_clang.h"
-
 #include <string.h>
 
 struct Alias {
@@ -134,8 +127,14 @@ static AstNode *trans_expr(Context *c, ResultUsed result_used, TransScope *scope
 static AstNode *trans_qual_type(Context *c, clang::QualType qt, const clang::SourceLocation &source_loc);
 static AstNode *trans_bool_expr(Context *c, ResultUsed result_used, TransScope *scope, const clang::Expr *expr, TransLRValue lrval);
 
+static ZigClangSourceLocation bitcast(clang::SourceLocation src) {
+    ZigClangSourceLocation dest;
+    memcpy(&dest, &src, sizeof(ZigClangSourceLocation));
+    return dest;
+}
+
 ATTRIBUTE_PRINTF(3, 4)
-static void emit_warning(Context *c, const clang::SourceLocation &sl, const char *format, ...) {
+static void emit_warning(Context *c, const clang::SourceLocation &clang_sl, const char *format, ...) {
     if (!c->warnings_on) {
         return;
     }
@@ -145,6 +144,7 @@ static void emit_warning(Context *c, const clang::SourceLocation &sl, const char
     Buf *msg = buf_vprintf(format, ap);
     va_end(ap);
 
+    ZigClangSourceLocation sl = bitcast(clang_sl);
     const char *filename_bytes = ZigClangSourceManager_getFilename(c->source_manager,
             ZigClangSourceManager_getSpellingLoc(c->source_manager, sl));
     Buf *path;
@@ -4707,7 +4707,7 @@ static void process_preprocessor_entities(Context *c, clang::ASTUnit &unit) {
                         continue;
                     }
 
-                    const char *begin_c = ZigClangSourceManager_getCharacterData(c->source_manager, begin_loc);
+                    const char *begin_c = ZigClangSourceManager_getCharacterData(c->source_manager, bitcast(begin_loc));
                     process_macro(c, &ctok, name, begin_c);
                 }
         }
src/zig_clang.cpp
@@ -12,6 +12,7 @@
  * 2. Provide a C interface to the Clang functions we need for self-hosting purposes.
  * 3. Prevent C++ from infecting the rest of the project.
  */
+#include "zig_clang.h"
 
 #if __GNUC__ >= 8
 #pragma GCC diagnostic push
@@ -26,14 +27,6 @@
 #pragma GCC diagnostic pop
 #endif
 
-// Before the #include of zig_clang.h
-// We'll check that the types are compatible but just use
-// the clang type.
-#define ZigClangSourceLocation clang::SourceLocation
-#define ZIG_CLANG_SOURCE_LOCATION ZigClangABISourceLocation
-
-#include "zig_clang.h"
-
 // Detect additions to the enum
 void zig2clang_BO(ZigClangBO op) {
     switch (op) {
@@ -144,35 +137,47 @@ static_assert((clang::UnaryOperatorKind)ZigClangUO_PreDec == clang::UO_PreDec, "
 static_assert((clang::UnaryOperatorKind)ZigClangUO_PreInc == clang::UO_PreInc, "");
 static_assert((clang::UnaryOperatorKind)ZigClangUO_Real == clang::UO_Real, "");
 
-static_assert(sizeof(ZigClangABISourceLocation) == sizeof(clang::SourceLocation));
+static_assert(sizeof(ZigClangSourceLocation) == sizeof(clang::SourceLocation), "");
 
-clang::SourceLocation ZigClangSourceManager_getSpellingLoc(const ZigClangSourceManager *self,
-        clang::SourceLocation Loc)
+static ZigClangSourceLocation bitcast(clang::SourceLocation src) {
+    ZigClangSourceLocation dest;
+    memcpy(&dest, &src, sizeof(ZigClangSourceLocation));
+    return dest;
+}
+
+static clang::SourceLocation bitcast(ZigClangSourceLocation src) {
+    clang::SourceLocation dest;
+    memcpy(&dest, &src, sizeof(ZigClangSourceLocation));
+    return dest;
+}
+
+ZigClangSourceLocation ZigClangSourceManager_getSpellingLoc(const ZigClangSourceManager *self,
+        ZigClangSourceLocation Loc)
 {
-    return reinterpret_cast<const clang::SourceManager *>(self)->getSpellingLoc(Loc);
+    return bitcast(reinterpret_cast<const clang::SourceManager *>(self)->getSpellingLoc(bitcast(Loc)));
 }
 
 const char *ZigClangSourceManager_getFilename(const ZigClangSourceManager *self,
-        clang::SourceLocation SpellingLoc)
+        ZigClangSourceLocation SpellingLoc)
 {
-    StringRef s = reinterpret_cast<const clang::SourceManager *>(self)->getFilename(SpellingLoc);
+    StringRef s = reinterpret_cast<const clang::SourceManager *>(self)->getFilename(bitcast(SpellingLoc));
     return (const char *)s.bytes_begin();
 }
 
 unsigned ZigClangSourceManager_getSpellingLineNumber(const ZigClangSourceManager *self,
         ZigClangSourceLocation Loc)
 {
-    return reinterpret_cast<const clang::SourceManager *>(self)->getSpellingLineNumber(Loc);
+    return reinterpret_cast<const clang::SourceManager *>(self)->getSpellingLineNumber(bitcast(Loc));
 }
 
 unsigned ZigClangSourceManager_getSpellingColumnNumber(const ZigClangSourceManager *self,
         ZigClangSourceLocation Loc)
 {
-    return reinterpret_cast<const clang::SourceManager *>(self)->getSpellingColumnNumber(Loc);
+    return reinterpret_cast<const clang::SourceManager *>(self)->getSpellingColumnNumber(bitcast(Loc));
 }
 
 const char* ZigClangSourceManager_getCharacterData(const ZigClangSourceManager *self,
         ZigClangSourceLocation SL)
 {
-    return reinterpret_cast<const clang::SourceManager *>(self)->getCharacterData(SL);
+    return reinterpret_cast<const clang::SourceManager *>(self)->getCharacterData(bitcast(SL));
 }
src/zig_clang.h
@@ -17,11 +17,7 @@
 // ATTENTION: If you modify this file, be sure to update the corresponding
 // extern function declarations in the self-hosted compiler.
 
-#ifndef ZIG_CLANG_SOURCE_LOCATION
-#define ZIG_CLANG_SOURCE_LOCATION ZigClangSourceLocation
-#endif
-
-struct ZIG_CLANG_SOURCE_LOCATION {
+struct ZigClangSourceLocation {
     unsigned ID;
 };