ide-compile-commands.c 21.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* ide-compile-commands.c
 *
 * Copyright © 2017 Christian Hergert <chergert@redhat.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#define G_LOG_DOMAIN "ide-compile-commands"

21 22
#include "config.h"

23
#include <dazzle.h>
24
#include <json-glib/json-glib.h>
25
#include <string.h>
26 27 28 29

#include "ide-debug.h"

#include "buildsystem/ide-compile-commands.h"
30
#include "threading/ide-task.h"
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

/**
 * SECTION:ide-compile-commands
 * @title: IdeCompileCommands
 * @short_description: Integration with compile_commands.json
 *
 * The #IdeCompileCommands object provides a simplified interface to
 * interact with compile_commands.json files which are generated by a
 * number of build systems, including Clang tooling, Meson and CMake.
 *
 * Create a new #IdeCompileCommands instance, and then asynchronously
 * load the file using ide_compile_commands_load_async(). After the
 * database has been loaded, you can access build commands using
 * ide_compile_commands_lookup().
 *
 * Due to the rather unfortunate design of JSON, this file holds on
 * to a number of strings during the lifetime of the object, for each
 * of the compile commands. On larger projects, this can be the order
 * of a couple of megabytes.
 *
 * Since: 3.28
 */

struct _IdeCompileCommands
{
56 57 58
  GObject parent_instance;

  /*
59
   * The info_by_file field contains a hashtable whose keys are #GFile
60 61 62
   * matching the file that is to be compiled. It contains as a value
   * the CompileInfo struct describing how to compile that file.
   */
63
  GHashTable *info_by_file;
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79

  /*
   * The vala_info field contains an array of every vala like file we've
   * discovered while parsing the database. This is used so because some
   * compile_commands.json only have a single valac command which wont
   * match the file we want to lookup (Notably Meson-based).
   */
  GPtrArray *vala_info;

  /*
   * The has_loaded field determines if we've had a load (async or sync
   * variant) operation called. We can only do this safely once because
   * we assign state in the task worker. Callers must discard the object
   * if the load operation fails.
   */
  guint has_loaded : 1;
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
};

typedef struct
{
  GFile *directory;
  GFile *file;
  gchar *command;
} CompileInfo;

G_DEFINE_TYPE (IdeCompileCommands, ide_compile_commands, G_TYPE_OBJECT)

static void
compile_info_free (gpointer data)
{
  CompileInfo *info = data;

  if (info != NULL)
    {
      g_clear_object (&info->directory);
      g_clear_object (&info->file);
      g_clear_pointer (&info->command, g_free);
      g_slice_free (CompileInfo, info);
    }
}

static void
ide_compile_commands_finalize (GObject *object)
{
  IdeCompileCommands *self = (IdeCompileCommands *)object;

  g_clear_pointer (&self->info_by_file, g_hash_table_unref);
111
  g_clear_pointer (&self->vala_info, g_ptr_array_unref);
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

  G_OBJECT_CLASS (ide_compile_commands_parent_class)->finalize (object);
}

static void
ide_compile_commands_class_init (IdeCompileCommandsClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->finalize = ide_compile_commands_finalize;
}

static void
ide_compile_commands_init (IdeCompileCommands *self)
{
}

/**
 * ide_compile_commands_new:
 *
 * Creates a new #IdeCompileCommands object which can be used to parse
 * clang-style compile commands database files (compile_commands.json).
 *
 * Returns: The newly created #IdeCompileCommands
 *
 * Since: 3.28
 */
IdeCompileCommands *
ide_compile_commands_new (void)
{
  return g_object_new (IDE_TYPE_COMPILE_COMMANDS, NULL);
}

