Skip to content

Commit

Permalink
Merge pull request #313 from maximmenshikov/cpp_context_improvements1
Browse files Browse the repository at this point in the history
[#312,#314] Fix header parsing in C++ context and Windows build issues
  • Loading branch information
Victor-Y-Fadeev authored Apr 19, 2022
2 parents 8d1061a + ff7b242 commit b6ec034
Show file tree
Hide file tree
Showing 13 changed files with 114 additions and 23 deletions.
9 changes: 7 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ set(CMAKE_INSTALL_RPATH_USE_LINK_PATH TRUE)


if(MSVC)
set(CMAKE_CXX_STANDARD 20)

# Starting in Visual Studio 2019 version 16.8, you may specify /std:c11
add_compile_options(/W4 /WX /wd4204 /wd4221 /wd5105 /utf-8
"$<$<VERSION_GREATER_EQUAL:MSVC_VERSION,1928>:/std:c11>"
Expand All @@ -23,9 +25,11 @@ if(MSVC)

add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
else()
set(CMAKE_C_STANDARD 11)

# Add POSIX flag to declare ‘fileno’ function
# Enable -s flag for Release build to remove code info from exec
add_compile_options(-Werror=return-type -Wall -Wextra -Wno-implicit-fallthrough -Wno-unused-const-variable -std=c11
add_compile_options(-Werror=return-type -Wall -Wextra -Wno-implicit-fallthrough -Wno-unused-const-variable
"$<$<NOT:$<CXX_COMPILER_ID:AppleClang,Clang>>:-posix>"
"$<$<NOT:$<OR:$<CXX_COMPILER_ID:AppleClang,Clang>,$<CONFIG:Debug>>>:-s>"
"$<$<NOT:$<CONFIG:Debug>>:-O2>"
Expand Down Expand Up @@ -68,4 +72,5 @@ endfunction()
get_all_targets(targets .)
install(TARGETS ${targets}
RUNTIME DESTINATION ${PROJECT_NAME}
LIBRARY DESTINATION ${PROJECT_NAME})
LIBRARY DESTINATION ${PROJECT_NAME}
ARCHIVE DESTINATION ${PROJECT_NAME})
3 changes: 3 additions & 0 deletions libs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ add_subdirectory(preprocessor)

# Add compiler library
add_subdirectory(compiler)

# Add C++ wrapper library
add_subdirectory(cpp-wrap)
6 changes: 6 additions & 0 deletions libs/compiler/AST.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ static inline void node_set_child(const node *const parent, const node *const ch
*/


location node_get_location(const node *const nd)
{
const size_t argc = node_get_argc(nd);
return (location){ (size_t)node_get_arg(nd, argc - 2), (size_t)node_get_arg(nd, argc - 1) };
}

expression_t expression_get_class(const node *const nd)
{
switch (node_get_type(nd))
Expand Down
6 changes: 1 addition & 5 deletions libs/compiler/AST.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,7 @@ inline node node_broken(void)
*
* @return Node location
*/
inline location node_get_location(const node *const nd)
{
const size_t argc = node_get_argc(nd);
return (location){ (size_t)node_get_arg(nd, argc - 2), (size_t)node_get_arg(nd, argc - 1) };
}
location node_get_location(const node *const nd);


/**
Expand Down
4 changes: 2 additions & 2 deletions libs/compiler/compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include "syntax.h"
#include "uniio.h"

#ifndef _MSC_VER
#ifndef _WIN32
#include <sys/stat.h>
#include <sys/types.h>
#endif
Expand All @@ -44,7 +44,7 @@ typedef int (*encoder)(const workspace *const ws, syntax *const sx);
/** Make executable actually executable on best-effort basis (if possible) */
static inline void make_executable(const char *const path)
{
#ifndef _MSC_VER
#ifndef _WIN32
struct stat stat_buf;

if (stat(path, &stat_buf))
Expand Down
13 changes: 13 additions & 0 deletions libs/cpp-wrap/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# This library wraps all RuC libraries, ensuring that their functionality is compilable
# in C++ context
cmake_minimum_required(VERSION 3.13.5)

project(ruccpp)

add_library(${PROJECT_NAME} SHARED wrap.cpp)

target_link_libraries(${PROJECT_NAME} PUBLIC compiler utils)
if(DEFINED RUC_CPPWRAP_CHECK_PREPROCESSOR)
target_compile_definitions(${PROJECT_NAME} PRIVATE -DRUC_CPPWRAP_CHECK_PREPROCESSOR)
target_link_libraries(${PROJECT_NAME} PUBLIC preprocessor)
endif()
63 changes: 63 additions & 0 deletions libs/cpp-wrap/wrap.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* Copyright 2022 Andrey Terekhov, Maxim Menshikov
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

/* compiler headers */
#include "AST.h"
#include "builder.h"
#include "codegen.h"
#include "compiler.h"
#include "errors.h"
#include "instructions.h"
#include "lexer.h"
#include "llvmgen.h"
#include "operations.h"
#include "parser.h"
#include "reporter.h"
#include "syntax.h"
#include "token.h"
#include "writer.h"

/* preprocessor headers */
#ifdef RUC_CPPWRAP_CHECK_PREPROCESSOR
#include "calculator.h"
#include "constants.h"
#include "environment.h"
#include "error.h"
#include "linker.h"
#include "macro_load.h"
#include "macro_save.h"
#include "parser.h"
#include "preprocessor.h"
#include "utils.h"
#endif

/* utils headers */
#include "commenter.h"
#include "dll.h"
#include "hash.h"
#include "item.h"
#include "logger.h"
#include "map.h"
#include "node_vector.h"
#include "stack.h"
#include "strings.h"
#include "tree.h"
#include "uniio.h"
#include "uniprinter.h"
#include "uniscanner.h"
#include "utf8.h"
#include "vector.h"
#include "workspace.h"
12 changes: 6 additions & 6 deletions libs/utils/logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <string.h>
#include "utf8.h"

#ifdef _MSC_VER
#ifdef _WIN32
#include <windows.h>

static const uint8_t COLOR_TAG = 0x0F;
Expand Down Expand Up @@ -61,7 +61,7 @@ static logger current_note_log = &default_note_log;
static inline void set_color(const uint8_t color)
{
#if defined(NDEBUG) || !defined(__APPLE__)
#ifdef _MSC_VER
#ifdef _WIN32
SetConsoleTextAttribute(GetStdHandle(STD_ERROR_HANDLE), color);
#else
fprintf(stderr, "\x1B[1;%im", color);
Expand Down Expand Up @@ -101,7 +101,7 @@ static inline void print_msg(const uint8_t color, const char *const msg)

while (msg[j] != '^')
{
#ifdef _MSC_VER
#ifdef _WIN32
fprintf(stderr, "%c", msg[i++]);
#else
for (size_t k = utf8_symbol_size(msg[i]); k > 0; k--)
Expand All @@ -116,7 +116,7 @@ static inline void print_msg(const uint8_t color, const char *const msg)
set_color(color);
while (msg[j] != '\0')
{
#ifdef _MSC_VER
#ifdef _WIN32
fprintf(stderr, "%c", msg[i++]);
#else
for (size_t k = utf8_symbol_size(msg[i]); k > 0; k--)
Expand Down Expand Up @@ -146,15 +146,15 @@ static inline void default_log(const char *const tag, const char *const msg, con
fprintf(stderr, "%s: ", tag);

set_color(color);
#ifdef _MSC_VER
#ifdef _WIN32
char buffer[MAX_MSG_SIZE];
utf8_to_cp866(tag_log, buffer);
fprintf(stderr, "%s: ", buffer);
#else
fprintf(stderr, "%s: ", tag_log);
#endif

#ifdef _MSC_VER
#ifdef _WIN32
utf8_to_cp866(msg, buffer);
print_msg(color, buffer);
#else
Expand Down
6 changes: 6 additions & 0 deletions libs/utils/node_vector.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,9 @@ extern node node_vector_get(const node_vector *const vec, const size_t index);
extern size_t node_vector_size(const node_vector *const vec);
extern bool node_vector_is_correct(const node_vector *const vec);
extern int node_vector_clear(node_vector *const vec);


node_vector node_vector_create(void)
{
return (node_vector){ .tree = NULL, .nodes = vector_create(NODE_VECTOR_SIZE) };
}
5 changes: 1 addition & 4 deletions libs/utils/node_vector.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,7 @@ typedef struct node_vector
*
* @return Node vector
*/
inline node_vector node_vector_create(void)
{
return (node_vector){ .tree = NULL, .nodes = vector_create(NODE_VECTOR_SIZE) };
}
EXPORTED node_vector node_vector_create(void);

/**
* Add new node
Expand Down
4 changes: 2 additions & 2 deletions libs/utils/uniio.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <string.h>
#include "workspace.h"

#ifdef _MSC_VER
#ifdef _WIN32
#include <windows.h>

extern intptr_t _get_osfhandle(int fd);
Expand Down Expand Up @@ -192,7 +192,7 @@ static int out_func_user(universal_io *const io, const char *const format, va_li

static inline size_t io_get_path(FILE *const file, char *const buffer)
{
#ifdef _MSC_VER
#ifdef _WIN32
GetFinalPathNameByHandleA((HANDLE)_get_osfhandle(_fileno(file)), buffer, MAX_PATH, FILE_NAME_NORMALIZED);

size_t ret = 0;
Expand Down
4 changes: 3 additions & 1 deletion libs/utils/utf8.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
#include "dll.h"

#ifdef __APPLE__
typedef uint32_t char32_t;
#ifndef __cplusplus
typedef uint32_t char32_t;
#endif
#else
#include <uchar.h>
#endif
Expand Down
2 changes: 1 addition & 1 deletion libs/utils/workspace.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "workspace.h"
#include <string.h>

#ifndef _MSC_VER
#ifndef _WIN32
#include <unistd.h>
#else
#define F_OK 0
Expand Down

0 comments on commit b6ec034

Please sign in to comment.