~ubuntu-branches/ubuntu/trusty/protobuf/trusty-proposed

« back to all changes in this revision

Viewing changes to src/google/protobuf/compiler/mock_code_generator.cc

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2011-05-31 14:41:47 UTC
  • mfrom: (2.2.8 sid)
  • Revision ID: james.westby@ubuntu.com-20110531144147-s41g5fozgvyo462l
Tags: 2.4.0a-2ubuntu1
* Merge with Debian; remaining changes:
  - Fix linking with -lpthread.

Show diffs side-by-side

added added

removed removed

Lines of Context:
45
45
namespace protobuf {
46
46
namespace compiler {
47
47
 
 
48
// Returns the list of the names of files in all_files in the form of a
 
49
// comma-separated string.
 
50
string CommaSeparatedList(const vector<const FileDescriptor*> all_files) {
 
51
  vector<string> names;
 
52
  for (int i = 0; i < all_files.size(); i++) {
 
53
    names.push_back(all_files[i]->name());
 
54
  }
 
55
  return JoinStrings(names, ",");
 
56
}
 
57
 
48
58
static const char* kFirstInsertionPointName = "first_mock_insertion_point";
49
59
static const char* kSecondInsertionPointName = "second_mock_insertion_point";
50
60
static const char* kFirstInsertionPoint =
63
73
    const string& insertions,
64
74
    const string& file,
65
75
    const string& first_message_name,
 
76
    const string& first_parsed_file_name,
66
77
    const string& output_directory) {
67
78
  string content;
68
79
  ASSERT_TRUE(File::ReadFileToString(
84
95
  }
85
96
 
86
97
  ASSERT_EQ(lines.size(), 3 + insertion_list.size() * 2);
87
 
  EXPECT_EQ(GetOutputFileContent(name, parameter, file, first_message_name),
 
98
  EXPECT_EQ(GetOutputFileContent(name, parameter, file,
 
99
                                 first_parsed_file_name, first_message_name),
88
100
            lines[0]);
89
101
 
90
102
  EXPECT_EQ(kFirstInsertionPoint, lines[1 + insertion_list.size()]);
92
104
 
93
105
  for (int i = 0; i < insertion_list.size(); i++) {
94
106
    EXPECT_EQ(GetOutputFileContent(insertion_list[i], "first_insert",
95
 
                                   file, first_message_name),
 
107
                                   file, file, first_message_name),
96
108
              lines[1 + i]);
97
109
    // Second insertion point is indented, so the inserted text should
98
110
    // automatically be indented too.
99
111
    EXPECT_EQ("  " + GetOutputFileContent(insertion_list[i], "second_insert",
100
 
                                          file, first_message_name),
 
112
                                          file, file, first_message_name),
101
113
              lines[2 + insertion_list.size() + i]);
102
114
  }
103
115
}
105
117
bool MockCodeGenerator::Generate(
106
118
    const FileDescriptor* file,
107
119
    const string& parameter,
108
 
    OutputDirectory* output_directory,
 
120
    GeneratorContext* context,
109
121
    string* error) const {
110
122
  for (int i = 0; i < file->message_type_count(); i++) {
111
123
    if (HasPrefixString(file->message_type(i)->name(), "MockCodeGenerator_")) {
134
146
    for (int i = 0; i < insert_into.size(); i++) {
135
147
      {
136
148
        scoped_ptr<io::ZeroCopyOutputStream> output(
137
 
            output_directory->OpenForInsert(
 
149
            context->OpenForInsert(
138
150
              GetOutputFileName(insert_into[i], file),
139
151
              kFirstInsertionPointName));
140
152
        io::Printer printer(output.get(), '$');
141
 
        printer.PrintRaw(GetOutputFileContent(name_, "first_insert", file));
 
153
        printer.PrintRaw(GetOutputFileContent(name_, "first_insert",
 
154
                                              file, context));
142
155
        if (printer.failed()) {
143
156
          *error = "MockCodeGenerator detected write error.";
144
157
          return false;
147
160
 
148
161
      {
149
162
        scoped_ptr<io::ZeroCopyOutputStream> output(
150
 
            output_directory->OpenForInsert(
 
163
            context->OpenForInsert(
151
164
              GetOutputFileName(insert_into[i], file),
152
165
              kSecondInsertionPointName));
153
166
        io::Printer printer(output.get(), '$');
154
 
        printer.PrintRaw(GetOutputFileContent(name_, "second_insert", file));
 
167
        printer.PrintRaw(GetOutputFileContent(name_, "second_insert",
 
168
                                              file, context));
155
169
        if (printer.failed()) {
156
170
          *error = "MockCodeGenerator detected write error.";
157
171
          return false;
160
174
    }
161
175
  } else {
162
176
    scoped_ptr<io::ZeroCopyOutputStream> output(
163
 
        output_directory->Open(GetOutputFileName(name_, file)));
 
177
        context->Open(GetOutputFileName(name_, file)));
164
178
 
165
179
    io::Printer printer(output.get(), '$');
166
 
    printer.PrintRaw(GetOutputFileContent(name_, parameter, file));
 
180
    printer.PrintRaw(GetOutputFileContent(name_, parameter,
 
181
                                          file, context));
167
182
    printer.PrintRaw(kFirstInsertionPoint);
168
183
    printer.PrintRaw(kSecondInsertionPoint);
169
184
 
186
201
  return file + ".MockCodeGenerator." + generator_name;
187
202
}
188
203
 
189
 
string MockCodeGenerator::GetOutputFileContent(const string& generator_name,
190
 
                                               const string& parameter,
191
 
                                               const FileDescriptor* file) {
 
204
string MockCodeGenerator::GetOutputFileContent(
 
205
    const string& generator_name,
 
206
    const string& parameter,
 
207
    const FileDescriptor* file,
 
208
    GeneratorContext *context) {
 
209
  vector<const FileDescriptor*> all_files;
 
210
  context->ListParsedFiles(&all_files);
192
211
  return GetOutputFileContent(
193
212
      generator_name, parameter, file->name(),
 
213
      CommaSeparatedList(all_files),
194
214
      file->message_type_count() > 0 ?
195
215
          file->message_type(0)->name() : "(none)");
196
216
}
199
219
    const string& generator_name,
200
220
    const string& parameter,
201
221
    const string& file,
 
222
    const string& parsed_file_list,
202
223
    const string& first_message_name) {
203
 
  return strings::Substitute("$0: $1, $2, $3\n",
204
 
      generator_name, parameter, file, first_message_name);
 
224
  return strings::Substitute("$0: $1, $2, $3, $4\n",
 
225
      generator_name, parameter, file,
 
226
      first_message_name, parsed_file_list);
205
227
}
206
228
 
207
229
}  // namespace compiler