static void
146
ide_compile_commands_load_worker (IdeTask      *task,
147 148 149 150 151 152 153 154 155 156
                                  gpointer      source_object,
                                  gpointer      task_data,
                                  GCancellable *cancellable)
{
  IdeCompileCommands *self = source_object;
  GFile *gfile = task_data;
  g_autoptr(JsonParser) parser = NULL;
  g_autoptr(GError) error = NULL;
  g_autoptr(GHashTable) info_by_file = NULL;
  g_autoptr(GHashTable) directories_by_path = NULL;
157
  g_autoptr(GPtrArray) vala_info = NULL;
158 159 160 161 162 163 164 165
  g_autofree gchar *contents = NULL;
  JsonNode *root;
  JsonArray *ar;
  gsize len = 0;
  guint n_items;

  IDE_ENTRY;

166
  g_assert (IDE_IS_TASK (task));
167 168 169 170 171 172 173 174 175
  g_assert (IDE_IS_COMPILE_COMMANDS (self));
  g_assert (G_IS_FILE (gfile));
  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));

  parser = json_parser_new ();

  if (!g_file_load_contents (gfile, cancellable, &contents, &len, NULL, &error) ||
      !json_parser_load_from_data (parser, contents, len, &error))
    {
176
      ide_task_return_error (task, g_steal_pointer (&error));
177 178 179 180 181 182 183
      IDE_EXIT;
    }

  if (NULL == (root = json_parser_get_root (parser)) ||
      !JSON_NODE_HOLDS_ARRAY (root) ||
      NULL == (ar = json_node_get_array (root)))
    {
184 185 186 187
      ide_task_return_new_error (task,
                                 G_IO_ERROR,
                                 G_IO_ERROR_INVALID_DATA,
                                 "Failed to extract commands, invalid json");
188 189 190 191 192 193 194 195 196 197 198 199 200
      IDE_EXIT;
    }

  info_by_file = g_hash_table_new_full (g_file_hash,
                                        (GEqualFunc)g_file_equal,
                                        NULL,
                                        compile_info_free);

  directories_by_path = g_hash_table_new_full (g_str_hash,
                                               g_str_equal,
                                               NULL,
                                               g_object_unref);

201 202
  vala_info = g_ptr_array_new_with_free_func (compile_info_free);

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
  n_items = json_array_get_length (ar);

  for (guint i = 0; i < n_items; i++)
    {
      CompileInfo *info;
      JsonNode *item;
      JsonNode *value;
      JsonObject *obj;
      GFile *dir;
      const gchar *directory = NULL;
      const gchar *file = NULL;
      const gchar *command = NULL;

      item = json_array_get_element (ar, i);

      /* Skip past this node if its invalid for some reason, so we
       * can try to be tolerante of errors created by broken tooling.
       */
      if (item == NULL ||
          !JSON_NODE_HOLDS_OBJECT (item) ||
          NULL == (obj = json_node_get_object (item)))
        continue;

      if (json_object_has_member (obj, "file") &&
          NULL != (value = json_object_get_member (obj, "file")) &&
          JSON_NODE_HOLDS_VALUE (value))
        file = json_node_get_string (value);

      if (json_object_has_member (obj, "directory") &&
          NULL != (value = json_object_get_member (obj, "directory")) &&
          JSON_NODE_HOLDS_VALUE (value))
        directory = json_node_get_string (value);

      if (json_object_has_member (obj, "command") &&
          NULL != (value = json_object_get_member (obj, "command")) &&
          JSON_NODE_HOLDS_VALUE (value))
        command = json_node_get_string (value);

      /* Ignore items that are missing something or other */
      if (file == NULL || command == NULL || directory == NULL)
        continue;

      /* Try to reduce the number of GFile we have for directories */
      if (NULL == (dir = g_hash_table_lookup (directories_by_path, directory)))
        {
          dir = g_file_new_for_path (directory);
          g_hash_table_insert (directories_by_path, (gchar *)directory, dir);
        }

Christian Hergert's avatar
Christian Hergert committed
252
      info = g_slice_new0 (CompileInfo);
253 254 255
      info->file = g_file_resolve_relative_path (dir, file);
      info->directory = g_object_ref (dir);
      info->command = g_strdup (command);
256
      g_hash_table_replace (info_by_file, info->file, info);
257 258 259 260 261 262 263 264 265

      /*
       * We might need to keep a special copy of this for resolving .vala
       * builds which won't be able ot be matched based on the filename. We
       * keep all of them around right now in case we want to later on find
       * the closest match based on directory.
       */
      if (g_str_has_suffix (file, ".vala"))
        {
Christian Hergert's avatar
Christian Hergert committed
266
          info = g_slice_new0 (CompileInfo);
267 268 269 270 271
          info->file = g_file_resolve_relative_path (dir, file);
          info->directory = g_object_ref (dir);
          info->command = g_strdup (command);
          g_ptr_array_add (vala_info, info);
        }
272 273 274
    }

  self->info_by_file = g_steal_pointer (&info_by_file);
