Skip to content

Commit

Permalink
Fix implicit outputs.
Browse files Browse the repository at this point in the history
  • Loading branch information
dillof committed Jan 8, 2024
1 parent 9c902a8 commit 01043ca
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 7 deletions.
32 changes: 26 additions & 6 deletions src/Text.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,37 @@ IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "Text.h"


#include "Exception.h"
#include "File.h"
#include <Exception.h>

// clang-format: off
const std::unordered_map<Tokenizer::TokenType, Text::ElementType> Text::typemap = {
{ Tokenizer::TokenType::IMPLICIT_DEPENDENCY, ElementType::PUNCTUATION },
{ Tokenizer::TokenType::ORDER_DEPENDENCY, ElementType::PUNCTUATION },
{ Tokenizer::TokenType::SPACE, ElementType::WHITESPACE },
{ Tokenizer::TokenType::VALIDATION_DEPENDENCY, ElementType::PUNCTUATION },
};

// clang-format: on

Text::Element::Element(const Tokenizer::Token& token) {
const auto it = typemap.find(token.type);
if (it != typemap.end()) {
type = it->second;
}
else {
type = ElementType::WORD;
}
value = token.string();
}

std::string Text::Element::string() const {
if (!is_word() && !is_file()) {
return value;
}
else {
auto result = std::string{};
auto index = size_t{0};
auto index = size_t{ 0 };

while (index < value.size()) {
const auto special = value.find_first_of("$ \n", index);
Expand Down Expand Up @@ -98,7 +118,7 @@ Text::Text(Tokenizer& tokenizer, Tokenizer::TokenType terminator) {
}
// fallthrough
default:
emplace_back((token.is_whitespace() ? ElementType::WHITESPACE : ElementType::WORD), token.string());
emplace_back(token);
break;
}
}
Expand Down Expand Up @@ -148,7 +168,7 @@ void Text::collect_words(std::unordered_set<std::string>& words) const {
element.variable->value.collect_words(words);
}
}
else {
else if (element.is_word() || element.is_build_file()) {
words.insert(element.value);
}
}
Expand All @@ -157,7 +177,7 @@ void Text::collect_words(std::unordered_set<std::string>& words) const {
std::string Text::string() const {
auto result = std::string{};

for (auto& element: *this) {
for (auto& element : *this) {
result += element.string();
}

Expand Down
7 changes: 6 additions & 1 deletion src/Text.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,14 @@ class Text {
enum class ElementType {
BUILD_FILE,
SOURCE_FILE,
PUNCTUATION,
VARIABLE,
WHITESPACE,
WORD
};
class Element {
public:
Element(const Tokenizer::Token& token);
Element(ElementType type, std::string value) : type{type}, value{ std::move(value) } {}

[[nodiscard]] bool is_build_file() const {return type == ElementType::BUILD_FILE;}
Expand All @@ -72,7 +74,7 @@ class Text {

ElementType type;
std::string value;
const Variable *variable = nullptr;
const Variable *variable{};
};

Text() = default;
Expand All @@ -82,6 +84,7 @@ class Text {

void append(const Text& other) {elements.insert(elements.end(), other.elements.begin(), other.elements.end());}
void emplace_back(ElementType type, std::string value) { elements.emplace_back(type, std::move(value)); }
void emplace_back(const Element& element) { elements.emplace_back(element); }

void print(std::ostream& stream) const;
void process(const File& file);
Expand All @@ -103,6 +106,8 @@ class Text {

private:
std::vector<Element> elements;

static const std::unordered_map<Tokenizer::TokenType, ElementType> typemap;
};

std::ostream& operator<<(std::ostream& stream, const Text& text);
Expand Down
29 changes: 29 additions & 0 deletions tests/implicit-output.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
arguments ..
file input <>
input
end-of-inline-data
file build.fninja <>
rule a
command = a $in $out
flags = --verbose

build output | implicit: a input | output
end-of-inline-data
file build/build.ninja {} <>
top_source_directory = ..
source_directory = ..
top_build_directory = .
build_directory = .

rule a
command = a $in $out
flags = --verbose

rule fast-ninja
command = fast-ninja $top_source_directory
generator = 1

build $build_directory/output | $build_directory/implicit : a $source_directory/input | $build_directory/output

build ./build.ninja : fast-ninja ../build.fninja
end-of-inline-data

0 comments on commit 01043ca

Please sign in to comment.