aboutsummaryrefslogtreecommitdiff
path: root/odb/plugin.cxx
blob: 51f0cb126a98f60a5708337d9ae7d8a13c52b22c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
// file      : odb/plugin.cxx
// copyright : Copyright (c) 2009-2015 Code Synthesis Tools CC
// license   : GNU GPL v3; see accompanying LICENSE file

#include <odb/gcc.hxx> // Keep it first.

#include <unistd.h>    // stat()
#include <sys/types.h> // stat
#include <sys/stat.h>  // stat

#include <memory>  // std::auto_ptr
#include <string>
#include <vector>
#include <cstring> // std::strcpy, std::strstr
#include <cassert>
#include <iostream>

#include <cutl/re.hxx>
#include <cutl/fs/path.hxx>

#include <odb/pragma.hxx>
#include <odb/parser.hxx>
#include <odb/options.hxx>
#include <odb/option-functions.hxx>
#include <odb/features.hxx>
#include <odb/profile.hxx>
#include <odb/version.hxx>
#include <odb/validator.hxx>
#include <odb/processor.hxx>
#include <odb/generator.hxx>
#include <odb/semantics/unit.hxx>

using namespace std;
using namespace semantics;

using cutl::fs::path;
using cutl::fs::invalid_path;

typedef vector<path> paths;

int plugin_is_GPL_compatible;
auto_ptr<options const> options_;
paths profile_paths_;
path file_;    // File being compiled.
paths inputs_; // List of input files in at-once mode or just file_.

bool (*cpp_error_prev) (
  cpp_reader*, int, int, location_t, unsigned int, const char*, va_list*);

static bool
cpp_error_filter (cpp_reader* r,
                  int level,
                  int reason,
                  location_t l,
                  unsigned int column_override,
                  const char* msg,
                  va_list* ap)
{
  // #pragma once in the main file. Note that the message that we get is
  // potentially translated so we search for the substring (there is
  // currently only one warning in libcpp that mentions #pragma once).
  // Unfortunately, some translations translate the 'once' word (e.g,
  // #pragma uno; I wonder if one can actually specify it like that in
  // the source code). Oh, well, there is only so much we can do.
  //
  if (strstr (msg, "#pragma once") != 0)
    return true;

  return cpp_error_prev (r, level, reason, l, column_override, msg, ap);
}

// A prefix of the _cpp_file struct. This struct is not part of the
// public interface so we have to resort to this technique (based on
// libcpp/files.c).
//
struct cpp_file_prefix
{
  char const* name;
  char const* path;
  char const* pchname;
  char const* dir_name;
  _cpp_file* next_file;
  const uchar* buffer;
  const uchar* buffer_start;
  const cpp_hashnode *cmacro;
  cpp_dir *dir;
  struct stat st;
};

extern "C" void
start_unit_callback (void*, void*)
{
  // Set the preprocessor error callback to filter out useless diagnostics.
  //
  cpp_callbacks* cb (cpp_get_callbacks (parse_in));

  if (cb->error == 0)
  {
    cerr << "ice: expected cpp error callback to be set" << endl;
    exit (1);
  }

  cpp_error_prev = cb->error;
  cb->error = &cpp_error_filter;

  // Set the directory of the main file (stdin) to that of the orginal
  // file so that relative inclusion works. Also adjust the path and
  // re-stat the file so that #pragma once works.
  //
  cpp_buffer* b (cpp_get_buffer (parse_in));
  _cpp_file* f (cpp_get_file (b));
  cpp_dir* d (cpp_get_dir (f));
  char const* p (cpp_get_path (f));

  cpp_file_prefix* fp (reinterpret_cast<cpp_file_prefix*> (f));

  // Perform some sanity checks.
  //
  if (p != 0 && *p == '\0'     // The path should be empty (stdin).
      && cpp_get_prev (b) == 0 // This is the only buffer (main file).
      && fp->path == p         // Our prefix corresponds to the actual type.
      && fp->dir == d          // Our prefix corresponds to the actual type.
      && fp->dir_name == 0)    // The directory part hasn't been initialized.
  {
    // The dir_name is initialized by libcpp lazily so we can preemptively
    // set it to what we need.
    //
    path d (file_.directory ());
    char* s;

    if (d.empty ())
    {
      s = XNEWVEC (char, 1);
      *s = '\0';
    }
    else
    {
      size_t n (d.string ().size ());
      s = XNEWVEC (char, n + 2);
      strcpy (s, d.string ().c_str ());
      s[n] = path::traits::directory_separator;
      s[n + 1] = '\0';
    }

    fp->dir_name = s;

    // Unless we are in the at-once mode (where input files are actually
    // #include'ed into the synthesized stdin), pretend that we are the
    // actual input file. This is necessary for the #pragma once to work.
    //
    // All this relies on the way things are implemented in libcpp. In
    // particular, the #pragma once code first checks if the mtime and
    // size of files match (that's why we need stat() below). If they
    // do, then it goes ahead and compares their contents. To re-load
    // the contents of the file libcpp uses the path (that's why we
    // need to adjust that as well).
    //
    if (inputs_.size () == 1)
    {
      string const& f (file_.string ());

      XDELETEVEC (fp->path);
      size_t n (f.size ());
      char* p (XNEWVEC (char, n + 1));
      strcpy (p, f.c_str ());
      p[n] = '\0';
      fp->path = p;

      // This call shouldn't fail since we've just opened it in the driver.
      stat (fp->path, &fp->st);
    }
  }
  else
  {
    cerr << "ice: unable to initialize main file directory" << endl;
    exit (1);
  }
}