275
  self->vala_info = g_steal_pointer (&vala_info);
276

277
  ide_task_return_boolean (task, TRUE);
278 279 280 281 282 283 284

  IDE_EXIT;
}

/**
 * ide_compile_commands_load:
 * @self: An #IdeCompileCommands
285 286
 * @file: a #GFile
 * @cancellable: (nullable): a #GCancellable, or %NULL
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
 * @error: A location for a #GError, or %NULL
 *
 * Synchronously loads the contents of the requested @file and parses
 * the JSON command database contained within.
 *
 * You may only call this function once on an #IdeCompileCommands object.
 * If there is a failure, you must create a new #IdeCompileCommands instance
 * instead of calling this function again.
 *
 * See also: ide_compile_commands_load_async()
 *
 * Returns: %TRUE if successful; otherwise %FALSE and @error is set.
 *
 * Since: 3.28
 */
gboolean
ide_compile_commands_load (IdeCompileCommands  *self,
                           GFile               *file,
                           GCancellable        *cancellable,
                           GError             **error)
{
308
  g_autoptr(IdeTask) task = NULL;
309 310 311 312 313 314 315 316 317 318 319
  gboolean ret;

  IDE_ENTRY;

  g_return_val_if_fail (IDE_IS_COMPILE_COMMANDS (self), FALSE);
  g_return_val_if_fail (self->has_loaded == FALSE, FALSE);
  g_return_val_if_fail (G_IS_FILE (file), FALSE);
  g_return_val_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable), FALSE);

  self->has_loaded = TRUE;

320 321 322 323
  task = ide_task_new (self, cancellable, NULL, NULL);
  ide_task_set_priority (task, G_PRIORITY_LOW);
  ide_task_set_source_tag (task, ide_compile_commands_load);
  ide_task_set_task_data (task, g_object_ref (file), g_object_unref);
324
  ide_compile_commands_load_worker (task, self, file, cancellable);
325

326
  ret = ide_task_propagate_boolean (task, error);
327 328 329 330 331 332 333

  IDE_RETURN (ret);
}

/**
 * ide_compile_commands_load_async:
 * @self: An #IdeCompileCommands
334 335
 * @file: a #GFile
 * @cancellable: (nullable): a #GCancellable, or %NULL
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
 * @callback: the callback for the async operation
 * @user_data: user data for @callback
 *
 * Asynchronously loads the contents of the requested @file and parses
 * the JSON command database contained within.
 *
 * You may only call this function once on an #IdeCompileCommands object.
 * If there is a failure, you must create a new #IdeCompileCommands instance
 * instead of calling this function again.
 *
 * See also: ide_compile_commands_load_finish()
 *
 * Since: 3.28
 */
void
ide_compile_commands_load_async (IdeCompileCommands  *self,
                                 GFile               *file,
                                 GCancellable        *cancellable,
                                 GAsyncReadyCallback  callback,
                                 gpointer             user_data)
{
357
  g_autoptr(IdeTask) task = NULL;
358 359 360 361 362 363 364 365 366 367

  IDE_ENTRY;

  g_return_if_fail (IDE_IS_COMPILE_COMMANDS (self));
  g_return_if_fail (self->has_loaded == FALSE);
  g_return_if_fail (G_IS_FILE (file));
  g_return_if_fail (!cancellable || G_IS_CANCELLABLE (cancellable));

  self->has_loaded = TRUE;

368 369 370 371 372
  task = ide_task_new (self, cancellable, callback, user_data);
  ide_task_set_priority (task, G_PRIORITY_LOW);
  ide_task_set_source_tag (task, ide_compile_commands_load_async);
  ide_task_set_task_data (task, g_object_ref (file), g_object_unref);
  ide_task_run_in_thread (task, ide_compile_commands_load_worker);
373 374 375 376 377 378 379

  IDE_EXIT;
}

