Skip to content

Commit

Permalink
Add a few clarifications
Browse files Browse the repository at this point in the history
  • Loading branch information
agagniere committed Feb 10, 2024
1 parent b264171 commit 5279f40
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
- name: Checkout
uses: actions/checkout@v3
- name: Generate html
run: Open=echo ClangFormat=clang-format-14 make -C book html
run: Open=echo ClangFormat=clang-format-15 CC=clang-15 make -C book html
- name: Upload artifact
uses: actions/upload-pages-artifact@v2
with:
Expand Down
4 changes: 4 additions & 0 deletions book/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
*.exe
*.i
*.o
*.txt
.env
preprocessed
sphinx
5 changes: 3 additions & 2 deletions book/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,11 @@ env: $(VenvActivate) ## Install the python virtual environment with dependencies
$(DoxygenHtml): Doxyfile.cfg $(Pages) $(SourceCode) Makefile
PROJECT_VERSION=$(Version) doxygen $<

# Preprocess samples
$(ProcessedSamples): $(PreproFolder)/%.i: $(SampleFolder)/%.c | $(PreproFolder)
temp="$(@D)/_raw_$(@F)" ; \
$(CC) -E $(CPPFLAGS) -P $< -o "$$temp" ; \
discard=$$(grep "int main" "$$temp" -n | cut -d':' -f1 | tail -1) ; \
$(CC) -E $(CPPFLAGS) -CC $< -o "$$temp" ; \
discard=$$(grep "$(<F)" "$$temp" -n | cut -d':' -f1 | tail -1) ; \
tail +$$(( $$discard + 1 )) "$$temp" | $(ClangFormat) > $@

# Compile code samples
Expand Down
7 changes: 5 additions & 2 deletions book/pages/01_preprocessor.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ What that means, is that the preprocessor manipulates text, not values:

[^strlen]: While in theory not different from other functions, `strlen` _may_ be computed at compile-time in practice, as an inlined compiler built-in, when its input is a string literal.

Interacting with the preprocessor is done by starting a line with the `#` character, followed by a preprocessing directive.
Interacting with the preprocessor is done by starting a line with the `#` character, followed by a preprocessing directive. (Any number of spaces can be present before and after the `#` character)

## Directives

Expand Down Expand Up @@ -89,7 +89,7 @@ _Source_: {bdg-link-primary-line}`cppreference <https://en.cppreference.com/w/c/
`#ifndef`{l=C} _MACRO_
: Equivalent to `#if !defined(MACRO)`{l=C}

`#elif`{l=C} condition2 _B_ `#endif`{l=C}
`#elif`{l=C} _condition2_ _B_ `#endif`{l=C}
: Convenient way to chain multiple conditions without nesting, equivalent to:
```C
#else
Expand Down Expand Up @@ -138,3 +138,6 @@ It should be of no surprise then, that the 2 preprocessor operators are about ma
`##`
: Concatenate 2 tokens
: {bdg-primary-line}`some` {bdg-primary-line}`thing` {octicon}`arrow-right` {bdg-primary-line}`something`

It is only when expanding a macro that the preprocessor takes these operators into account.
It means they can only be used within the definition of a macro.
6 changes: 3 additions & 3 deletions book/samples/02_constants.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
/* Reducing the amount of magic numbers */
int main(int arg_count, char** arg_values)
{
if (arg_count <= 1 || strchr(arg_values[1], '-') == NULL)
if (arg_count <= 1 || strchr(arg_values[1], '=') == NULL)
{
dprintf(STDERR_FILENO, "Expected an argument\n");
dprintf(STDERR_FILENO, "Expected an assignation\n");
return EXIT_FAILURE;
}
dprintf(STDOUT_FILENO, "Received argument: %s\n", arg_values[1] + 1);
dprintf(STDOUT_FILENO, "Environment:\n\t%s\n", arg_values[1]);
return EXIT_SUCCESS;
}

0 comments on commit 5279f40

Please sign in to comment.