LLVM  15.0.0git
Parser.cpp
Go to the documentation of this file.
1 //===- Parser.cpp - Top-Level TableGen Parser implementation --------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "llvm/TableGen/Parser.h"
10 #include "TGParser.h"
12 #include "llvm/TableGen/Record.h"
13 
14 using namespace llvm;
15 
16 bool llvm::TableGenParseFile(SourceMgr &InputSrcMgr, RecordKeeper &Records) {
17  // Initialize the global TableGen source manager by temporarily taking control
18  // of the input buffer in `SrcMgr`. This is kind of a hack, but allows for
19  // preserving TableGen's current awkward diagnostic behavior. If we can remove
20  // this reliance, we could drop all of this.
21  SrcMgr = SourceMgr();
22  SrcMgr.takeSourceBuffersFrom(InputSrcMgr);
23  SrcMgr.setIncludeDirs(InputSrcMgr.getIncludeDirs());
24  SrcMgr.setDiagHandler(InputSrcMgr.getDiagHandler(),
25  InputSrcMgr.getDiagContext());
26 
27  // Setup the record keeper and try to parse the file.
28  auto *MainFileBuffer = SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID());
29  Records.saveInputFilename(MainFileBuffer->getBufferIdentifier().str());
30 
31  TGParser Parser(SrcMgr, /*Macros=*/None, Records);
32  bool ParseResult = Parser.ParseFile();
33 
34  // After parsing, reclaim the source manager buffers from TableGen's global
35  // manager.
36  InputSrcMgr.takeSourceBuffersFrom(SrcMgr);
37  SrcMgr = SourceMgr();
38  return ParseResult;
39 }
MemoryBuffer.h
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
llvm::SourceMgr::getIncludeDirs
ArrayRef< std::string > getIncludeDirs() const
Return the include directories of this source manager.
Definition: SourceMgr.h:104
llvm::RecordKeeper
Definition: Record.h:1842
Parser.h
llvm::SrcMgr
SourceMgr SrcMgr
Definition: Error.cpp:24
llvm::SourceMgr::getMainFileID
unsigned getMainFileID() const
Definition: SourceMgr.h:132
llvm::SourceMgr::setDiagHandler
void setDiagHandler(DiagHandlerTy DH, void *Ctx=nullptr)
Specify a diagnostic handler to be invoked every time PrintMessage is called.
Definition: SourceMgr.h:112
llvm::None
const NoneType None
Definition: None.h:24
llvm::SourceMgr::getMemoryBuffer
const MemoryBuffer * getMemoryBuffer(unsigned i) const
Definition: SourceMgr.h:125
llvm::TGParser
Definition: TGParser.h:130
llvm::SourceMgr::getDiagContext
void * getDiagContext() const
Definition: SourceMgr.h:118
llvm::TableGenParseFile
bool TableGenParseFile(SourceMgr &InputSrcMgr, RecordKeeper &Records)
Parse the TableGen file defined within the main buffer of the given SourceMgr.
Definition: Parser.cpp:16
llvm::SourceMgr::getDiagHandler
DiagHandlerTy getDiagHandler() const
Definition: SourceMgr.h:117
llvm::SourceMgr
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition: SourceMgr.h:31
llvm::SourceMgr::setIncludeDirs
void setIncludeDirs(const std::vector< std::string > &Dirs)
Definition: SourceMgr.h:106
llvm::SourceMgr::takeSourceBuffersFrom
void takeSourceBuffersFrom(SourceMgr &SrcMgr, SMLoc MainBufferIncludeLoc=SMLoc())
Takes the source buffers from the given source manager and append them to the current manager.
Definition: SourceMgr.h:157
TGParser.h
llvm::RecordKeeper::saveInputFilename
void saveInputFilename(std::string Filename)
Definition: Record.h:1885
Record.h
llvm::TGParser::ParseFile
bool ParseFile()
ParseFile - Main entrypoint for parsing a tblgen file.
Definition: TGParser.cpp:3788