/**
 * ide_compile_commands_load_finish:
 * @self: An #IdeCompileCommands
380
 * @result: a #GAsyncResult provided to the callback
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
 * @error: A location for a #GError, or %NULL
 *
 * Completes an asynchronous request to ide_compile_commands_load_async().
 *
 * See also: ide_compile_commands_load_async()
 *
 * Returns: %TRUE if the file was loaded successfully; otherwise %FALSE
 *   and @error is set.
 *
 * Since: 3.28
 */
gboolean
ide_compile_commands_load_finish (IdeCompileCommands  *self,
                                  GAsyncResult        *result,
                                  GError             **error)
{
  gboolean ret;

  IDE_ENTRY;

  g_return_val_if_fail (IDE_IS_COMPILE_COMMANDS (self), FALSE);
402
  g_return_val_if_fail (IDE_IS_TASK (result), FALSE);
403

404
  ret = ide_task_propagate_boolean (IDE_TASK (result), error);
405 406 407 408

  IDE_RETURN (ret);
}

409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455
static gboolean
suffix_is_c_like (const gchar *suffix)
{
  if (suffix == NULL)
    return FALSE;

  return !!strstr (suffix, ".c") || !!strstr (suffix, ".h") ||
         !!strstr (suffix, ".cc") || !!strstr (suffix, ".hh") ||
         !!strstr (suffix, ".cxx") || !!strstr (suffix, ".hxx") ||
         !!strstr (suffix, ".cpp") || !!strstr (suffix, ".hpp");
}

static gboolean
suffix_is_vala (const gchar *suffix)
{
  if (suffix == NULL)
    return FALSE;

  return !!strstr (suffix, ".vala");
}

static gchar *
ide_compile_commands_resolve (IdeCompileCommands *self,
                              const CompileInfo  *info,
                              const gchar        *path)
{
  g_autoptr(GFile) file = NULL;

  g_assert (IDE_IS_COMPILE_COMMANDS (self));
  g_assert (info != NULL);

  if (path == NULL)
    return NULL;

  if (g_path_is_absolute (path))
    return g_strdup (path);

  file = g_file_resolve_relative_path (info->directory, path);
  if (file != NULL)
    return g_file_get_path (file);

  return NULL;
}

static void
ide_compile_commands_filter_c (IdeCompileCommands   *self,
                               const CompileInfo    *info,
456
                               const gchar * const  *system_includes,
457 458
                               gchar              ***argv)
{
459
  g_autoptr(GPtrArray) ar = NULL;
460 461 462 463 464

  g_assert (IDE_IS_COMPILE_COMMANDS (self));
  g_assert (info != NULL);
  g_assert (argv != NULL);

465 466 467
  if (*argv == NULL)
    return;

468 469 470 471 472 473 474
  ar = g_ptr_array_new_with_free_func (g_free);

  if (system_includes != NULL)
    {
      for (guint i = 0; system_includes[i]; i++)
        g_ptr_array_add (ar, g_strdup_printf ("-I%s", system_includes[i]));
    }
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497

  for (guint i = 0; (*argv)[i] != NULL; i++)
    {
      const gchar *param = (*argv)[i];
      const gchar *next = (*argv)[i+1];
      g_autofree gchar *resolved = NULL;

      if (param[0] != '-')
        continue;

      switch (param[1])
        {
        case 'I': /* -I/usr/include, -I /usr/include */
          if (param[2] != '\0')
            next = &param[2];
          resolved = ide_compile_commands_resolve (self, info, next);
          if (resolved != NULL)
            g_ptr_array_add (ar, g_strdup_printf ("-I%s", resolved));
          break;

        case 'f': /* -fPIC */
        case 'W': /* -Werror... */
        case 'm': /* -m64 -mtune=native */
498
        case 'O': /* -O2 */
499 500 501
          g_ptr_array_add (ar, g_strdup (param));
          break;

502 503 504 505
        case 'M': /* -MMD -MQ -MT -MF <file> */
          /* ignore the -M class of commands */
          break;

506 507 508 509 510 511 512 513
        case 'D': /* -DFOO, -D FOO */
        case 'x': /* -xc++ */
          g_ptr_array_add (ar, g_strdup (param));
          if (param[2] == '\0')
            g_ptr_array_add (ar, g_strdup (next));
          break;

        default:
514
          if (g_str_has_prefix (param, "-std=") ||
515
              dzl_str_equal0 (param, "-pthread") ||
516 517 518 519
              g_str_has_prefix (param, "-isystem"))
            {
              g_ptr_array_add (ar, g_strdup (param));
            }
520
          else if (next != NULL && dzl_str_equal0 (param, "-include"))
521 522
            {
              g_ptr_array_add (ar, g_strdup (param));
523
              g_ptr_array_add (ar, ide_compile_commands_resolve (self, info, next));
524
            }
525 526 527 528 529
          break;
        }
    }

  g_ptr_array_add (ar, NULL);
530 531 532

  g_strfreev (*argv);
  *argv = (gchar **)g_ptr_array_free (g_steal_pointer (&ar), FALSE);
533 534 535 536
}

