Skip to content

Commit

Permalink
clang-tidy applied and CMake installation of the single header added (#…
Browse files Browse the repository at this point in the history
…287)

* Fixed headers and linter marks applied

Headers were wrong.
Applied some linter suggestions. There are still many left.

* CMake adjusted for single header

CMake can now install the single header.
Removed an unneeded header.

---------

Co-authored-by: Berscheid <[email protected]>
  • Loading branch information
Spixmaster and pantor authored Oct 5, 2024
1 parent 1281271 commit 92994be
Show file tree
Hide file tree
Showing 17 changed files with 163 additions and 71 deletions.
21 changes: 16 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ project(inja LANGUAGES CXX VERSION 3.4.0)

option(INJA_USE_EMBEDDED_JSON "Use the shipped json header if not available on the system" ON)
option(INJA_INSTALL "Generate install targets for inja" ON)
option(INJA_INSTALL_SINGLE_HEADER "Install the single header instead" OFF)
option(INJA_EXPORT "Export the current build tree to the package registry" ON)
option(BUILD_TESTING "Build unit tests" ON)
option(INJA_BUILD_TESTS "Build unit tests when BUILD_TESTING is enabled." ON)
Expand Down Expand Up @@ -166,18 +167,28 @@ if(INJA_INSTALL)
NO_CHECK_REQUIRED_COMPONENTS_MACRO
)

install(
DIRECTORY include/inja
DESTINATION ${INJA_INSTALL_INCLUDE_DIR}
FILES_MATCHING PATTERN "*.hpp"
)
if(INJA_INSTALL_SINGLE_HEADER)
install(
DIRECTORY single_include/inja
DESTINATION ${INJA_INSTALL_INCLUDE_DIR}
FILES_MATCHING PATTERN "*.hpp"
)
else()
install(
DIRECTORY include/inja
DESTINATION ${INJA_INSTALL_INCLUDE_DIR}
FILES_MATCHING PATTERN "*.hpp"
)
endif()

if(INJA_USE_EMBEDDED_JSON)
install(
DIRECTORY third_party/include/nlohmann
DESTINATION ${INJA_INSTALL_INCLUDE_DIR}
FILES_MATCHING PATTERN "*.hpp"
)
endif()

