ReactOS  0.4.15-dev-3294-ge98684e
main.cpp File Reference
#include <gcc-plugin.h>
#include <plugin-version.h>
#include <function.h>
#include <tree.h>
#include <c-family/c-pragma.h>
#include <c-family/c-common.h>
#include <iostream>
#include <sstream>
#include <unordered_map>
#include <vector>
Include dependency graph for main.cpp:

Go to the source code of this file.

Classes

struct  seh_function
 

Macros

#define is_alpha(c)   (((c)>64 && (c)<91) || ((c)>96 && (c)<123))
 
#define VISIBLE   __attribute__((__visibility__("default")))
 
#define UNUSED   __attribute__((__unused__))
 

Functions

static struct seh_functionget_seh_function ()
 
static void handle_seh_pragma (cpp_reader *UNUSED parser)
 
static void finish_seh_function (void *event_data, void *UNUSED user_data)
 
static void register_seh_pragmas (void *UNUSED event_data, void *UNUSED user_data)
 
VISIBLE int plugin_init (struct plugin_name_args *info, struct plugin_gcc_version *version)
 

Variables

int VISIBLE plugin_is_GPL_compatible = 1
 
static std::unordered_map< struct function *, struct seh_function * > func_seh_map = {}
 

Macro Definition Documentation

◆ is_alpha

#define is_alpha (   c)    (((c)>64 && (c)<91) || ((c)>96 && (c)<123))

Definition at line 20 of file main.cpp.

◆ UNUSED

#define UNUSED   __attribute__((__unused__))

Definition at line 28 of file main.cpp.

◆ VISIBLE

#define VISIBLE   __attribute__((__visibility__("default")))

Definition at line 25 of file main.cpp.

Function Documentation

◆ finish_seh_function()

static void finish_seh_function ( void event_data,
void *UNUSED  user_data 
)
static

Definition at line 115 of file main.cpp.

116 {
117  tree fndef = (tree)event_data;
118  struct function* fun = DECL_STRUCT_FUNCTION(fndef);
119 
120  auto search = func_seh_map.find(fun);
121  if (search == func_seh_map.end())
122  return;
123 
124  /* Get our SEH details and remove us from the map */
125  seh_function* seh_fun = search->second;
126  func_seh_map.erase(search);
127 
128  if (DECL_FUNCTION_PERSONALITY(fndef) != nullptr)
129  {
130  error("Function %s has a personality. Are you mixing SEH with C++ exceptions ?",
131  IDENTIFIER_POINTER(fndef));
132  return;
133  }
134 
135  /* Update asm statement */
136  std::stringstream asm_str;
137  asm_str << ".seh_handler __C_specific_handler";
138  if (seh_fun->unwind)
139  asm_str << ", @unwind";
140  if (seh_fun->except)
141  asm_str << ", @except";
142  asm_str << "\n";
143  asm_str << "\t.seh_handlerdata\n";
144  asm_str << "\t.long " << seh_fun->count << "\n";
145  asm_str << "\t.seh_code";
146 
147  strncpy(const_cast<char*>(TREE_STRING_POINTER(seh_fun->asm_header_text)),
148  asm_str.str().c_str(),
149  TREE_STRING_LENGTH(seh_fun->asm_header_text));
150 
151  delete seh_fun;
152 }
static short search(int val, const short *table, int size)
Definition: msg711.c:255
struct _tree tree
char * strncpy(char *DstString, const char *SrcString, ACPI_SIZE Count)
Definition: utclib.c:427
bool except
Definition: main.cpp:37
void error(const std::string &err)
Definition: main.cpp:99
tree asm_header_text
Definition: main.cpp:38
static std::unordered_map< struct function *, struct seh_function * > func_seh_map
Definition: main.cpp:57
bool unwind
Definition: main.cpp:36
basic_stringstream< char, char_traits< char >, allocator< char > > stringstream
Definition: _iosfwd.h:128
size_t count
Definition: main.cpp:40

Referenced by plugin_init().

◆ get_seh_function()

static struct seh_function* get_seh_function ( )
static

Definition at line 61 of file main.cpp.