static void
ide_compile_commands_filter_vala (IdeCompileCommands   *self,
537
                                  const CompileInfo    *info,
538 539
                                  gchar              ***argv)
{
540 541
  GPtrArray *ar;

542
  g_assert (IDE_IS_COMPILE_COMMANDS (self));
543
  g_assert (info != NULL);
544 545
  g_assert (argv != NULL);

546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
  if (*argv == NULL)
    return;

  ar = g_ptr_array_new ();

  for (guint i = 0; (*argv)[i] != NULL; i++)
    {
      const gchar *param = (*argv)[i];
      const gchar *next = (*argv)[i+1];

      if (g_str_has_prefix (param, "--pkg=") ||
          g_str_has_prefix (param, "--target-glib=") ||
          !!strstr (param, ".vapi"))
        {
          g_ptr_array_add (ar, g_strdup (param));
        }
      else if (g_str_has_prefix (param, "--vapidir=") ||
               g_str_has_prefix (param, "--girdir=") ||
               g_str_has_prefix (param, "--metadatadir="))
        {
          g_autofree gchar *resolved = NULL;
          gchar *eq = strchr (param, '=');

          next = eq + 1;
          *eq = '\0';

          resolved = ide_compile_commands_resolve (self, info, next);
          g_ptr_array_add (ar, g_strdup_printf ("%s=%s", param, resolved));
        }
      else if (next != NULL &&
               (g_str_has_prefix (param, "--pkg") ||
                g_str_has_prefix (param, "--target-glib")))
        {
          g_ptr_array_add (ar, g_strdup (param));
          g_ptr_array_add (ar, g_strdup (next));
          i++;
        }
      else if (next != NULL &&
               (g_str_has_prefix (param, "--vapidir") ||
                g_str_has_prefix (param, "--girdir") ||
                g_str_has_prefix (param, "--metadatadir")))
        {
          g_ptr_array_add (ar, g_strdup (param));
          g_ptr_array_add (ar, ide_compile_commands_resolve (self, info, next));
          i++;
        }
    }

  g_free (*argv);

  g_ptr_array_add (ar, NULL);
  *argv = (gchar **)g_ptr_array_free (ar, FALSE);
598 599
}

