iree/base/internal/flags.c
static iree_status_t iree_flags_parse_file(iree_string_view_t file_path) {
  // Read file contents.
  // NOTE: we intentionally leak the contents here so that the flags remain in
  // memory in case they are referenced.
  // NOTE: safe to use file_path.data here as it will always have a NUL
  // terminator.
  iree_allocator_t allocator = iree_flags_leaky_allocator();
  iree_byte_span_t file_contents;
  IREE_RETURN_IF_ERROR(
      iree_file_read_contents(file_path.data, allocator, &file_contents),
      "while trying to parse flagfile");

  // Run through the file line-by-line.
  int line_number = 0;
  iree_string_view_t contents = iree_make_string_view(
      (const char*)file_contents.data, file_contents.data_length);
  while (!iree_string_view_is_empty(contents)) {
    // Split into a single line and the entire rest of the file contents.
    iree_string_view_t line;
    iree_string_view_split(contents, '\n', &line, &contents);
    ++line_number;

    // Strip whitespace.
    line = iree_string_view_trim(line);
    if (iree_string_view_is_empty(line)) continue;

    // Ignore comments.
    if (iree_string_view_starts_with(line, iree_make_cstring_view("#")) ||
        iree_string_view_starts_with(line, iree_make_cstring_view("//"))) {
      continue;
    }

    // Strip `--`.
    if (!iree_string_view_starts_with(line, iree_make_cstring_view("--"))) {
      // Positional arguments can't be specified in flag files.
      return iree_make_status(
          IREE_STATUS_INVALID_ARGUMENT,
          "%.*s:%d: positional arguments not allowed in flag files",
          (int)file_path.size, file_path.data, line_number);
    }
    line = iree_string_view_remove_prefix(line, 2);

    // Split into `flag_name` = `flag_value`.
    iree_string_view_t flag_name;
    iree_string_view_t flag_value;
    iree_string_view_split(line, '=', &flag_name, &flag_value);
    flag_name = iree_string_view_trim(flag_name);
    flag_value = iree_string_view_trim(flag_value);

    // Lookup the flag by name.
    iree_flag_t* flag = iree_flag_lookup(flag_name);
    if (!flag) {
      return iree_make_status(IREE_STATUS_INVALID_ARGUMENT,
                              "%.*s:%d: flag '%.*s' not recognized",
                              (int)file_path.size, file_path.data, line_number,
                              (int)flag_name.size, flag_name.data);
    }

    // Parse the flag value.
    IREE_RETURN_IF_ERROR(iree_flag_parse(flag, flag_value),
                         "%.*s:%d: while parsing flag '%.*s'",
                         (int)file_path.size, file_path.data, line_number,
                         (int)line.size, line.data);
  }

  // NOTE: we intentionally leak the memory as flags may continue to reference
  // segments of it for their string values.
  return iree_ok_status();
}
// Parses a newline-separated list of flags from a file.
Parses a newline-separated list of flags from a file.
Parses a newline - separated list of flags from a file .
static iree_status_t iree_flags_parse_file(iree_string_view_t file_path) {
  iree_allocator_t allocator = iree_flags_leaky_allocator();
  iree_byte_span_t file_contents;
  IREE_RETURN_IF_ERROR(
      iree_file_read_contents(file_path.data, allocator, &file_contents),
      "while trying to parse flagfile");
  int line_number = 0;
  iree_string_view_t contents = iree_make_string_view(
      (const char*)file_contents.data, file_contents.data_length);
  while (!iree_string_view_is_empty(contents)) {
    iree_string_view_t line;
    iree_string_view_split(contents, '\n', &line, &contents);
    ++line_number;
    line = iree_string_view_trim(line);
    if (iree_string_view_is_empty(line)) continue;
    if (iree_string_view_starts_with(line, iree_make_cstring_view("#")) ||
        iree_string_view_starts_with(line, iree_make_cstring_view("//"))) {
      continue;
    }
    if (!iree_string_view_starts_with(line, iree_make_cstring_view("--"))) {
      return iree_make_status(
          IREE_STATUS_INVALID_ARGUMENT,
          "%.*s:%d: positional arguments not allowed in flag files",
          (int)file_path.size, file_path.data, line_number);
    }
    line = iree_string_view_remove_prefix(line, 2);
    iree_string_view_t flag_name;
    iree_string_view_t flag_value;
    iree_string_view_split(line, '=', &flag_name, &flag_value);
    flag_name = iree_string_view_trim(flag_name);
    flag_value = iree_string_view_trim(flag_value);
    iree_flag_t* flag = iree_flag_lookup(flag_name);
    if (!flag) {
      return iree_make_status(IREE_STATUS_INVALID_ARGUMENT,
                              "%.*s:%d: flag '%.*s' not recognized",
                              (int)file_path.size, file_path.data, line_number,
                              (int)flag_name.size, flag_name.data);
    }
    IREE_RETURN_IF_ERROR(iree_flag_parse(flag, flag_value),
                         "%.*s:%d: while parsing flag '%.*s'",
                         (int)file_path.size, file_path.data, line_number,
                         (int)line.size, line.data);
  }
  return iree_ok_status();
}
static iree_status_t iree_flags_parse_file ( iree_string_view_t file_path ) { iree_allocator_t allocator = iree_flags_leaky_allocator ( ) ; iree_byte_span_t file_contents ; IREE_RETURN_IF_ERROR ( iree_file_read_contents ( file_path . data , allocator , & file_contents ) , " " ) ; int line_number = 0 ; iree_string_view_t contents = iree_make_string_view ( ( const char * ) file_contents . data , file_contents . data_length ) ; while ( ! iree_string_view_is_empty ( contents ) ) { iree_string_view_t line ; iree_string_view_split ( contents , ' \n ' , & line , & contents ) ; ++ line_number ; line = iree_string_view_trim ( line ) ; if ( iree_string_view_is_empty ( line ) ) continue ; if ( iree_string_view_starts_with ( line , iree_make_cstring_view ( " " ) ) || iree_string_view_starts_with ( line , iree_make_cstring_view ( " " ) ) ) { continue ; } if ( ! iree_string_view_starts_with ( line , iree_make_cstring_view ( " " ) ) ) { return iree_make_status ( IREE_STATUS_INVALID_ARGUMENT , " " , ( int ) file_path . size , file_path . data , line_number ) ; } line = iree_string_view_remove_prefix ( line , 2 ) ; iree_string_view_t flag_name ; iree_string_view_t flag_value ; iree_string_view_split ( line , ' ' , & flag_name , & flag_value ) ; flag_name = iree_string_view_trim ( flag_name ) ; flag_value = iree_string_view_trim ( flag_value ) ; iree_flag_t * flag = iree_flag_lookup ( flag_name ) ; if ( ! flag ) { return iree_make_status ( IREE_STATUS_INVALID_ARGUMENT , " " , ( int ) file_path . size , file_path . data , line_number , ( int ) flag_name . size , flag_name . data ) ; } IREE_RETURN_IF_ERROR ( iree_flag_parse ( flag , flag_value ) , " " , ( int ) file_path . size , file_path . data , line_number , ( int ) line . size , line . data ) ; } return iree_ok_status ( ) ; }
Parses a newline-separated list of flags from a file.
Parses a newline - separated list of flags from a file .
// Read file contents. // NOTE: we intentionally leak the contents here so that the flags remain in // memory in case they are referenced. // NOTE: safe to use file_path.data here as it will always have a NUL // terminator. // Run through the file line-by-line. // Split into a single line and the entire rest of the file contents. // Strip whitespace. // Ignore comments. // Strip `--`. // Positional arguments can't be specified in flag files. // Split into `flag_name` = `flag_value`. // Lookup the flag by name. // Parse the flag value. // NOTE: we intentionally leak the memory as flags may continue to reference // segments of it for their string values.