62 {
63  auto search = func_seh_map.find(cfun);
64  if (search != func_seh_map.end())
65  return search->second;
66 
67  auto seh_fun = new seh_function(cfun);
68  func_seh_map.insert({cfun, seh_fun});
69 
70  return seh_fun;
71 }
static short search(int val, const short *table, int size)
Definition: msg711.c:255
static std::unordered_map< struct function *, struct seh_function * > func_seh_map
Definition: main.cpp:57

Referenced by handle_seh_pragma().

◆ handle_seh_pragma()

static void handle_seh_pragma ( cpp_reader *UNUSED  parser)
static

Definition at line 75 of file main.cpp.

76 {
77  tree x, arg;
78  std::stringstream label_decl;
79 
80  if (!cfun)
81  {
82  error("%<#pragma REACTOS seh%> is not allowed outside functions");
83  return;
84  }
85 
86  if ((pragma_lex(&x) != CPP_OPEN_PAREN) ||
87  (pragma_lex(&arg) != CPP_NAME) ||
88  (pragma_lex(&x) != CPP_CLOSE_PAREN) ||
89  (pragma_lex(&x) != CPP_EOF))
90  {
91  error("%<#pragma REACTOS seh%> needs one parameter%>");
92  return;
93  }
94 
95  const char* op = IDENTIFIER_POINTER(arg);
96 
97  seh_function* seh_fun = get_seh_function();
98  if (strcmp(op, "except") == 0)
99  seh_fun->except = true;
100  else if (strcmp(op, "finally") == 0)
101  seh_fun->unwind = true;
102  else
103  {
104  error("Wrong argument for %<#pragma REACTOS seh%>. Expected \"except\" or \"finally\"");
105  return;
106  }
107  seh_fun->count++;
108 
109  /* Make sure we use a frame pointer. REACTOS' PSEH depends on this */
110  cfun->machine->accesses_prev_frame = 1;
111 }
GLint GLint GLint GLint GLint x
Definition: gl.h:1548
void * arg
Definition: msvc.h:10
bool except
Definition: main.cpp:37
void error(const std::string &err)
Definition: main.cpp:99
bool unwind
Definition: main.cpp:36
UINT op
Definition: effect.c:236
static struct seh_function * get_seh_function()
Definition: main.cpp:61
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
basic_stringstream< char, char_traits< char >, allocator< char > > stringstream
Definition: _iosfwd.h:128
size_t count
Definition: main.cpp:40

Referenced by register_seh_pragmas().

◆ plugin_init()

VISIBLE int plugin_init ( struct plugin_name_args *  info,
struct plugin_gcc_version *  version 
)

Definition at line 164 of file main.cpp.

166 {
167  if (!plugin_default_version_check (version, &gcc_version))
168  {
169  std::cerr << "This GCC plugin is for version " << GCCPLUGIN_VERSION_MAJOR << "." << GCCPLUGIN_VERSION_MINOR << "\n";
170  return 1;
171  }
172 
173  register_callback(info->base_name, PLUGIN_PRAGMAS, register_seh_pragmas, NULL);
174  register_callback(info->base_name, PLUGIN_FINISH_PARSE_FUNCTION, finish_seh_function, NULL);
175 
176  return 0;
177 }
static void finish_seh_function(void *event_data, void *UNUSED user_data)
Definition: main.cpp:115
static const WCHAR version[]
Definition: asmname.c:66
_STLP_DECLSPEC _Stl_aligned_buffer< ostream > cerr
Definition: iostream.cpp:102
static void register_seh_pragmas(void *UNUSED event_data, void *UNUSED user_data)
Definition: main.cpp:156
#define NULL
Definition: types.h:112

◆ register_seh_pragmas()

static void register_seh_pragmas ( void *UNUSED  event_data,
void *UNUSED  user_data 
)
static

Definition at line 156 of file main.cpp.

157 {
158  c_register_pragma("REACTOS", "seh", handle_seh_pragma);
159 }
static void handle_seh_pragma(cpp_reader *UNUSED parser)
Definition: main.cpp:75

Referenced by plugin_init().

Variable Documentation

◆ func_seh_map

std::unordered_map<struct function*, struct seh_function*> func_seh_map = {}
static

Definition at line 57 of file main.cpp.

Referenced by finish_seh_function(), and get_seh_function().

◆ plugin_is_GPL_compatible

int VISIBLE plugin_is_GPL_compatible = 1

Definition at line 32 of file main.cpp.