# depend.cpp File Reference

Custom implementation of Makedepend. More...

`#include <stdio.h>`
`#include <stdlib.h>`
`#include <string.h>`
`#include <ctype.h>`
`#include <limits.h>`
`#include <unistd.h>`
`#include <map>`
`#include <set>`
`#include <stack>`

Go to the source code of this file.

## Data Structures
struct | StringCompare
| Simple string comparator using strcmp as implementation. More...
class | File
| Helper class to read a file. More...
class | Lexer
| Lexer of a file. More...
## Defines
#define | PATH_MAX 260
## Typedefs
typedef std::set< const char *, StringCompare > | StringSet
| Set of C-style strings.
typedef std::map< const char *, StringSet *, StringCompare > | StringMap
| Mapping of C-style string to a set of C-style strings.
typedef std::pair< const char *, StringSet * > | StringMapItem
| Pair of C-style string and a set of C-style strings.
typedef std::map< const char *, Token, StringCompare > | KeywordList
| Mapping from a C-style keyword representation to a Token.
## Enumerations
enum | Token { TOKEN_UNKNOWN, TOKEN_END, TOKEN_EOL, TOKEN_SHARP, TOKEN_LOCAL, TOKEN_GLOBAL, TOKEN_IDENTIFIER, TOKEN_DEFINE, TOKEN_IF, TOKEN_IFDEF, TOKEN_IFNDEF, TOKEN_ELIF, TOKEN_ELSE, TOKEN_ENDIF, TOKEN_UNDEF, TOKEN_OR, TOKEN_AND, TOKEN_DEFINED, TOKEN_OPEN, TOKEN_CLOSE, TOKEN_NOT, TOKEN_ZERO, TOKEN_INCLUDE }
| A token returned by the tokenizer. More...
enum | Ignore { NOT_IGNORE, IGNORE_UNTIL_ELSE, IGNORE_UNTIL_ENDIF }
| Enumerator to tell how long to ignore 'stuff'. More...
## Functions
const char * | GeneratePath (const char *dirname, const char *filename, bool local)
| Generate a path from a directory name and a relative filename.
bool | ExpressionDefined (Lexer *lexer, StringSet *defines, bool verbose)
| Try to parse a 'defined(expr)' expression.
bool | ExpressionOr (Lexer *lexer, StringSet *defines, bool verbose)
| Try to parse a 'expr || expr' expression.
bool | ExpressionNot (Lexer *lexer, StringSet *defines, bool verbose)
| Try to parse a '!expr' expression.
bool | ExpressionAnd (Lexer *lexer, StringSet *defines, bool verbose)
| Try to parse a 'expr && expr' expression.
void | ScanFile (const char *filename, const char *ext, bool header, bool verbose)
| Scan a file for includes, defines and the lot.
int | main (int argc, char *argv[])
| Entry point.
## Variables
static StringSet | _include_dirs
| Include directory to search in.
static StringMap | _files
| Files that have been parsed/handled with their dependencies.
static StringMap | _headers
| Dependencies of headers.
static StringSet | _defines
| The current 'active' defines.

## Detailed Description

Custom implementation of Makedepend.

We previously used makedepend, but that could not handle the amount of files we have and does not handle conditional includes in a sane manner. This caused many link problems because not enough files were recompiled. This has lead to the development of our own dependency generator. It is meant to be a substitute to the (relatively slow) dependency generation via gcc. It thus helps speeding up compilation. It will also ignore system headers making it less error prone when system headers are moved or renamed.

Definition in file depend.cpp.

## Typedef Documentation

typedef std::map<const char*, Token, StringCompare> KeywordList

Mapping from a C-style keyword representation to a Token.

Definition at line 153 of file depend.cpp.

typedef std::map<const char*, StringSet*, StringCompare> StringMap

Mapping of C-style string to a set of C-style strings.

Definition at line 53 of file depend.cpp.

typedef std::pair<const char*, StringSet*> StringMapItem

Pair of C-style string and a set of C-style strings.

Definition at line 55 of file depend.cpp.

typedef std::set<const char*, StringCompare> StringSet

Set of C-style strings.

Definition at line 51 of file depend.cpp.

## Enumeration Type Documentation

enum Ignore

Enumerator to tell how long to ignore 'stuff'.

Enumerator:

  NOT_IGNORE | No ignoring.
  IGNORE_UNTIL_ELSE | Ignore till a else is reached.
  IGNORE_UNTIL_ENDIF | Ignore till a endif is reached.

Definition at line 596 of file depend.cpp.

enum Token

A token returned by the tokenizer.