install(
FILES
"${CMAKE_CURRENT_BINARY_DIR}/${INJA_CONFIG_PATH}/injaConfig.cmake"
Expand Down
3 changes: 1 addition & 2 deletions include/inja/environment.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@

#include <fstream>
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#include <string_view>

#include "config.hpp"
#include "function_storage.hpp"
#include "inja.hpp"
#include "parser.hpp"
#include "renderer.hpp"
#include "template.hpp"
#include "utils.hpp"

namespace inja {

Expand Down
1 change: 1 addition & 0 deletions include/inja/exceptions.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#ifndef INCLUDE_INJA_EXCEPTIONS_HPP_
#define INCLUDE_INJA_EXCEPTIONS_HPP_

#include <cstddef>
#include <stdexcept>
#include <string>

Expand Down
6 changes: 6 additions & 0 deletions include/inja/function_storage.hpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
#ifndef INCLUDE_INJA_FUNCTION_STORAGE_HPP_
#define INCLUDE_INJA_FUNCTION_STORAGE_HPP_

#include <functional>
#include <map>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

#include "inja.hpp"

namespace inja {

using Arguments = std::vector<const json*>;
Expand Down
6 changes: 4 additions & 2 deletions include/inja/lexer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
#define INCLUDE_INJA_LEXER_HPP_

#include <cctype>
#include <locale>
#include <cstddef>
#include <string_view>

#include "config.hpp"
#include "exceptions.hpp"
#include "token.hpp"
#include "utils.hpp"

Expand Down Expand Up @@ -311,7 +313,7 @@ class Lexer {
pos += open_start;

// try to match one of the opening sequences, and get the close
std::string_view open_str = m_in.substr(pos);
const std::string_view open_str = m_in.substr(pos);
bool must_lstrip = false;
if (inja::string_view::starts_with(open_str, config.expression_open)) {
if (inja::string_view::starts_with(open_str, config.expression_open_force_lstrip)) {
Expand Down
6 changes: 5 additions & 1 deletion include/inja/node.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#ifndef INCLUDE_INJA_NODE_HPP_
#define INCLUDE_INJA_NODE_HPP_

#include <cstddef>
#include <memory>
#include <string>
#include <string_view>
#include <utility>
#include <tuple>
#include <vector>

#include "function_storage.hpp"
#include "inja.hpp"
#include "utils.hpp"

namespace inja {
Expand Down
22 changes: 13 additions & 9 deletions include/inja/parser.hpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
#ifndef INCLUDE_INJA_PARSER_HPP_
#define INCLUDE_INJA_PARSER_HPP_

#include <limits>
#include <cstddef>
#include <fstream>
#include <iterator>
#include <memory>
#include <stack>
#include <string>
#include <string_view>
#include <utility>
#include <vector>

#include "config.hpp"
#include "exceptions.hpp"
#include "function_storage.hpp"
#include "inja.hpp"
#include "lexer.hpp"
#include "node.hpp"
#include "template.hpp"
#include "token.hpp"
#include "utils.hpp"

namespace inja {

Expand Down Expand Up @@ -64,7 +68,7 @@ class Parser {
}

inline void add_literal(Arguments &arguments, const char* content_ptr) {
std::string_view data_text(literal_start.data(), tok.text.data() - literal_start.data() + tok.text.size());
const std::string_view data_text(literal_start.data(), tok.text.data() - literal_start.data() + tok.text.size());
arguments.emplace_back(std::make_shared<LiteralNode>(data_text, data_text.data() - content_ptr));
}

Expand All @@ -88,8 +92,8 @@ class Parser {
return;
}

std::string original_path = static_cast<std::string>(path);
std::string original_name = template_name;
const std::string original_path = static_cast<std::string>(path);
const std::string original_name = template_name;

if (config.search_included_templates_in_files) {
// Build the relative path
Expand All @@ -103,7 +107,7 @@ class Parser {
std::ifstream file;
file.open(template_name);
if (!file.fail()) {
std::string text((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
const std::string text((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());

auto include_template = Template(text);
template_storage.emplace(template_name, include_template);
Expand Down Expand Up @@ -472,7 +476,7 @@ class Parser {
throw_parser_error("expected id, got '" + tok.describe() + "'");
}

Token key_token = std::move(value_token);
const Token key_token = std::move(value_token);
value_token = tok;
get_next_token();

Expand Down Expand Up @@ -533,7 +537,7 @@ class Parser {
throw_parser_error("expected variable name, got '" + tok.describe() + "'");
}

std::string key = static_cast<std::string>(tok.text);
const std::string key = static_cast<std::string>(tok.text);
get_next_token();

auto set_statement_node = std::make_shared<SetStatementNode>(key, tok.text.data() - tmpl.content.c_str());
Expand Down Expand Up @@ -627,7 +631,7 @@ class Parser {
}

void parse_into_template(Template& tmpl, std::string_view filename) {
std::string_view path = filename.substr(0, filename.find_last_of("/\\") + 1);
const std::string_view path = filename.substr(0, filename.find_last_of("/\\") + 1);

// StringRef path = sys::path::parent_path(filename);
auto sub_parser = Parser(config, lexer.get_config(), template_storage, function_storage);
Expand Down
20 changes: 15 additions & 5 deletions include/inja/renderer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,23 @@
#define INCLUDE_INJA_RENDERER_HPP_

#include <algorithm>
#include <array>
#include <cctype>
#include <cmath>
#include <cstddef>
#include <memory>
#include <numeric>
#include <ostream>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#include "config.hpp"
#include "exceptions.hpp"
#include "function_storage.hpp"
#include "inja.hpp"
#include "node.hpp"
#include "template.hpp"
#include "utils.hpp"
Expand Down Expand Up @@ -69,7 +79,7 @@ class Renderer : public NodeVisitor {
return buffer;
}

void print_data(const std::shared_ptr<json> value) {
void print_data(const std::shared_ptr<json>& value) {
if (value->is_string()) {
if (config.html_autoescape) {
*output_stream << htmlescape(value->get_ref<const json::string_t&>());
Expand Down Expand Up @@ -107,7 +117,7 @@ class Renderer : public NodeVisitor {
throw_renderer_error("expression could not be evaluated", expression_list);
}

auto node = not_found_stack.top();
const auto node = not_found_stack.top();
not_found_stack.pop();

throw_renderer_error("variable '" + static_cast<std::string>(node->name) + "' not found", *node);
Expand All @@ -116,7 +126,7 @@ class Renderer : public NodeVisitor {
}

void throw_renderer_error(const std::string& message, const AstNode& node) {
SourceLocation loc = get_source_location(current_template->content, node.pos);
const SourceLocation loc = get_source_location(current_template->content, node.pos);
INJA_THROW(RenderError(message, loc));
}

Expand Down Expand Up @@ -158,7 +168,7 @@ class Renderer : public NodeVisitor {

template <bool throw_not_found = true> Arguments get_argument_vector(const FunctionNode& node) {
const size_t N = node.arguments.size();
for (auto a : node.arguments) {
for (const auto& a : node.arguments) {
a->accept(*this);
}

Expand All @@ -184,7 +194,7 @@ class Renderer : public NodeVisitor {
}

void visit(const BlockNode& node) {
for (auto& n : node.nodes) {
for (const auto& n : node.nodes) {
n->accept(*this);

if (break_rendering) {
Expand Down
4 changes: 2 additions & 2 deletions include/inja/statistics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace inja {
*/
class StatisticsVisitor : public NodeVisitor {
void visit(const BlockNode& node) {
for (auto& n : node.nodes) {
for (const auto& n : node.nodes) {
n->accept(*this);
}
}
Expand All @@ -24,7 +24,7 @@ class StatisticsVisitor : public NodeVisitor {
}

void visit(const FunctionNode& node) {
for (auto& n : node.arguments) {
for (const auto& n : node.arguments) {
n->accept(*this);
}
}
Expand Down
3 changes: 1 addition & 2 deletions include/inja/template.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <map>
#include <memory>
#include <string>
#include <vector>

#include "node.hpp"
#include "statistics.hpp"
Expand All @@ -23,7 +22,7 @@ struct Template {
explicit Template(const std::string& content): content(content) {}

/// Return number of variables (total number, not distinct ones) in the template
int count_variables() {
int count_variables() const {
auto statistic_visitor = StatisticsVisitor();
root.accept(statistic_visitor);
return statistic_visitor.variable_counter;
Expand Down
8 changes: 4 additions & 4 deletions include/inja/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#define INCLUDE_INJA_UTILS_HPP_

#include <algorithm>
#include <fstream>
#include <cstddef>
#include <string>
#include <string_view>
#include <utility>
Expand All @@ -19,7 +19,7 @@ inline std::string_view slice(std::string_view view, size_t start, size_t end) {
}

inline std::pair<std::string_view, std::string_view> split(std::string_view view, char Separator) {
size_t idx = view.find(Separator);
const size_t idx = view.find(Separator);
if (idx == std::string_view::npos) {
return std::make_pair(view, std::string_view());
}
Expand All @@ -34,7 +34,7 @@ inline bool starts_with(std::string_view view, std::string_view prefix) {
inline SourceLocation get_source_location(std::string_view content, size_t pos) {
// Get line and offset position (starts at 1:1)
auto sliced = string_view::slice(content, 0, pos);
std::size_t last_newline = sliced.rfind("\n");
const std::size_t last_newline = sliced.rfind('\n');

if (last_newline == std::string_view::npos) {
return {1, sliced.length() + 1};
Expand All @@ -44,7 +44,7 @@ inline SourceLocation get_source_location(std::string_view content, size_t pos)
size_t count_lines = 0;
size_t search_start = 0;
while (search_start <= sliced.size()) {
search_start = sliced.find("\n", search_start) + 1;
search_start = sliced.find('\n', search_start) + 1;
if (search_start == 0) {
break;
}
Expand Down
Loading

0 comments on commit 92994be

Please sign in to comment.