extern "C" void
gate_callback (void*, void*)
{
  // If there were errors during compilation, let GCC handle the
  // exit.
  //
  if (errorcount || sorrycount)
    return;

  int r (0);

  try
  {
    // Post process pragmas.
    //
    post_process_pragmas ();

    // Parse the GCC tree to semantic graph.
    //
    parser p (*options_, loc_pragmas_, ns_loc_pragmas_, decl_pragmas_);
    auto_ptr<unit> u (p.parse (global_namespace, file_));

    features f;

    // Validate, pass 1.
    //
    validate (*options_, f, *u, file_, 1);

    // Process.
    //
    process (*options_, f, *u, file_);

    // Validate, pass 2.
    //
    validate (*options_, f, *u, file_, 2);

    // Generate.
    //
    generate (*options_, f, *u, file_, inputs_);
  }
  catch (cutl::re::format const& e)
  {
    cerr << "error: invalid regex: '" << e.regex () << "': " <<
      e.description () << endl;
    r = 1;
  }
  catch (pragmas_failed const&)
  {
    // Diagnostics has aready been issued.
    //
    r = 1;
  }
  catch (parser::failed const&)
  {
    // Diagnostics has aready been issued.
    //
    r = 1;
  }
  catch (validator_failed const&)
  {
    // Diagnostics has aready been issued.
    //
    r = 1;
  }
  catch (processor_failed const&)
  {
    // Diagnostics has aready been issued.
    //
    r = 1;
  }
  catch (generator_failed const&)
  {
    // Diagnostics has aready been issued.
    //
    r = 1;
  }

  exit (r);
}

static char const* const odb_version = ODB_COMPILER_VERSION_STR;

typedef vector<string> strings;

extern "C" int
plugin_init (plugin_name_args* plugin_info, plugin_gcc_version*)
{
  int r (0);
  plugin_info->version = odb_version;

  try
  {
    // Parse options.
    //
    {
      strings argv_str;
      argv_str.push_back (plugin_info->base_name);

      for (int i (0); i < plugin_info->argc; ++i)
      {
        plugin_argument& a (plugin_info->argv[i]);

        // A value cannot contain '=' so it is passed as the backspace
        // character.
        //
        string v (a.value != 0 ? a.value : "");
        for (size_t i (0); i < v.size (); ++i)
          if (v[i] == '\b')
            v[i] = '=';

        // Handle service options.
        //
        if (strcmp (a.key, "svc-path") == 0)
        {
          profile_paths_.push_back (path (v));
          continue;
        }

        if (strcmp (a.key, "svc-file") == 0)
        {
          // First is the main file. Subsequent are inputs in the at-once
          // mode.
          //
          if (file_.empty ())
            file_ = path (v);
          else
            inputs_.push_back (path (v));

          continue;
        }

        string opt (strlen (a.key) > 1 ? "--" : "-");
        opt += a.key;

        argv_str.push_back (opt);

        if (!v.empty ())
          argv_str.push_back (v);
      }

      vector<char*> argv;
      for (strings::iterator i (argv_str.begin ()); i != argv_str.end (); ++i)
        argv.push_back (const_cast<char*> (i->c_str ()));

      int argc (static_cast<int> (argv.size ()));

      if (inputs_.empty ())
        inputs_.push_back (file_);

      // Two-phase options parsing, similar to the driver.
      //
      cli::argv_file_scanner::option_info oi[3];
      oi[0].option = "--options-file";
      oi[0].search_func = 0;
      oi[1].option = "-p";
      oi[2].option = "--profile";

      database db;
      {
        oi[1].search_func = &profile_search_ignore;
        oi[2].search_func = &profile_search_ignore;

        cli::argv_file_scanner scan (argc, &argv[0], oi, 3);
        options ops (scan);
        assert (ops.database_specified ());
        db = ops.database ()[0];
      }

      profile_data pd (profile_paths_, db, "odb plugin");
      oi[1].search_func = &profile_search;
      oi[2].search_func = &profile_search;
      oi[1].arg = &pd;
      oi[2].arg = &pd;

      cli::argv_file_scanner scan (argc, &argv[0], oi, 3);
      auto_ptr<options> ops (
        new options (scan, cli::unknown_mode::fail, cli::unknown_mode::fail));

      // Process options.
      //
      process_options (*ops);

      options_ = ops;
      pragma_db_ = db;
      pragma_multi_ = options_->multi_database ();
    }

    if (options_->trace ())
      cerr << "starting plugin " << plugin_info->base_name << endl;

    // Disable assembly output. GCC doesn't define HOST_BIT_BUCKET
    // correctly for MinGW (it still used /dev/null which fails to
    // open).
    //
#ifdef _WIN32
    asm_file_name = "nul";
#else
    asm_file_name = HOST_BIT_BUCKET;
#endif

    // Register callbacks.
    //
    register_callback (plugin_info->base_name,
                       PLUGIN_PRAGMAS,
                       register_odb_pragmas,
                       0);

    register_callback (plugin_info->base_name,
                       PLUGIN_START_UNIT,
                       start_unit_callback,
                       0);

    register_callback (plugin_info->base_name,
                       PLUGIN_OVERRIDE_GATE,
                       &gate_callback,
                       0);
  }
  catch (cli::exception const& ex)
  {
    cerr << ex << endl;
    r = 1;
  }

  if (r != 0)
    exit (r);

  return r;
}