Enumerator:

  TOKEN_UNKNOWN | Unknown token.
  TOKEN_END | End of document.
  TOKEN_EOL | End of line.
  TOKEN_SHARP | # character, usually telling something important comes.
  TOKEN_LOCAL | Read a local include.
  TOKEN_GLOBAL | Read a global include.
  TOKEN_IDENTIFIER | Identifier within the data.
  TOKEN_DEFINE | (#)define in code
  TOKEN_IF | (#)if in code
  TOKEN_IFDEF | (#)ifdef in code
  TOKEN_IFNDEF | (#)ifndef in code
  TOKEN_ELIF | (#)elif in code
  TOKEN_ELSE | (#)else in code
  TOKEN_ENDIF | (#)endif in code
  TOKEN_UNDEF | (#)undef in code
  TOKEN_OR | '||' within if expression
  TOKEN_AND | '&&' within if expression
  TOKEN_DEFINED | 'defined' within if expression
  TOKEN_OPEN | '(' within if expression
  TOKEN_CLOSE | ')' within if expression
  TOKEN_NOT | '!' within if expression
  TOKEN_ZERO | '0' within if expression
  TOKEN_INCLUDE | (#)include in code

Definition at line 126 of file depend.cpp.

## Function Documentation

bool ExpressionAnd | ( | Lexer * | lexer,
|  | StringSet * | defines,
|  | bool | verbose |
| ) |  |  |

Try to parse a 'expr && expr' expression.

Parameters:

  | lexer | the lexer to get tokens from.
  | defines | the set of known defines.
  | verbose | whether to give verbose debugging information.

Returns:
  the value of the expression.

Definition at line 564 of file depend.cpp.

References ExpressionDefined(), Lexer::GetToken(), Lexer::Lex(), and TOKEN_AND.

Referenced by ExpressionOr().

bool ExpressionDefined | ( | Lexer * | lexer,
|  | StringSet * | defines,
|  | bool | verbose |
| ) |  |  |

Try to parse a 'defined(expr)' expression.

Parameters:

  | lexer | the lexer to get tokens from.
  | defines | the set of known defines.
  | verbose | whether to give verbose debugging information.

Returns:
  the value of the expression.

Definition at line 534 of file depend.cpp.

References ExpressionNot(), Lexer::GetString(), Lexer::GetToken(), Lexer::Lex(), TOKEN_DEFINED, TOKEN_IDENTIFIER, and TOKEN_OPEN.

Referenced by ExpressionAnd(), and ExpressionNot().

bool ExpressionNot | ( | Lexer * | lexer,
|  | StringSet * | defines,
|  | bool | verbose |
| ) |  |  |

Try to parse a '!expr' expression.

Also parses the '(expr)', '0' and identifiers. Finally it also consumes any unknown tokens.

Parameters:

  | lexer | the lexer to get tokens from.
  | defines | the set of known defines.
  | verbose | whether to give verbose debugging information.

Returns:
  the value of the expression.

Definition at line 491 of file depend.cpp.

References ExpressionDefined(), ExpressionOr(), Lexer::GetToken(), Lexer::Lex(), TOKEN_IDENTIFIER, TOKEN_NOT, TOKEN_OPEN, TOKEN_UNKNOWN, and TOKEN_ZERO.

Referenced by ExpressionDefined().

bool ExpressionOr | ( | Lexer * | lexer,
|  | StringSet * | defines,
|  | bool | verbose |
| ) |  |  |

Try to parse a 'expr || expr' expression.

Parameters:

  | lexer | the lexer to get tokens from.
  | defines | the set of known defines.
  | verbose | whether to give verbose debugging information.

Returns:
  the value of the expression.

Definition at line 583 of file depend.cpp.

References ExpressionAnd(), Lexer::GetToken(), Lexer::Lex(), and TOKEN_OR.

Referenced by ExpressionNot(), and ScanFile().

const char* GeneratePath | ( | const char * | dirname,
|  | const char * | filename,
|  | bool | local |
| ) |  |  |

Generate a path from a directory name and a relative filename.

If the file is not local the include directory names will be used instead of the passed parameter with directory name. If the file is local both will be queried where the parameter takes precedence.

Parameters:

  | dirname | the directory to look in.
  | filename | the file to look for.
  | local | whether to look locally (in dirname) for the file.

Returns:
  the absolute path, or NULL if the file doesn't exist.

Definition at line 422 of file depend.cpp.

References _include_dirs.

Referenced by ScanFile().

int main | ( | int | argc,
|  | char * | argv[] |
| ) |  |  |

Entry point.

Arguably the most common function in all applications.

Parameters:

  | argc | the number of arguments.
  | argv | the actual arguments.

Returns:
  return value for the caller to tell we succeed or not.

Definition at line 823 of file depend.cpp.

References _defines, _files, _headers, _include_dirs, and ScanFile().

void ScanFile | ( | const char * | filename,
|  | const char * | ext,
|  | bool | header,
|  | bool | verbose |
| ) |  |  |

Scan a file for includes, defines and the lot.

Parameters:

  | filename | the name of the file to scan.
  | ext | the extension of the filename.
  | header | whether the file is a header or not.
  | verbose | whether to give verbose debugging information.

Definition at line 609 of file depend.cpp.

References _defines, _files, _headers, ExpressionOr(), GeneratePath(), File::GetDirname(), Lexer::GetString(), Lexer::GetToken(), IGNORE_UNTIL_ELSE, IGNORE_UNTIL_ENDIF, Lexer::Lex(), NOT_IGNORE, TOKEN_DEFINE, TOKEN_ELIF, TOKEN_ELSE, TOKEN_END, TOKEN_ENDIF, TOKEN_EOL, TOKEN_GLOBAL, TOKEN_IDENTIFIER, TOKEN_IF, TOKEN_IFDEF, TOKEN_IFNDEF, TOKEN_INCLUDE, TOKEN_LOCAL, TOKEN_SHARP, and TOKEN_UNDEF.

Referenced by main().

## Variable Documentation

StringSet _defines`[static]`

The current 'active' defines.

Definition at line 64 of file depend.cpp.

Referenced by main(), and ScanFile().

StringMap _files`[static]`

Files that have been parsed/handled with their dependencies.

Definition at line 60 of file depend.cpp.

Referenced by main(), and ScanFile().

StringMap _headers`[static]`

Dependencies of headers.

Definition at line 62 of file depend.cpp.

Referenced by main(), and ScanFile().

StringSet _include_dirs`[static]`

Include directory to search in.

Definition at line 58 of file depend.cpp.

Referenced by GeneratePath(), and main().