From 2dfa76a1a754d2537b9cc6faf7b98461de0b1429 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sat, 16 Feb 2019 16:17:30 -0500 Subject: [PATCH] fix regressions from previous commit when building with clang --- src/translate_c.cpp | 20 ++++++++++---------- src/zig_clang.cpp | 39 ++++++++++++++++++++++----------------- src/zig_clang.h | 6 +----- 3 files changed, 33 insertions(+), 32 deletions(-) diff --git a/src/translate_c.cpp b/src/translate_c.cpp index 7462c8e046..7ebd90e3fe 100644 --- a/src/translate_c.cpp +++ b/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 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); } } diff --git a/src/zig_clang.cpp b/src/zig_clang.cpp index 62d1e34658..4e6e4e7a98 100644 --- a/src/zig_clang.cpp +++ b/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(self)->getSpellingLoc(Loc); + return bitcast(reinterpret_cast(self)->getSpellingLoc(bitcast(Loc))); } const char *ZigClangSourceManager_getFilename(const ZigClangSourceManager *self, - clang::SourceLocation SpellingLoc) + ZigClangSourceLocation SpellingLoc) { - StringRef s = reinterpret_cast(self)->getFilename(SpellingLoc); + StringRef s = reinterpret_cast(self)->getFilename(bitcast(SpellingLoc)); return (const char *)s.bytes_begin(); } unsigned ZigClangSourceManager_getSpellingLineNumber(const ZigClangSourceManager *self, ZigClangSourceLocation Loc) { - return reinterpret_cast(self)->getSpellingLineNumber(Loc); + return reinterpret_cast(self)->getSpellingLineNumber(bitcast(Loc)); } unsigned ZigClangSourceManager_getSpellingColumnNumber(const ZigClangSourceManager *self, ZigClangSourceLocation Loc) { - return reinterpret_cast(self)->getSpellingColumnNumber(Loc); + return reinterpret_cast(self)->getSpellingColumnNumber(bitcast(Loc)); } const char* ZigClangSourceManager_getCharacterData(const ZigClangSourceManager *self, ZigClangSourceLocation SL) { - return reinterpret_cast(self)->getCharacterData(SL); + return reinterpret_cast(self)->getCharacterData(bitcast(SL)); } diff --git a/src/zig_clang.h b/src/zig_clang.h index 840d227bb4..78d5cd8cbc 100644 --- a/src/zig_clang.h +++ b/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; };