600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
static const CompileInfo *
find_with_alternates (IdeCompileCommands *self,
                      GFile              *file)
{
  const CompileInfo *info;

  g_assert (IDE_IS_COMPILE_COMMANDS (self));
  g_assert (G_IS_FILE (file));

  if (self->info_by_file == NULL)
    return NULL;

  if (NULL != (info = g_hash_table_lookup (self->info_by_file, file)))
    return info;

  {
    g_autofree gchar *path = g_file_get_path (file);
    gsize len = strlen (path);

619 620 621 622 623 624 625 626 627 628 629 630 631 632
    if (g_str_has_suffix (path, "-private.h"))
      {
        g_autofree gchar *other_path = NULL;
        g_autoptr(GFile) other = NULL;

        path[len - strlen ("-private.h")] = 0;

        other_path = g_strconcat (path, ".c", NULL);
        other = g_file_new_for_path (other_path);

        if (NULL != (info = g_hash_table_lookup (self->info_by_file, other)))
          return info;
      }
    else if (g_str_has_suffix (path, ".h"))
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
      {
        static const gchar *tries[] = { "c", "cc", "cpp" };
        path[--len] = 0;

        for (guint i = 0; i < G_N_ELEMENTS (tries); i++)
          {
            g_autofree gchar *other_path = g_strconcat (path, tries[i], NULL);
            g_autoptr(GFile) other = g_file_new_for_path (other_path);

            if (NULL != (info = g_hash_table_lookup (self->info_by_file, other)))
              return info;
          }
      }
  }

  return NULL;
}

651 652 653
/**
 * ide_compile_commands_lookup:
 * @self: An #IdeCompileCommands
654
 * @file: a #GFile representing the file to lookup
655
 * @system_includes: system include dirs if any
656 657 658 659 660 661 662 663 664 665 666 667 668 669
 * @directory: (out) (optional) (transfer full): A location for a #GFile, or %NULL
 * @error: A location for a #GError, or %NULL
 *
 * Locates the commands to compile the @file requested.
 *
 * If @directory is non-NULL, then the directory to run the command from
 * is placed in @directory.
 *
 * Returns: (nullable) (transfer full): A string array or %NULL if
 *   there was a failure to locate or parse the command.
 *
 * Since: 3.28
 */
gchar **
670 671 672 673 674
ide_compile_commands_lookup (IdeCompileCommands   *self,
                             GFile                *file,
                             const gchar * const  *system_includes,
                             GFile               **directory,
                             GError              **error)
675
{
676
  g_autofree gchar *base = NULL;
677 678
  const CompileInfo *info;
  const gchar *dot;
679 680 681 682

  g_return_val_if_fail (IDE_IS_COMPILE_COMMANDS (self), NULL);
  g_return_val_if_fail (G_IS_FILE (file), NULL);

683 684 685
  base = g_file_get_basename (file);
  dot = strrchr (base, '.');

686
  if (NULL != (info = find_with_alternates (self, file)))
687
    {
688 689
      g_auto(GStrv) argv = NULL;
      gint argc = 0;
690

691 692 693
      if (!g_shell_parse_argv (info->command, &argc, &argv, error))
        return NULL;

694
      if (suffix_is_c_like (dot))
695
        ide_compile_commands_filter_c (self, info, system_includes, &argv);
696
      else if (suffix_is_vala (dot))
697
        ide_compile_commands_filter_vala (self, info, &argv);
698

699 700 701 702 703 704
      if (directory != NULL)
        *directory = g_object_ref (info->directory);

      return g_steal_pointer (&argv);
    }

705 706 707 708 709 710
  /*
   * Some compile-commands databases will give us info about .vala, but there
   * may only be a single valac command to run. While we parsed the JSON
   * document we stored information about each of the Vala files in a special
   * list for exactly this purpose.
   */
711
  if (dzl_str_equal0 (dot, ".vala") && self->vala_info != NULL)
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731
    {
      for (guint i = 0; i < self->vala_info->len; i++)
        {
          g_auto(GStrv) argv = NULL;
          gint argc = 0;

          info = g_ptr_array_index (self->vala_info, i);

          if (!g_shell_parse_argv (info->command, &argc, &argv, NULL))
            continue;

          ide_compile_commands_filter_vala (self, info, &argv);

          if (directory != NULL)
            *directory = g_object_ref (info->directory);

          return g_steal_pointer (&argv);
        }
    }

732 733 734 735 736 737 738
  g_set_error_literal (error,
                       G_IO_ERROR,
                       G_IO_ERROR_NOT_FOUND,
                       "Failed to locate command for requested file");

  return NULL;
}