xref: /llvm-project/llvm/lib/TableGen/Parser.cpp (revision d60a65abb6b050e10d9efdbc56dcb2e2e4772af1)
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 "RecordContext.h"
11 #include "TGParser.h"
12 #include "llvm/Support/MemoryBuffer.h"
13 #include "llvm/TableGen/Error.h"
14 #include "llvm/TableGen/Record.h"
15 
16 using namespace llvm;
17 
18 bool llvm::TableGenParseFile(std::unique_ptr<MemoryBuffer> Buffer,
19                              std::vector<std::string> IncludeDirs,
20                              TableGenParserFn ParserFn) {
21   RecordKeeper Records;
22   Records.saveInputFilename(Buffer->getBufferIdentifier().str());
23 
24   SrcMgr = SourceMgr();
25   SrcMgr.AddNewSourceBuffer(std::move(Buffer), SMLoc());
26   SrcMgr.setIncludeDirs(IncludeDirs);
27   TGParser Parser(SrcMgr, /*Macros=*/None, Records);
28   if (Parser.ParseFile())
29     return true;
30 
31   // Invoke the provided handler function.
32   if (ParserFn(Records))
33     return true;
34 
35   // After parsing, reset the tablegen data.
36   detail::resetTablegenRecordContext();
37   SrcMgr = SourceMgr();
38   return false;
39 }
40