Managing the input files for the compiler
A real compiler has to deal with many files. Usually, the developer calls the compiler with the name of the main compilation unit. This compilation unit can refer to other files – for example, via #include
directives in C or import
statements in Python or Modula-2. An imported module can import other modules, and so on. All these files must be loaded into memory and run through the analysis stages of the compiler. During development, a developer may make syntactical or semantical errors. When detected, an error message, including the source line and a marker, should be printed. This essential component is not trivial.
Luckily, LLVM comes with a solution: the llvm::SourceMgr
class. A new source file is added to SourceMgr
with a call to the AddNewSourceBuffer()
method. Alternatively, a file can be loaded with a call to the AddIncludeFile()
method. Both methods return an ID to identify the buffer. You can use this ID to retrieve a pointer...