summaryrefslogtreecommitdiff
path: root/src/Result.hpp
blob: 01068c9e6f06e25d3fccc249038b85c8df17177d (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
// Copyright (C) 2019-2020 Joel Rosdahl and other contributors
//
// See doc/AUTHORS.adoc for a complete list of contributors.
//
// 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, write to the Free Software Foundation, Inc., 51
// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

#pragma once

#include "system.hpp"

#include "third_party/nonstd/optional.hpp"

#include <map>
#include <string>
#include <vector>

class CacheEntryReader;
class CacheEntryWriter;
class Context;

namespace Result {

extern const std::string k_file_suffix;
extern const uint8_t k_magic[4];
extern const uint8_t k_version;

extern const char* const k_unknown_file_type;

using UnderlyingFileTypeInt = uint8_t;
enum class FileType : UnderlyingFileTypeInt {
  // These values are written into the cache result file. This means they must
  // never be changed or removed unless the result file version is incremented.
  // Adding new values is OK.

  // The main output specified with -o or implicitly from the input filename.
  object = 0,

  // Dependency file specified with -MF or implicitly from the output filename.
  dependency = 1,

  // Text sent to standard output.
  stderr_output = 2,

  // Coverage notes file generated by -ftest-coverage with filename in unmangled
  // form, i.e. output file but with a .gcno extension.
  coverage_unmangled = 3,

  // Stack usage file generated by -fstack-usage, i.e. output file but with a
  // .su extension.
  stackusage = 4,

  // Diagnostics output file specified by --serialize-diagnostics.
  diagnostic = 5,

  // DWARF object file geenrated by -gsplit-dwarf, i.e. output file but with a
  // .dwo extension.
  dwarf_object = 6,

  // Coverage notes file generated by -ftest-coverage with filename in mangled
  // form, i.e. full output file path but with a .gcno extension and with
  // slashes replaced with hashes.
  coverage_mangled = 7,
};

const char* file_type_to_string(FileType type);

std::string gcno_file_in_mangled_form(const Context& ctx);
std::string gcno_file_in_unmangled_form(const Context& ctx);

// This class knows how to read a result cache entry.
class Reader
{
public:
  Reader(const std::string& result_path);

  class Consumer
  {
  public:
    virtual ~Consumer() = default;

    virtual void on_header(CacheEntryReader& cache_entry_reader) = 0;
    virtual void on_entry_start(uint32_t entry_number,
                                FileType file_type,
                                uint64_t file_len,
                                nonstd::optional<std::string> raw_file) = 0;
    virtual void on_entry_data(const uint8_t* data, size_t size) = 0;
    virtual void on_entry_end() = 0;
  };

  // Returns error message on error, otherwise nonstd::nullopt.
  nonstd::optional<std::string> read(Consumer& consumer);

private:
  const std::string m_result_path;

  bool read_result(Consumer& consumer);
  void read_entry(CacheEntryReader& cache_entry_reader,
                  uint32_t entry_number,
                  Reader::Consumer& consumer);
};

// This class knows how to write a result cache entry.
class Writer
{
public:
  Writer(Context& ctx, const std::string& result_path);

  // Register a file to include in the result. Does not throw.
  void write(FileType file_type, const std::string& file_path);

  // Write registered files to the result. Returns an error message on error.
  nonstd::optional<std::string> finalize();

private:
  Context& m_ctx;
  const std::string m_result_path;
  std::vector<std::pair<FileType, std::string>> m_entries_to_write;

  void do_finalize();
  static void write_embedded_file_entry(CacheEntryWriter& writer,
                                        const std::string& path,
                                        uint64_t file_size);
  void write_raw_file_entry(const std::string& path, uint32_t entry_number);
};

} // namespace Result