File: Configuration.h

package info (click to toggle)
bear 3.1.6-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,904 kB
  • sloc: cpp: 9,184; sh: 706; ansic: 497; python: 175; makefile: 29
file content (111 lines) | stat: -rw-r--r-- 4,079 bytes parent folder | download | duplicates (2)
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
/*  Copyright (C) 2012-2024 by László Nagy
    This file is part of Bear.

    Bear is a tool to generate compilation database for clang tooling.

    Bear 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.

    Bear 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/>.
 */

#pragma once

#include <libresult/Result.h>

#include <filesystem>
#include <iosfwd>
#include <list>
#include <map>
#include <string>
#include <optional>
#include <utility>

namespace fs = std::filesystem;

namespace cs {

    static const std::string DUPLICATE_FILE = "file";
    static const std::string DUPLICATE_FILE_OUTPUT = "file_output";
    static const std::string DUPLICATE_ALL = "all";

    // Controls the output format.
    //
    // The entries in the JSON compilation database can have different forms.
    // One format element is how the command is represented: it can be an array
    // of strings or a single string (shell escaping to protect white spaces).
    // Another format element is if the output field is emitted or not.
    struct Format {
        bool command_as_array = true;
        bool drop_output_field = false;
    };

    // Controls the content of the output.
    //
    // This will act as a filter on the output elements.
    // These attributes can be read from the configuration file, and can be
    // overridden by command line arguments.
    struct Content {
        bool include_only_existing_source = false;
        std::string duplicate_filter_fields = DUPLICATE_FILE_OUTPUT;
        std::list<fs::path> paths_to_include = {};
        std::list<fs::path> paths_to_exclude = {};
    };

    // Groups together the output related configurations.
    struct Output {
        Format format;
        Content content;
    };

    // Represents a compiler wrapper that the tool will recognize.
    //
    // When executable name matches it tries to parse the flags as it would
    // be a known compiler, and append the additional flags to the output
    // entry if the compiler is recognized.
    struct CompilerWrapper {
        fs::path executable;
        std::list<std::string> flags_to_add;
        std::list<std::string> flags_to_remove;
    };

    // Represents compiler related configuration.
    struct Compilation {
        std::list<CompilerWrapper> compilers_to_recognize;
        std::list<fs::path> compilers_to_exclude;
    };

    // Represents the application configuration.
    struct Configuration {
        Output output;
        Compilation compilation;
    };

    // Convenient methods for these types.
    std::ostream& operator<<(std::ostream&, const Format&);
    std::ostream& operator<<(std::ostream&, const Content&);
    std::ostream& operator<<(std::ostream&, const Output&);
    std::ostream& operator<<(std::ostream&, const CompilerWrapper&);
    std::ostream& operator<<(std::ostream&, const Compilation&);
    std::ostream& operator<<(std::ostream&, const Configuration&);

    // Utility class to persists configuration in JSON.
    struct ConfigurationSerializer {
        virtual ~ConfigurationSerializer() noexcept = default;

        // Serialization methods with error mapping.
        [[nodiscard]] virtual rust::Result<size_t> to_json(const fs::path &, const Configuration &rhs) const;
        [[nodiscard]] virtual rust::Result<size_t> to_json(std::ostream &ostream, const Configuration &rhs) const;

        [[nodiscard]] virtual rust::Result<Configuration> from_json(const fs::path &) const;
        [[nodiscard]] virtual rust::Result<Configuration> from_json(std::istream &istream) const;
    };
}