File: names.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (104 lines) | stat: -rw-r--r-- 2,806 bytes parent folder | download | duplicates (6)
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
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC.  All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd

#include "upb_generator/c/names.h"

#include <array>
#include <cstdint>
#include <string>
#include <vector>

#include "absl/container/flat_hash_map.h"
#include "absl/strings/match.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_join.h"
#include "absl/strings/str_replace.h"
#include "absl/strings/str_split.h"
#include "absl/strings/string_view.h"
#include "upb_generator/c/names_internal.h"

namespace upb {
namespace generator {

namespace {

std::string ToCIdent(absl::string_view str) {
  return absl::StrReplaceAll(str, {{".", "_"}, {"/", "_"}, {"-", "_"}});
}

}  // namespace

std::string CApiHeaderFilename(absl::string_view proto_filename) {
  return CApiHeaderFilename(proto_filename, false);
}

std::string CApiMessageType(absl::string_view full_name) {
  return ToCIdent(full_name);
}

std::string CApiEnumType(absl::string_view full_name) {
  return ToCIdent(full_name);
}

std::string CApiEnumValueSymbol(absl::string_view full_name) {
  return ToCIdent(full_name);
}

std::string CApiExtensionIdentBase(absl::string_view full_name) {
  std::vector<std::string> parts = absl::StrSplit(full_name, '.');
  parts.pop_back();
  return ToCIdent(absl::StrJoin(parts, "."));
}

std::string CApiOneofIdentBase(absl::string_view full_name) {
  return ToCIdent(full_name);
}

namespace {

struct Prefix {
  absl::string_view name;
  uint32_t conflict_set;
};

constexpr uint32_t kAnyField = UINT32_MAX;

// Prefixes used by C code generator for field access.
static constexpr std::array<Prefix, 6> kPrefixes{
    Prefix{"clear_", kContainerField | kStringField},
    Prefix{"delete_", kContainerField},
    Prefix{"add_", kContainerField},
    Prefix{"resize_", kContainerField},
    Prefix{"set_", kAnyField},
    Prefix{"has_", kAnyField},
};

bool HasConflict(absl::string_view name,
                 const absl::flat_hash_map<std::string, FieldClass>& fields) {
  for (const auto& prefix : kPrefixes) {
    if (!absl::StartsWith(name, prefix.name)) continue;
    auto match = fields.find(name.substr(prefix.name.size()));
    if (match == fields.end()) continue;
    if (prefix.conflict_set & match->second) return true;
  }
  return false;
}

}  // namespace

NameMangler::NameMangler(
    const absl::flat_hash_map<std::string, FieldClass>& fields) {
  for (const auto& pair : fields) {
    const std::string& field_name = pair.first;
    if (HasConflict(field_name, fields)) {
      names_.emplace(field_name, absl::StrCat(field_name, "_"));
    }
  }
}

}  // namespace generator
}  // namespace upb