xref: /src/contrib/llvm-project/llvm/lib/TableGen/Parser.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4) !
1145449b1SDimitry Andric //===- Parser.cpp - Top-Level TableGen Parser implementation --------------===//
2145449b1SDimitry Andric //
3145449b1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4145449b1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5145449b1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6145449b1SDimitry Andric //
7145449b1SDimitry Andric //===----------------------------------------------------------------------===//
8145449b1SDimitry Andric 
9145449b1SDimitry Andric #include "llvm/TableGen/Parser.h"
10145449b1SDimitry Andric #include "TGParser.h"
11145449b1SDimitry Andric #include "llvm/Support/MemoryBuffer.h"
12145449b1SDimitry Andric #include "llvm/TableGen/Record.h"
13145449b1SDimitry Andric 
14145449b1SDimitry Andric using namespace llvm;
15145449b1SDimitry Andric 
TableGenParseFile(SourceMgr & InputSrcMgr,RecordKeeper & Records)16145449b1SDimitry Andric bool llvm::TableGenParseFile(SourceMgr &InputSrcMgr, RecordKeeper &Records) {
17145449b1SDimitry Andric   // Initialize the global TableGen source manager by temporarily taking control
18145449b1SDimitry Andric   // of the input buffer in `SrcMgr`. This is kind of a hack, but allows for
19145449b1SDimitry Andric   // preserving TableGen's current awkward diagnostic behavior. If we can remove
20145449b1SDimitry Andric   // this reliance, we could drop all of this.
21145449b1SDimitry Andric   SrcMgr = SourceMgr();
22145449b1SDimitry Andric   SrcMgr.takeSourceBuffersFrom(InputSrcMgr);
23145449b1SDimitry Andric   SrcMgr.setIncludeDirs(InputSrcMgr.getIncludeDirs());
24145449b1SDimitry Andric   SrcMgr.setDiagHandler(InputSrcMgr.getDiagHandler(),
25145449b1SDimitry Andric                         InputSrcMgr.getDiagContext());
26145449b1SDimitry Andric 
27145449b1SDimitry Andric   // Setup the record keeper and try to parse the file.
28145449b1SDimitry Andric   auto *MainFileBuffer = SrcMgr.getMemoryBuffer(SrcMgr.getMainFileID());
29145449b1SDimitry Andric   Records.saveInputFilename(MainFileBuffer->getBufferIdentifier().str());
30145449b1SDimitry Andric 
31e3b55780SDimitry Andric   TGParser Parser(SrcMgr, /*Macros=*/std::nullopt, Records,
32e3b55780SDimitry Andric                   /*NoWarnOnUnusedTemplateArgs=*/false,
33e3b55780SDimitry Andric                   /*TrackReferenceLocs=*/true);
34145449b1SDimitry Andric   bool ParseResult = Parser.ParseFile();
35145449b1SDimitry Andric 
36145449b1SDimitry Andric   // After parsing, reclaim the source manager buffers from TableGen's global
37145449b1SDimitry Andric   // manager.
38145449b1SDimitry Andric   InputSrcMgr.takeSourceBuffersFrom(SrcMgr);
39145449b1SDimitry Andric   SrcMgr = SourceMgr();
40145449b1SDimitry Andric   return ParseResult;
41145449b1SDimitry Andric }
42