blob: 125b0cb63fd97194a403b234b05f68373add0b9c [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- Sema.cpp - AST Builder and Semantic Analysis Implementation ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the actions class which performs semantic analysis and
11// builds an AST out of a parse stream.
12//
13//===----------------------------------------------------------------------===//
14
15#include "Sema.h"
16#include "clang/AST/ASTContext.h"
17#include "clang/Lex/Preprocessor.h"
18#include "clang/Basic/Diagnostic.h"
Chris Lattner2e64c072007-08-10 20:18:51 +000019
Chris Lattner4b009652007-07-25 00:24:17 +000020using namespace clang;
21
Steve Naroff9637a9b2007-10-09 22:01:59 +000022void Sema::ActOnTranslationUnitScope(SourceLocation Loc, Scope *S) {
23 TUScope = S;
24}
25
Chris Lattner4b009652007-07-25 00:24:17 +000026Sema::Sema(Preprocessor &pp, ASTContext &ctxt, std::vector<Decl*> &prevInGroup)
27 : PP(pp), Context(ctxt), CurFunctionDecl(0), LastInGroupList(prevInGroup) {
Chris Lattner2e64c072007-08-10 20:18:51 +000028
29 // Get IdentifierInfo objects for known functions for which we
30 // do extra checking.
31 IdentifierTable& IT = PP.getIdentifierTable();
32
33 KnownFunctionIDs[ id_printf ] = &IT.get("printf");
34 KnownFunctionIDs[ id_fprintf ] = &IT.get("fprintf");
35 KnownFunctionIDs[ id_sprintf ] = &IT.get("sprintf");
36 KnownFunctionIDs[ id_snprintf ] = &IT.get("snprintf");
Chris Lattner2e64c072007-08-10 20:18:51 +000037 KnownFunctionIDs[ id_asprintf ] = &IT.get("asprintf");
Ted Kremenek2d7e9532007-08-10 21:13:51 +000038 KnownFunctionIDs[ id_vsnprintf ] = &IT.get("vsnprintf");
Chris Lattner2e64c072007-08-10 20:18:51 +000039 KnownFunctionIDs[ id_vasprintf ] = &IT.get("vasprintf");
40 KnownFunctionIDs[ id_vfprintf ] = &IT.get("vfprintf");
41 KnownFunctionIDs[ id_vsprintf ] = &IT.get("vsprintf");
42 KnownFunctionIDs[ id_vprintf ] = &IT.get("vprintf");
Chris Lattner4b009652007-07-25 00:24:17 +000043}
44
Chris Lattnerb26b7ad2007-08-31 04:53:24 +000045void Sema::DeleteExpr(ExprTy *E) {
46 delete static_cast<Expr*>(E);
47}
48void Sema::DeleteStmt(StmtTy *S) {
49 delete static_cast<Stmt*>(S);
50}
51
Chris Lattner4b009652007-07-25 00:24:17 +000052//===----------------------------------------------------------------------===//
53// Helper functions.
54//===----------------------------------------------------------------------===//
55
56bool Sema::Diag(SourceLocation Loc, unsigned DiagID) {
57 PP.getDiagnostics().Report(Loc, DiagID);
58 return true;
59}
60
61bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg) {
62 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1);
63 return true;
64}
65
66bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
67 const std::string &Msg2) {
68 std::string MsgArr[] = { Msg1, Msg2 };
69 PP.getDiagnostics().Report(Loc, DiagID, MsgArr, 2);
70 return true;
71}
72
73bool Sema::Diag(SourceLocation Loc, unsigned DiagID, SourceRange Range) {
74 PP.getDiagnostics().Report(Loc, DiagID, 0, 0, &Range, 1);
75 return true;
76}
77
78bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
79 SourceRange Range) {
80 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1, &Range, 1);
81 return true;
82}
83
84bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg1,
85 const std::string &Msg2, SourceRange Range) {
86 std::string MsgArr[] = { Msg1, Msg2 };
87 PP.getDiagnostics().Report(Loc, DiagID, MsgArr, 2, &Range, 1);
88 return true;
89}
90
91bool Sema::Diag(SourceLocation Loc, unsigned DiagID,
92 SourceRange R1, SourceRange R2) {
93 SourceRange RangeArr[] = { R1, R2 };
94 PP.getDiagnostics().Report(Loc, DiagID, 0, 0, RangeArr, 2);
95 return true;
96}
97
98bool Sema::Diag(SourceLocation Loc, unsigned DiagID, const std::string &Msg,
99 SourceRange R1, SourceRange R2) {
100 SourceRange RangeArr[] = { R1, R2 };
101 PP.getDiagnostics().Report(Loc, DiagID, &Msg, 1, RangeArr, 2);
102 return true;
103}
104
105bool Sema::Diag(SourceLocation Range, unsigned DiagID, const std::string &Msg1,
106 const std::string &Msg2, SourceRange R1, SourceRange R2) {
107 std::string MsgArr[] = { Msg1, Msg2 };
108 SourceRange RangeArr[] = { R1, R2 };
109 PP.getDiagnostics().Report(Range, DiagID, MsgArr, 2, RangeArr, 2);
110 return true;
111}
112
113const LangOptions &Sema::getLangOptions() const {
114 return PP.getLangOptions();
115}